XML
- 1960 SGML standard utilisé par l’industrie aéronautique
- 1995 HTML
- 1998 XML eXtensible Markup Language
- 2000 XHTML
XML est un méta-langage qui permet de définir d’autres langages.
XML est un langage orienté données, il est très répandu ce qui fait que l’on retrouve une API pour XML pour tous langages de programmation
DOM
Le DOM est un langage permettant de représenter sous forme d’objet un document.
<?xml version="1.0" encoding="UTF-8"?> <library> <!-- library = noeud racine & racine du document (1er element rencontré) --> <!-- isbn10 = noeud attribut, book,title,author = noeud element, "mon titre","auteur" ... = noeud texte --> <book isbn10="1234567890"> <title>Mon titre</title> <author>Auteur</author> </book> <book isbn10="1234587890"> <title>Mon titre 2</title> <author>Auteur 2</author> </book> <book isbn10="1234567890"> <title>Mon titre 3</title> <author>Auteur</author> </book> <book isbn10="1234567090"> <title>Mon titre 4</title> <author>Auteur</author> </book> <book isbn10="1134567890"> <title>Mon titre 5</title> <author>Auteur 2</author> </book> <book isbn10="12345-7890"> <title>Mon titre 6</title> <author>Auteur</author> </book> <book isbn10="1239567890"> <title>Mon titre 7</title> <author>Auteur</author> </book> <book isbn10="1234554890"> <title>Mon titre 8</title> <author>Auteur 2</author> </book> </library>
Un exemple d’utilisation du DOM par l’API de PHP donnerais pour la lecture :
$doc = new DOMDocument();
//On charge le fichier
$doc->load('xml.xml');
//on récupère le noeud racine, ici library
$rootNode = $doc->documentElement;
//on vérifie que c'est le bon
if($rootNode->nodeType == XML_ELEMENT_NODE && $rootNode->tagName == 'library')
{
//On récupère l'enfant
$first = $rootNode->firstChild; // C'est un noeud texte vide! Le retour chariot est pris en compte.
echo $rootNode->firstChild->nextSibling->tagName; //book
echo $rootNode->firstChild->nextSibling->getAttribute('isbn10'); // 1234567890
echo $rootNode->firstChild->nextSibling->firstChild->nextSibling->tagName; //title (obligé de faire un nextSibling pour echapper le noeud texte vide
echo $rootNode->firstChild->nextSibling->firstChild->nextSibling->nodeValue; //Mon titre
echo $rootNode->firstChild->nextSibling->firstChild->nextSibling->nextSibling->nextSibling->tagName; // Author
echo $rootNode->firstChild->nextSibling->firstChild->nextSibling->nextSibling->nextSibling->nodeValue; // Auteur
var_dump($rootNode->firstChild->nextSibling->childNodes->item(1)->nodeValue); //Mon Titre
var_dump($rootNode->firstChild->nextSibling->childNodes->item(3)->nodeValue); //Auteur
}
Pour la lecture
$bookElementNode = $doc->createElement('book'); //crée le
$bookElementNode->setAttribute('isbn10','1111111111'); //crée l'attribut
$bookElementNode1 = $doc->createElement('title'); //crée l'enfant de book
$bookElementNode2 = $doc->createElement('author');
$bookElementNode1->nodeValue = 'bla'; // affecte la valeur au noeud
$bookElementNode2->nodeValue = 'foo';
$bookElementNode->appendChild($bookElementNode1); //ajoute le noeud title au noeud book
$bookElementNode->appendChild($bookElementNode2);
$rootNode->appendChild($bookElementNode); // ajoute le noeud book au noeud library
echo $doc->saveXML();
C’est une syntaxe lourde, pour accéder (ou modifier) aux données, mais elle est compréhensible par tout développeur, elle est implémentée telle que dans beaucoup de langage de programmation.
Les espaces de noms
Les espaces de noms sont utilisé pour pouvoir utiliser des ressources portant des noms similaires dans plusieurs documents.
On va donc déclarer en XML les balises que l’on va utiliser a l’aide d’une URI. Pour faciliter l’utilisation des espaces de noms, on va utiliser un préfixe pour que l’on nomme nos balises comme « prefixe:balise » plutot que « URI:balise ».
<?xml version="1.0" ?> <lib:library xmlns:lib="http://example.com/NS" xmlns:foo="http://foo.bar/FOO"> <lib:book foo:isbn="12345"> <lib:title>...</lib:title> ...
Pour spécifier un espace de nom par défaut, on enlevera le préfixe du xmlns et il suffira de ne pas préfixer la balise
Exemple de code fonctionnel utilisant les espaces de noms en DOM
XPath
XPath est un langage de requête permettant de rechercher dans un fichier XML. Un tutorial complet se trouve ICI. En PHP, il faut travailler avec la classe DOMXPath
SAX
SAX est un analyseur évenementiel de fichier XML. Avec SAX, on ne peut que lire.
SimpleXML
L’objectf de cette API était de simplifier l’API DOM qui ést très/trop verbeuse. Avec SimpleXML, on travail avec des tableaux.
En revanche, si on à simplifié cette API : en gagnant en facilité on a perdu en flexibilité, on ne peut pas acceder aux commentaires par exemple.
- SimpleXMLElement->addAttribute() — Adds an attribute to the SimpleXML element
- SimpleXMLElement->addChild() — Adds a child element to the XML node
- SimpleXMLElement->asXML() — Return a well-formed XML string based on SimpleXML element
- SimpleXMLElement->attributes() — Identifies an element’s attributes
- SimpleXMLElement->children() — Finds children of given node
- SimpleXMLElement->__construct() — Creates a new SimpleXMLElement object
- SimpleXMLElement->getDocNamespaces() — Returns namespaces declared in document
- SimpleXMLElement->getName() — Gets the name of the XML element
- SimpleXMLElement->getNamespaces() — Returns namespaces used in document
- SimpleXMLElement->registerXPathNamespace() — Creates a prefix/ns context for the next XPath query
- SimpleXMLElement->xpath() — Runs XPath query on XML data
- simplexml_import_dom — Get a SimpleXMLElement object from a DOM node.
- simplexml_load_file — Interprets an XML file into an object
- simplexml_load_string — Interprets a string of XML into an object