Skip to content

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 <title>
    $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();
    </pre>
    <p>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.</p>
    <h3>Les espaces de noms</h3>
    <p><a title="Les espaces de noms" href="http://yrweb.fr/programmer-en-php5/les-espaces-de-noms">Les espaces de noms</a> sont utilisé pour pouvoir utiliser des ressources portant des noms similaires dans plusieurs documents.<br/><br />
    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 ».</p>
    <pre name="code" class="xml">
    <?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>
    			...
    </pre>
    <p>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</p>
    <p><a href="http://swift.servebeer.com/blog/wp-content/uploads/2008/05/xml.zip">Exemple de code fonctionnel utilisant les espaces de noms en DOM</a></p>
    <h3>XPath</h3>
    <p>XPath est un langage de requête permettant de rechercher dans un fichier XML. Un tutorial complet se trouve <a href="http://www.zvon.org/xxl/XPathTutorial/General_fre/examples.html">ICI</a>. En PHP, il faut travailler avec la classe <a href="http://fr3.php.net/manual/en/class.domxpath.php">DOMXPath</a></p>
    <h3>SAX</h3>
    <p>SAX est un analyseur évenementiel de fichier XML. Avec SAX, on ne peut que lire.</p>
    <h3>SimpleXML</h3>
    <p>L’objectf de cette API était de simplifier l’API DOM qui ést très/trop verbeuse. Avec SimpleXML, on travail avec des tableaux.<br/><br />
    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. </p>
    <ul class="chunklist chunklist_reference">
    <li><a href="http://fr3.php.net/manual/en/function.simplexml-element-addAttribute.php">SimpleXMLElement->addAttribute()</a> — Adds an attribute to the SimpleXML element</li>
    <li><a href="http://fr3.php.net/manual/en/function.simplexml-element-addChild.php">SimpleXMLElement->addChild()</a> — Adds a child element to the XML node</li>
    <li><a href="http://fr3.php.net/manual/en/function.simplexml-element-asXML.php">SimpleXMLElement->asXML()</a> — Return a well-formed XML string based on SimpleXML element</li>
    <li><a href="http://fr3.php.net/manual/en/function.simplexml-element-attributes.php">SimpleXMLElement->attributes()</a> — Identifies an element’s attributes</li>
    <li><a href="http://fr3.php.net/manual/en/function.simplexml-element-children.php">SimpleXMLElement->children()</a> — Finds children of given node</li>
    <li><a href="http://fr3.php.net/manual/en/function.simplexml-element-construct.php">SimpleXMLElement->__construct()</a> — Creates a new SimpleXMLElement object</li>
    <li><a href="http://fr3.php.net/manual/en/function.simplexml-element-getDocNamespaces.php">SimpleXMLElement->getDocNamespaces()</a> — Returns namespaces declared in document</li>
    <li><a href="http://fr3.php.net/manual/en/function.simplexml-element-getName.php">SimpleXMLElement->getName()</a> — Gets the name of the XML element</li>
    <li><a href="http://fr3.php.net/manual/en/function.simplexml-element-getNamespaces.php">SimpleXMLElement->getNamespaces()</a> — Returns namespaces used in document</li>
    <li><a href="http://fr3.php.net/manual/en/function.simplexml-element-registerXPathNamespace.php">SimpleXMLElement->registerXPathNamespace()</a> — Creates a prefix/ns context for the next XPath query</li>
    <li><a href="http://fr3.php.net/manual/en/function.simplexml-element-xpath.php">SimpleXMLElement->xpath()</a> — Runs XPath query on XML data</li>
    <li><a href="http://fr3.php.net/manual/en/function.simplexml-import-dom.php">simplexml_import_dom</a> — Get a SimpleXMLElement object from a DOM node.</li>
    <li><a href="http://fr3.php.net/manual/en/function.simplexml-load-file.php">simplexml_load_file</a> — Interprets an XML file into an object</li>
    <li><a href="http://fr3.php.net/manual/en/function.simplexml-load-string.php">simplexml_load_string</a> — Interprets a string of XML into an object</li>
    </ul>
    <!-- google_ad_section_end --><div class="addthis_toolbox addthis_default_style " addthis:url='http://yrweb.fr/programmer-en-php5/xml' addthis:title='XML '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div></div>
    
    
    		</div>
    	</div>
    </div>
    
    
    <div id="sidebar">
    	<div class="pad">
    
    		<h2>Sidebar</h2>
    
    	<div id="text-6" class="box widget_text"><div class="title"><h3>Annonceurs</h3></div><div class="interior">			<div class="textwidget"><script type="text/javascript"><!--
    google_ad_client = "pub-3255672094343890";
    /* 250x250, date de création 09/11/09 */
    google_ad_slot = "8783606043";
    google_ad_width = 250;
    google_ad_height = 250;
    //-->
    </script>
    <script type="text/javascript"
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script></div>
    		</div></div><div id="tag_cloud-3" class="box widget_tag_cloud"><div class="title"><h3>Tags</h3></div><div class="interior"><div class="tagcloud"><a href='http://yrweb.fr/tag/anonymat' class='tag-link-68' title='2 sujets' style='font-size: 13.25pt;'>anonymat</a>
    <a href='http://yrweb.fr/tag/array' class='tag-link-15' title='1 sujet' style='font-size: 8pt;'>array</a>
    <a href='http://yrweb.fr/tag/configuration' class='tag-link-61' title='1 sujet' style='font-size: 8pt;'>configuration</a>
    <a href='http://yrweb.fr/tag/dns' class='tag-link-56' title='1 sujet' style='font-size: 8pt;'>dns</a>
    <a href='http://yrweb.fr/tag/fonction' class='tag-link-7' title='3 sujets' style='font-size: 16.75pt;'>fonction</a>
    <a href='http://yrweb.fr/tag/google' class='tag-link-65' title='1 sujet' style='font-size: 8pt;'>Google</a>
    <a href='http://yrweb.fr/tag/geolocalisation' class='tag-link-63' title='1 sujet' style='font-size: 8pt;'>géolocalisation</a>
    <a href='http://yrweb.fr/tag/hardware' class='tag-link-66' title='1 sujet' style='font-size: 8pt;'>hardware</a>
    <a href='http://yrweb.fr/tag/htaccess' class='tag-link-60' title='1 sujet' style='font-size: 8pt;'>htaccess</a>
    <a href='http://yrweb.fr/tag/httpd' class='tag-link-59' title='1 sujet' style='font-size: 8pt;'>httpd</a>
    <a href='http://yrweb.fr/tag/ie-sucks' class='tag-link-58' title='4 sujets' style='font-size: 19.6666666667pt;'>ie sucks</a>
    <a href='http://yrweb.fr/tag/javascript' class='tag-link-5' title='2 sujets' style='font-size: 13.25pt;'>Javascript</a>
    <a href='http://yrweb.fr/tag/linux' class='tag-link-62' title='2 sujets' style='font-size: 13.25pt;'>linux</a>
    <a href='http://yrweb.fr/tag/mootools' class='tag-link-6' title='1 sujet' style='font-size: 8pt;'>mootools</a>
    <a href='http://yrweb.fr/tag/mozilla' class='tag-link-73' title='1 sujet' style='font-size: 8pt;'>mozilla</a>
    <a href='http://yrweb.fr/tag/mysql' class='tag-link-4' title='1 sujet' style='font-size: 8pt;'>MySQL</a>
    <a href='http://yrweb.fr/tag/outils' class='tag-link-71' title='2 sujets' style='font-size: 13.25pt;'>outils</a>
    <a href='http://yrweb.fr/tag/php' class='tag-link-3' title='4 sujets' style='font-size: 19.6666666667pt;'>PHP</a>
    <a href='http://yrweb.fr/tag/securite' class='tag-link-67' title='5 sujets' style='font-size: 22pt;'>securité</a>
    <a href='http://yrweb.fr/tag/serveur-web' class='tag-link-45' title='2 sujets' style='font-size: 13.25pt;'>serveur web</a>
    <a href='http://yrweb.fr/tag/sortby' class='tag-link-57' title='1 sujet' style='font-size: 8pt;'>sortby</a>
    <a href='http://yrweb.fr/tag/spam' class='tag-link-69' title='1 sujet' style='font-size: 8pt;'>spam</a>
    <a href='http://yrweb.fr/tag/twitter' class='tag-link-76' title='1 sujet' style='font-size: 8pt;'>Twitter</a>
    <a href='http://yrweb.fr/tag/zend-framework' class='tag-link-75' title='1 sujet' style='font-size: 8pt;'>Zend Framework</a></div>
    </div></div>PNG
    
    ���
    IHDR���0���0���W���bKGD������	oFFs���(�����11���	pHYs���H���H�Fk>���	vpAg������0�$"��
    IDAThkř^Ձ8da"`#*hEJL2d7U떕S*nU*_fMUtݬQY% 
    "
    20g\o~3K4M%ί\~/
    *TPB
    *?W`FBAmTO@y X4�@/}Ҡ>6pA tcLhP8Ic7Ƅ?0Ƭ4<S
    w1oJf=l)d2?;=Ŝ\mp2 gcy<ѵVGB'$ppS9{ʥBYBԔ
    `|xxU!"V	x]fdI>=}ϑ>׏?roL=0x7ř`({g�`
    8	_iZ#Ve'oK&Μ#k!_,esH\6_kK٬T*AkC߯\wk~"}[=61e
    5e/\j|puA!OX9b<{oct-k_ڴzz7{9m@AmkKP@5h 
    / 0'Z/8X=K)y>#|<o־_uWpbQec˚J3H;39,p|쮽G~4VI
    -m7'-0%hcn"^4Z1;2]/bs%
    n,*mi<!e"?:V"ץ5QL]:snI/(yߵƕ?<[,N'Drn"~RZO- :=<扻!|KرV,E<=XM
    D"4xw&3Yl`}4_8Jix}S}$lGў/ruorUEIkb(Tv5f%�Xg7n9,@ˆo^YiQ?@ JJ0S,hr]ٿc'_qqdX/ؾfz`q{H)Ʊwlϛ
    ElcԶqgxmp 3y?l'nZC;vOl,K	-,@Y|Whi)L(PR<v==7|,'&HONQ<q
    )\\zUeʵJb
    %|wXq!_$=ѿ|xg}^?)ֆbE\hRH>FCu]+h&WNv;,d)f2xRbs9JMq1
    R!B.MK;),eQf_!.lwQc'-ٸNY3J	i&%05Rblۿ6x
    KT2r&U
    Փ%=6HUw_`rd:{`Ty
    oldءI'}yhZP2/m~ᚵ/}1UE{ub	!T_K뺵)+&01)݁<_4^E|wawɥR)bwa=z_xG}B^;m7\~-
    )E02o_x3&	H8*Bb#pc1mBVB8@m<ݾ8ш$B"=X4BmkGN8p5]v-I\!PY"p4
    yc;B
    G*$UױN$p$,ai*dsɦƩ]QT\v
    71nO
    ,)ϵ\UզY(8p\XnJU55.(J1X˾|	TŎh2q+9
    47KB_D6tlucX~$SGsC'Ҕ283{w#pYv(xs]w"B!R"D$��J[tm%`]wvӍiەDkđ[JϴJKֻ3[B["L@X7�H)`.fβ.RGjKx.?m\MOhiǢ4[u;D/?=s~m8oHmMۺ5z =}e6ӱmKj$|UÁZ3QHwzN&-MAmn!w3
    gr8yg�naC_`e![=;(sRy,k[Um8)-빫
    DZ]e75F1._(~';0oi>Iyrcv�7*"3QU	ҫ}>yLz(wy?!4WVjCM^\U
    *TPŸ^		S���%tEXtdate:create�2012-01-29T20:29:13+00:002ī���%tEXtdate:modify�2012-01-29T20:29:13+00:00C|���tEXtpng:bit-depth-written�,���tEXtSoftware�Adobe ImageReadyqe<����IENDB`<li id="latest-twitter-widget"><h2 id="latest-twitter-widget-title">Twitter</h2><a href="http://twitter.com/swiftycore"><img id="latest-twitter-widget-pic" src="http://yrweb.fr/wp-content/plugins/latest-twitter-sidebar-widget/swiftycore.com/profile_images/1790441613/jtBJzsp8_normal" alt=""></a><div class="latest-twitter-tweet">"Sympa le remake de braquage à l'italienne! <a href="http://twitter.com/#!/search?q=%23Sarkozy">#Sarkozy</a> braque ma télé <a href="http://twitter.com/#!/search?q=%23tf1">#tf1</a> http://t.co/BFlGVTHX"</div><div class="latest-twitter-tweet-time" id="latest-twitter-tweet-time-1">2 days ago</div><div class="latest-twitter-tweet">""<a href="http://twitter.com/YourAnonNews">@YourAnonNews</a>: BREAKING: French authorities make IRC.LC shut down its iframe pointing to <a href="http://twitter.com/#!/search?q=%23Anonymous">#Anonymous</a> webchat <a href="http://t.co/Cn6cWADm">http://t.co/Cn6cWADm</a> <a href="http://twitter.com/joepie91">@joepie91</a>""</div><div class="latest-twitter-tweet-time" id="latest-twitter-tweet-time-2">5 days ago</div><div class="latest-twitter-tweet">"~99% ;) “<a href="http://twitter.com/telecomix">@telecomix</a>: MEANWHILE IN POLISH PARLIAMENT: <a href="http://t.co/VMsaHmG0">http://t.co/VMsaHmG0</a> <a href="http://twitter.com/#!/search?q=%23ACTA">#ACTA</a> /this is NOT a fake!!”"</div><div class="latest-twitter-tweet-time" id="latest-twitter-tweet-time-3">5 days ago</div><div class="latest-twitter-tweet">"Se déclarer Anonymous et ne pas savoir ce qu'est IRC... Tssss, bande de zombies :) <a href="http://twitter.com/#!/search?q=%23expectPasGrandMonde">#expectPasGrandMonde</a>"</div><div class="latest-twitter-tweet-time" id="latest-twitter-tweet-time-4">8 days ago</div><div class="latest-twitter-tweet">"Ca marche bien ! <a href="http://twitter.com/#!/search?q=%23freemobile">#freemobile</a> http://t.co/NpUpD57R"</div><div class="latest-twitter-tweet-time" id="latest-twitter-tweet-time-5">10 days ago</div><div id="latest-twitter-follow-link"><a href="http://twitter.com/swiftycore">follow @swiftycore on twitter</a></div></li><div id="recent-comments-3" class="box widget_recent_comments"><div class="title"><h3>Commentaires récents</h3></div><div class="interior"><ul id="recentcomments"><li class="recentcomments">Swifty_core dans <a href="http://yrweb.fr/462-faites-confiance-a-varnish-et-votre-site-vous-dira-merci.htm/comment-page-1#comment-194">Faites confiance à Varnish, et votre site vous dira merci</a></li><li class="recentcomments"><a href='http://dimension-photographique.fr' rel='external nofollow' class='url'>Humanbomb</a> dans <a href="http://yrweb.fr/462-faites-confiance-a-varnish-et-votre-site-vous-dira-merci.htm/comment-page-1#comment-191">Faites confiance à Varnish, et votre site vous dira merci</a></li><li class="recentcomments">Swifty_core dans <a href="http://yrweb.fr/107-explode-en-mysql.htm/comment-page-1#comment-161">Explode en MySQL</a></li><li class="recentcomments"><a href='http://squallynou.blogspot.com' rel='external nofollow' class='url'>Squallynou</a> dans <a href="http://yrweb.fr/107-explode-en-mysql.htm/comment-page-1#comment-160">Explode en MySQL</a></li><li class="recentcomments"><a href='http://www.ados-mania.com' rel='external nofollow' class='url'>Mickey</a> dans <a href="http://yrweb.fr/274-le-retour-en-force-des-injections-xss-grace-a-hadopi.htm/comment-page-1#comment-149">Le retour en force des injections XSS, grâce à #Hadopi</a></li></ul></div></div>
    	</div>
    </div>
    
    
    
    <div class="clear"></div>
    
    
    		</div>
    	</div>
    	
    	<div id="footer">
    		<div class="pad append-clear wrap-inner-1">
    			<p class="copyright">
    				Copyright © 2012. YrWeb.fr. All rights reserved. 
    			</p>
    					</div>
    	</div>
    
    </div>
    
    <!-- AddThis Button Begin -->
    <script type="text/javascript">var addthis_product = 'wpp-262';
    var addthis_config = {"data_track_clickback":true,"data_track_addressbar":false};if (typeof(addthis_share) == "undefined"){ addthis_share = [];}</script><script type="text/javascript" src="//s7.addthis.com/js/250/addthis_widget.js#pubid=ra-4eb58f463599c213"></script><script type="text/javascript">
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
    </script>
    <script type="text/javascript">
    try {
    var pageTracker = _gat._getTracker("UA-9038961-2");
    pageTracker._setAllowAnchor(true);
    pageTracker._trackPageview();
    } catch(err) {}</script>
    	    <!-- WP-Cufon Plugin (ie6/7 compatibility tag)  -->    
            
    <script type='text/javascript'>
    /* <![CDATA[ */
    var thickboxL10n = {"next":"Next >","prev":"< Prev","image":"Image","of":"of","close":"Close","noiframes":"This feature requires inline frames. You have iframes disabled or your browser does not support them.","loadingAnimation":"http:\/\/yrweb.fr\/wp-includes\/js\/thickbox\/loadingAnimation.gif","closeImage":"http:\/\/yrweb.fr\/wp-includes\/js\/thickbox\/tb-close.png"};
    /* ]]> */
    </script>
    <script type='text/javascript' src='http://yrweb.fr/wp-includes/js/thickbox/thickbox.js?ver=3.1-20111117'></script>
    <script type='text/javascript' src='http://yrweb.fr/wp-content/plugins/contact-form-7/jquery.form.js?ver=2.52'></script>
    <script type='text/javascript' src='http://yrweb.fr/wp-content/plugins/contact-form-7/scripts.js?ver=3.0.2.1'></script>
    </body>
    </html>