Librairie XML libxml.php

Rédigé par BeHuman Aucun commentaire
Classé dans : Php, XML Mots clés : aucun

Cette librairie vous permettra de manipuler des fichiers XML en PHP en toute simplicité.

<?php

/***************************************** Création XML *****************************************/
function createFileXML($domFile, $oneElement="references", $attrs="") {
    $content="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>".chr(10)
                ."<".$oneElement." ".$attrs.">".chr(10)
                ."</".$oneElement.">";
    $ourFileHandle = fopen($domFile, 'w') or die("Impossible de cr&eacute;er le fichier XML ".$domFile);
    fclose($ourFileHandle);
    setContentFile($domFile, $content);
}
function setContentFile($file, $content) {
    file_put_contents($file, $content);
}

/***************************************** Chargement XML *****************************************/
function loadFileXML($domFile) {
    $dom = new DomDocument();
    $dom->load($domFile);
    return $dom;
}

/***************************************** Sauvegarde XML *****************************************/
function saveFileXML($dom, $domFile, $compress=false) {
    $dom->formatOutput = true;
    $dom->saveXML();
    $dom->save($domFile);
    clearFile($domFile, $compress);
}

/***************************************** Nettoyage d'un fichier *****************************************/
function clearFile($file, $compress=false) {
    $content = file_get_contents($file);
    $new_content = "";
    $lines = explode(chr(10), $content);
    foreach ($lines as $line) {
        if (strlen(trim($line)) > 0) {
            if ($compress) $new_content .= trim($line) . chr(10);
            else $new_content .= trim(preg_replace("/></", ">".chr(10)."<",$line)) . chr(10);
        }
    }
    file_put_contents($file, $new_content);
    unset($lines);
    unset($content);
    unset($new_content);
}
function toBR($content) {
    return preg_replace("/".chr(10)."/", "<br/>", $content);
}
/***************************************** Lecture XML *****************************************/

//Récupérer la valeur des tags
function getTag($dom, $tagName) {
    $ltag = $dom->getElementsByTagName($tagName);
    $items=array();
    foreach($ltag as $tag) {
        $items[]=$tag->firstChild->nodeValue;
    }
    return $items; //retourne un tableau
}

function get1Tag($dom, $tagName) {
    $ltag = $dom->getElementsByTagName($tagName);
    $items=array();
    foreach($ltag as $tag) {
        $items[]=$tag->firstChild->nodeValue;
    }
    return $items[0]; //retourne un tableau
}

function get1Tag1Parent($dom, $tagName, $parentName="references") {
    $parent = $dom->getElementsByTagName($parentName)->item(0);
    $ltag = $parent->getElementsByTagName($tagName);
    $items=array();
    foreach($ltag as $tag) {
        $items[]=$tag->firstChild->nodeValue;
    }
    return $items[0]; //retourne un tableau
}

//Récupérer la valeur des attribut X dans des tags
function getAttribute($dom, $tagName, $attributeName, $parentName="references") {
    $parent = $dom->getElementsByTagName($parentName)->item(0);
    $ltag = $parent->getElementsByTagName($tagName);
    $items=array();
    foreach($ltag as $tag) {
        if ($tag->hasAttribute($attributeName)) {
            $items[]=$tag->getAttribute($attributeName);
        }
    }
    return $items; //retourne un tableau
}

//Récupérer la valeur des attribut X dans des tags
function getAttributeWithout($dom, $tagName, $attributeName, $attributeValue, $parentName="references") {
    $parent = $dom->getElementsByTagName($parentName)->item(0);
    $ltag = $parent->getElementsByTagName($tagName);
    $items=array();
    foreach($ltag as $tag) {
        if ($tag->hasAttribute($attributeName)) {
            if ($tag->getAttribute($attributeName) != $attributeValue) {
                $items[]=$tag->getAttribute($attributeName);
            }
        }
    }
    return $items; //retourne un tableau
}

//Récupérer la valeur des tags ayant l'attribut X ou une valeur d'attribut X
function getTagWithAttribute($dom, $tagName, $attributeName, $attributeValue, $parentName="references") {
    $parent = $dom->getElementsByTagName($parentName)->item(0);
    $ltag = $parent->getElementsByTagName($tagName);
    $items=array();
    foreach($ltag as $tag) {
        if ($tag->hasAttribute($attributeName)) {
            if (!empty($attributeValue) || isset($attributeValue)) {
                if ($tag->getAttribute($attributeName) == $attributeValue) {
                    $items[]=$tag->firstChild->nodeValue;
                }
            } else { $items[]=$tag->firstChild->nodeValue; }
        }
    }
    return $items; //retourne un tableau
}

//Récupérer la valeur des tags n'ayant pas l'attribut X ou une valeur d'attribut X
function getNodeWithoutAttribute($dom, $tagName, $attributeName, $attributeValue, $parentName="references") {
    $parent = $dom->getElementsByTagName($parentName)->item(0);
    $ltag = $parent->getElementsByTagName($tagName);
    $items=array();
    foreach($ltag as $tag) {
        if ($tag->hasAttribute($attributeName)) {
            if (!empty($attributeValue) || isset($attributeValue)) {
                if ($tag->getAttribute($attributeName) != $attributeValue) {
                    $items[]=$tag->firstChild;
                }
            } else { $items[]=$tag->firstChild; }
        }
    }
    return $items; //retourne un tableau
}

