I'm trying to make this loop, but it is only returning the first values.
XML
<catalog>
<dd>
<categoryassignment categoryid="3d-magic" productid="000000000000109330_PURPLE/ BLUE"/>
<categoryassignment categoryid="3d-magic" productid="000000000000109330_PURPLE/ BLUE/ ORANGE"/>
<categoryassignment categoryid="3d-magic" productid="000000000000109330_RED/ GREEN/ YELLOW"/>
<categoryassignment categoryid="3d-magic" productid="000000000000109335_BLUE/YELLOW"/>
<categoryassignment categoryid="3d-magic" productid="000000000000109335_PURPLE/ORANGE"/>
<categoryassignment categoryid="3d-magic" productid="000000000000115881_GREEN/PURPLE"/>
<categoryassignment categoryid="3d-magic" productid="000000000000115881_ORANGE/BLUE"/>
<categoryassignment categoryid="3d-magic" productid="000000000000115881_RED/YELLOW"/>
<categoryassignment categoryid="3d-magic" productid="000000000109329001"/>
<categoryassignment categoryid="3d-magic" productid="000000000109329002"/>
<categoryassignment categoryid="3d-magic" productid="000000000109335001"/>
<categoryassignment categoryid="3d-magic" productid="000000000111568001"/>
<categoryassignment categoryid="3d-magic" productid="000000000111568002"/>
</dd>
PHP
$xml=simplexml_load_file("store.xml") or die("Failed to create an object");
foreach($xml->children() as $books) {
echo $books->categoryassignment['categoryid'];echo $books->categoryassignment['productid'];echo "<br>";}
From the example here: https://www.w3schools.com/php/php_xml_simplexml_get.asp
You'll need to change how you are referencing the data. If you first do a quick debug by doing:
print_r($xml);
You will see that you have an object like this:
SimpleXMLElement Object
(
[dd] => SimpleXMLElement Object
(
[categoryassignment] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[categoryid] => 3d-magic
[productid] => 000000000000109330_PURPLE/ BLUE
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[categoryid] => 3d-magic
[productid] => 000000000000109330_PURPLE/ BLUE/ ORANGE
)
)
... etc ...
With this knowledge, you can then easily see where you need to put your foreach
. It would be like this (with a slight change to $books
inside):
foreach($xml->dd->categoryassignment as $books) {
echo $books['categoryid'];
echo ' ';
echo $books['productid'];
echo "<br>\n";
}
And that results as:
3d-magic 000000000000109330_PURPLE/ BLUE 3d-magic 000000000000109330_PURPLE/ BLUE/ ORANGE 3d-magic 000000000000109330_RED/ GREEN/ YELLOW 3d-magic 000000000000109335_BLUE/YELLOW 3d-magic 000000000000109335_PURPLE/ORANGE 3d-magic 000000000000115881_GREEN/PURPLE 3d-magic 000000000000115881_ORANGE/BLUE 3d-magic 000000000000115881_RED/YELLOW 3d-magic 000000000109329001 3d-magic 000000000109329002 3d-magic 000000000109335001 3d-magic 000000000111568001 3d-magic 000000000111568002
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.