{"id":807,"date":"2011-02-01T17:30:59","date_gmt":"2011-02-01T16:30:59","guid":{"rendered":"http:\/\/blog.unelink.es\/?p=807"},"modified":"2018-08-06T09:11:04","modified_gmt":"2018-08-06T07:11:04","slug":"como-leer-un-xml-con-php","status":"publish","type":"post","link":"https:\/\/hosting.airetech.es\/blog\/wiki\/php\/como-leer-un-xml-con-php\/","title":{"rendered":"C\u00f3mo leer un XML con PHP"},"content":{"rendered":"<p>Aqu\u00ed tenemos un ejemplo de como leer un archivo XML que Contiene un grupo de personas cada una con su nombre. Su estructura es sencilla:<\/p>\n<ul>\n<li>Personas\n<ul>\n<li>Persona\n<ul>\n<li>Nombre<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>El c\u00f3digo es el siguiente:<\/p>\n<pre>&lt;?php\r\n$xmlstr = &lt;&lt;&lt;XML\r\n&lt;?xml version='1.0' standalone='yes'?&gt;\r\n&lt;personas&gt;\r\n&lt;persona&gt;\r\n&lt;nombre&gt;Adolfo&lt;\/nombre&gt;\r\n&lt;\/persona&gt;\r\n&lt;persona&gt;\r\n&lt;nombre&gt;Laura&lt;\/nombre&gt;\r\n&lt;\/persona&gt;\r\n&lt;persona&gt;\r\n&lt;nombre&gt;Jorge&lt;\/nombre&gt;\r\n&lt;\/persona&gt;\r\n&lt;\/personas&gt;\r\nXML;\r\n\r\n\r\n$xml = new SimpleXMLElement($xmlstr);\r\n\r\nforeach ($xml-&gt;children() as $persona)\r\n{\r\n\u00a0\u00a0\u00a0\u00a0echo \"Persona: \" . $persona-&gt;nombre . \"&lt;br&gt;\";\r\n}\r\n?&gt;<\/pre>\n<p>Como vemos generamos el XML con la clase\u00a0<strong>SimpleXMLElement <\/strong>de PHP. Una vez est\u00e1 creado, utilizaremos la funci\u00f3n <strong>children() <\/strong>en el objeto creado para recoger los elementos, que en este caso recoger\u00e1 los elementos \"persona\" y luego accedemos al nombre con \"-&gt;\" y el nombre del elemento que queremos mostrar.<\/p>\n<p>M\u00e1s informaci\u00f3n de <strong>SimpleXMLElement: <\/strong><a title=\"Clase SimpleXMLElement\" href=\"http:\/\/php.net\/manual\/es\/class.simplexmlelement.php\" target=\"_blank\">https:\/\/es1.php.net\/manual\/es\/class.simplexmlelement.php<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"Aqu\u00ed tenemos un ejemplo de como leer un archivo XML que Contiene un grupo de personas cada una con su nombre. Su estructura es sencilla: Personas Persona Nombre El c\u00f3digo es el siguiente: &lt;?php $xmlstr = &lt;&lt;&lt;XML &lt;?xml version='1.0' standalone='yes'?&gt; &lt;personas&gt; &lt;persona&gt; &lt;nombre&gt;Adolfo&lt;\/nombre&gt; &lt;\/persona&gt; &lt;persona&gt; &lt;nombre&gt;Laura&lt;\/nombre&gt; &lt;\/persona&gt; &lt;persona&gt; &lt;nombre&gt;Jorge&lt;\/nombre&gt; &lt;\/persona&gt; &lt;\/personas&gt; XML; $xml = new SimpleXMLElement($xmlstr); foreach ($xml-&gt;children() as $persona)&nbsp;<a href=\"https:\/\/hosting.airetech.es\/blog\/wiki\/php\/como-leer-un-xml-con-php\/\" class=\"read-more\">Seguir leyendo<\/a>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[39],"tags":[80,79,128],"class_list":["post-807","post","type-post","status-publish","format-standard","hentry","category-php","tag-leer-xml-en-php","tag-simplexmlelement","tag-xml","cat-39-id"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/hosting.airetech.es\/blog\/wp-json\/wp\/v2\/posts\/807","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hosting.airetech.es\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hosting.airetech.es\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hosting.airetech.es\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hosting.airetech.es\/blog\/wp-json\/wp\/v2\/comments?post=807"}],"version-history":[{"count":9,"href":"https:\/\/hosting.airetech.es\/blog\/wp-json\/wp\/v2\/posts\/807\/revisions"}],"predecessor-version":[{"id":7054,"href":"https:\/\/hosting.airetech.es\/blog\/wp-json\/wp\/v2\/posts\/807\/revisions\/7054"}],"wp:attachment":[{"href":"https:\/\/hosting.airetech.es\/blog\/wp-json\/wp\/v2\/media?parent=807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hosting.airetech.es\/blog\/wp-json\/wp\/v2\/categories?post=807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hosting.airetech.es\/blog\/wp-json\/wp\/v2\/tags?post=807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}