//Récupérer la valeur des tags n'ayant pas l'attribut X ou une valeur d'attribut X
function getTagWithoutAttribute($dom, $tagName, $attributeName, $attributeValue, $parentName="references") {
    $parent = $dom->getElementsByTagName($parentName)->item(0);
    $ltag = $parent->getElementsByTagName($tagName);
    $items=array();
    foreach($ltag as $tag) {
        if ($tag->hasAttribute($attributeName)) {
            if (!empty($attributeValue) || isset($attributeValue)) {
                if ($tag->getAttribute($attributeName) != $attributeValue) {
                    $items[]=$tag->firstChild->nodeValue;
                }
            } else { $items[]=$tag->firstChild->nodeValue; }
        }
    }
    return $items; //retourne un tableau
}

//Récupérer la valeur d'un tags via un tag ayant l'attribut X ou une valeur d'attribut X
function getChildTagWithAttribute($dom, $tagName, $attributeName, $attributeValue, $childName, $parentName="references", $posParent=0) {
    $parent = $dom->getElementsByTagName($parentName)->item($posParent);
    $ltag = $parent->getElementsByTagName($tagName);
    $items=array();
    foreach($ltag as $tag) {
        if ($tag->hasAttribute($attributeName)) {
            if (!empty($attributeValue) || isset($attributeValue)) {
                if ($tag->getAttribute($attributeName) == $attributeValue) {
                    $lltag = $tag->getElementsByTagName($childName);
                    foreach ($lltag as $child){
                        if ($child->localName==$childName) {
                            $items[]=$child->firstChild->nodeValue;
                        }
                    }
                    
                }
            }
        }
    }
    return $items; //retourne un tableau
}

/***************************************** Ajout XML *****************************************/

//Ajouter un Tag
function setTag($dom, $tagName, $tagValue, $parentName="references", $posParent=0) {
    $dom->formatOutput = true;
    $new_tag = $dom->createElement($tagName, $tagValue);
    if (isset($parentName)) {
        $parent = $dom->getElementsByTagName($parentName)->item($posParent);
        $parent->appendChild($new_tag);
    } else { $dom->appendChild($new_tag); }
    $dom->saveXML();
}

//Ajouter un Tag avec l'attribut X dans un autre Tag
function setTagWithAttribute($dom, $tagName, $tagValue, $attributeName, $attributeValue, $parentName="references", $posParent=0) {
    $dom->formatOutput = true;
    $new_tag = $dom->createElement($tagName, $tagValue);
    $new_tag->setAttribute($attributeName, $attributeValue);
    if (isset($parentName)) {
        $parent = $dom->getElementsByTagName($parentName)->item((isset($posParent)?$posParent:0));
        $parent->appendChild($new_tag);
    } else { $dom->appendChild($new_tag); }
    $dom->saveXML();
}

//Ajouter un Tag dans un autre Tag ayant l'attribut X
function setTagInTagWithAttribute($dom, $tagName, $tagValue, $attributeName, $attributeValue, $parentName="references") {
    $dom->formatOutput = true;
    $items = $dom->getElementsByTagName($parentName);
    foreach($items as $item) {
        if (strtolower($item->getAttribute($attributeName)) == $attributeValue) {
            $new_tag = $dom->createElement($tagName, $tagValue);
            $item->appendChild($new_tag);
            $dom->saveXML();
        }
    }
}

/***************************************** Suppression XML *****************************************/

//Supprimer un Tag
function delTag($dom, $tagName) {
    $dom->formatOutput = true;
    $domElement = $dom->documentElement;
    $items = $domElement->getElementsByTagName($tagName);
    foreach($items as $item) {
        $domElement->removeChild($item);
        $dom->saveXML();
    }
}

//Supprimer un Tag avec l'attribut X
function delTagWithAttribute($dom, $tagName, $attributeName, $attributeValue) {
    $dom->formatOutput = true;
    $domElement = $dom->documentElement;
    $items = $domElement->getElementsByTagName($tagName);
    foreach($items as $item) {
        if ($item->getAttribute($attributeName) == $attributeValue) {
            $domElement->removeChild($item);
            $dom->saveXML();
        }
    }
}



/***************************************** Update XML *****************************************/

//Update un Tag
function updateTag($dom, $domFile, $tagName, $tagValue, $parentName="references", $posParent=0) {
    delTag($dom, $tagName);
    setTag($dom, $tagName, $tagValue, $parentName, $posParent);
}
//Update un Tag avec un attribut
function updateTagWithAttribute($dom, $tagName, $tagValue, $attributeName, $attributeValue , $parentName="references", $posParent=0) {
    delTagWithAttribute($dom, $tagName, $attributeName, $attributeValue);
    setTagWithAttribute($dom, $tagName, $tagValue, $attributeName, $attributeValue, $parentName, $posParent);
}


/***************************************** hash SESSION XML *****************************************/
function sessionXML() {
    return sha1($_SERVER['SERVER_ADDR'].':'.$_SERVER['HTTP_USER_AGENT']);
}
?>

 

Écrire un commentaire

Quelle est la dernière lettre du mot 9lgaiy ?

Fil RSS des commentaires de cet article