Posted By

antpaw on 09/30/08


Tagged

php xml


Versions (?)

Who likes this?

12 people have marked this snippet as a favorite

jamesming
carlosabargues
JimiJay
josex
jasonseney
nb109
vali29
netzwerktourist
tapdrup
jackkeller
bobbym245
ringo380


read XML with PHP


 / Published in: PHP
 

URL: http://www.tsql.de/php/php-xml-lesen-simplexml.php

simple :)

  1. //xml.xml :
  2.  
  3.  
  4. <?xml version="1.0" encoding="utf-8" ?>
  5. <Kategorien>
  6. <Kategorie>
  7. <ID>1</ID>
  8. <Name>PHP</Name>
  9. <link>http://www.tsql.de/php.php</link>
  10. </Kategorie>
  11. <Kategorie>
  12. <ID>2</ID>
  13. <Name>SQL</Name>
  14. <link>http://www.tsql.de/transact-sql/howto.htm</link>
  15. </Kategorie>
  16. <Kategorie>
  17. <ID>3</ID>
  18. <Name>CSharp</Name>
  19. <link>http://tsql.de/csharp.htm</link>
  20. </Kategorie>
  21. </Kategorien>
  22.  
  23.  
  24.  
  25.  
  26. //xmlread.php :
  27.  
  28.  
  29. $Kategorien = simplexml_load_file('php-xml-lesen.xml');
  30.  
  31. echo $Kategorien->Kategorie[0]->Name.'<br />';
  32. echo $Kategorien->Kategorie[0]->ID.'<br />';
  33. echo $Kategorien->Kategorie[0]->link.'<br />';

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: koncept on October 5, 2008

Quick way to convert it all into an array...



Posted By: koncept on October 5, 2008

Quick way to convert it all into an array...


// PHP5
function XML2Array(SimpleXMLElement $sxe){
    static $array = array();
    foreach($sxe as $k => $v) {
        if(is_array($v)) {
            recurseXML($sxe[$k]);
        } elseif(is_object($v)) {
            $array[] = (array) $v;
        }
    }
    return $array;
}

print_r(XML2Array(simplexml_load_string($xml)));

You need to login to post a comment.