{"id":105,"date":"2009-09-24T17:47:24","date_gmt":"2009-09-24T17:47:24","guid":{"rendered":"http:\/\/www.softov.com.br\/blog\/?p=105"},"modified":"2019-02-02T01:46:36","modified_gmt":"2019-02-02T01:46:36","slug":"php-lendo-e-tratando-informacoes-vindas-de-um-arquivo-xml","status":"publish","type":"post","link":"https:\/\/softov.com.br\/blog\/2009\/09\/24\/php-lendo-e-tratando-informacoes-vindas-de-um-arquivo-xml\/","title":{"rendered":"PHP: lendo e tratando informa\u00e7\u00f5es vindas de um arquivo XML"},"content":{"rendered":"<p>N\u00e3o sou muito ativo no php, sou mais um web-designer, mas como sou um desenvolvedor web e trabalho sozinho nesta \u00e1rea, tenho que me virar procurando certos problemas na web (google).<\/p>\n<p>Estes dias estava com dificuldade em ler um XML, de tanto procurar na web encontrei este blog:<\/p>\n<p>http:\/\/www.vedovelli.com.br\/?p=83<\/p>\n<p>Resolvi compartilhar para ajudar quem tem ou ter\u00e1 o mesmo problema que eu tive. Modifiquei certas coisas, comentei e postei aqui, espero que tenham facilidade em entender o Post!, qualquer duvida comentem.<\/p>\n<p>[code language=&#8221;xml&#8221;]<br \/>\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br \/>\n&lt;root&gt;<br \/>\n&lt;bandas&gt;<br \/>\n&lt;nome&gt;Asian Dub Foundation&lt;\/nome&gt;<br \/>\n&lt;nome&gt;REM&lt;\/nome&gt;<br \/>\n&lt;nome&gt;Everything But The Girl&lt;\/nome&gt;<br \/>\n&lt;\/bandas&gt;<br \/>\n&lt;\/root&gt;<br \/>\n[\/code]<\/p>\n<p>O xml \u00e9 o mesmo do exemplo do blog que eu copiei.<\/p>\n<p>Agora temos o php que ler\u00e1 o XML acima<\/p>\n<p>[code language=&#8221;php&#8221;]<\/p>\n<p>&lt;?php<br \/>\n$xml = simplexml_load_file(&quot;arquivo.xml&quot;); \/\/ Pode ser http:\/\/www.algumdominio.com\/services\/arquivo.xml<\/p>\n<p>for($i=0; $i &lt; count($xml-&gt;bandas-&gt;nome); $i++) {<br \/>\n echo $xml-&gt;bandas-&gt;nome[$i].&quot;&lt;br \/&gt;&quot;;<br \/>\n}<br \/>\n?&gt;<\/p>\n<p>[\/code]<\/p>\n<p>Para outro exemplo tenho o XML abaixo<\/p>\n<p>[code language=&#8221;xml&#8221;]<\/p>\n<p>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br \/>\n&lt;root&gt;<br \/>\n&lt;bandas&gt;<br \/>\nGringas<br \/>\n&lt;nome&gt;Asian Dub Foundation&lt;\/nome&gt;<br \/>\n&lt;nome&gt;REM&lt;\/nome&gt;<br \/>\n&lt;nome&gt;Everything But The Girl&lt;\/nome&gt;<br \/>\n&lt;\/bandas&gt;<\/p>\n<p>&lt;bandas&gt;<br \/>\nDa nossa Terra<br \/>\n&lt;nome&gt;Detonautas&lt;\/nome&gt;<br \/>\n&lt;nome&gt;CPM 22&lt;\/nome&gt;<br \/>\n&lt;nome&gt;Skank&lt;\/nome&gt;<br \/>\n&lt;\/bandas&gt;<br \/>\n&lt;\/root&gt;<\/p>\n<p>[\/code]<\/p>\n<p>E o php que faz a leitura<\/p>\n<p>[code language=&#8221;php&#8221;]<\/p>\n<p>&lt;?php<br \/>\n\/\/ l\u00ea o arquivo xml<br \/>\n$xml = simplexml_load_file(&quot;arquivo2.xml&quot;);<\/p>\n<p>\/\/ le todos os filhos pricipais &#8211; no caso &quot;bandas&quot;<br \/>\nforeach($xml-&gt;children() as $child){<br \/>\n \/\/ imprime o texto interno<br \/>\n echo &quot;&lt;br \/&gt;&lt;br \/&gt;&lt;strong&gt;&quot;.$child.&quot;:&lt;\/strong&gt; &lt;br \/&gt;&quot;;<\/p>\n<p> \/\/ le todos os of filhos dos filhos pricipais &#8211; no caso &quot;nome&quot;<br \/>\n foreach($child as $asd){<br \/>\n \/\/ imprime o texto interno<br \/>\n echo &quot;&quot; . $asd . &quot;&lt;br \/&gt;&quot;;<br \/>\n }<br \/>\n}<\/p>\n<p>\/\/ foreach &#8211; enquanto tem dados ele l\u00ea<br \/>\n\/\/ pelo menos \u00e9 isso que eu entendo<br \/>\n?&gt;<\/p>\n<p>[\/code]<\/p>\n<p>Neste segundo exemplo eu leio o XML, e para cada filho leio seus filhos. Assim tenho o filho &#8220;bandas&#8221; e seus filhos &#8220;nome&#8221;.<\/p>\n<p><a href=\"http:\/\/www.softov.com.br\/blogmodelos\/phpxml1\/exemplo1.php\" target=\"_blank\">Os exemplos podem ser vistos aqui<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>N\u00e3o sou muito ativo no php, sou mais um web-designer, mas como sou um desenvolvedor web e trabalho sozinho nesta \u00e1rea, tenho que me virar procurando certos problemas na web (google). Estes dias estava com dificuldade em ler um XML, de tanto procurar na web encontrei este blog: http:\/\/www.vedovelli.com.br\/?p=83 Resolvi compartilhar para ajudar quem tem&hellip; <a class=\"more-link\" href=\"https:\/\/softov.com.br\/blog\/2009\/09\/24\/php-lendo-e-tratando-informacoes-vindas-de-um-arquivo-xml\/\">Continuar lendo <span class=\"screen-reader-text\">PHP: lendo e tratando informa\u00e7\u00f5es vindas de um arquivo XML<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,1,24],"tags":[106,91],"_links":{"self":[{"href":"https:\/\/softov.com.br\/blog\/wp-json\/wp\/v2\/posts\/105"}],"collection":[{"href":"https:\/\/softov.com.br\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/softov.com.br\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/softov.com.br\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/softov.com.br\/blog\/wp-json\/wp\/v2\/comments?post=105"}],"version-history":[{"count":1,"href":"https:\/\/softov.com.br\/blog\/wp-json\/wp\/v2\/posts\/105\/revisions"}],"predecessor-version":[{"id":4676,"href":"https:\/\/softov.com.br\/blog\/wp-json\/wp\/v2\/posts\/105\/revisions\/4676"}],"wp:attachment":[{"href":"https:\/\/softov.com.br\/blog\/wp-json\/wp\/v2\/media?parent=105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/softov.com.br\/blog\/wp-json\/wp\/v2\/categories?post=105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/softov.com.br\/blog\/wp-json\/wp\/v2\/tags?post=105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}