Reputation: 847
Using the code here: http://www.w3schools.com/php/php_xml_parser_expat.asp
How would you get the attributes using the switch (if possible) for each one if the XML file was like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to id="01">Tove</to>
<from id="02">Jani</from>
<heading color="#CEAF33">Reminder</heading>
<body type="small" important="low">Don't forget me this weekend!</body>
</note>
Upvotes: 0
Views: 215
Reputation: 59699
I wouldn't use W3's tutorial. I'd use simplexml_load_string to load your string into an XML object, then iterate over it like so:
$notes = simplexml_load_string( $xml);
foreach( $notes as $note)
{
echo $note . "[" . $note->getName() . "]\n";
foreach( $note->attributes() as $key => $value)
{
echo "\t" . $key . '=' . $value . "\n";
}
echo "\n";
}
Example (uses your input string)
Upvotes: 1
Reputation: 751
You should use the SimpleXML extension, it is much simpler and easier to work with.
From the PHP docs:
<?php
$string = <<<XML
<a>
<foo name="one" game="lonely">1</foo>
</a>
XML;
$xml = simplexml_load_string($string);
foreach($xml->foo[0]->attributes() as $a => $b) {
echo $a,'="',$b,"\"\n";
}
?>
See https://www.php.net/manual/en/book.simplexml.php for more information.
Upvotes: 1