1

XML:

 <Result xmlns="" xmlns:xsi="" totalResultsAvailable="0" totalResultsReturned="0" schk="true" totalLooseOffers="0" xsi:schemaLocation=""> <details> <ID></ID> <applicationVersion>1.0</applicationVersion> <applicationPath/> <date>2016-05-23T12:17:16.369-03:00</date> <elapsedTime>17</elapsedTime> <status>success</status> <message>success</message> </details> <category id="1"> <thumbnail url="http://image.google.com/test.jpg"/> <links> <link url="www.google.com" type="category"/> <link url="www.google2.com" type="xml"/> </links> <name>Category</name> <filters> <filter id="1" name="Filter1"> <value id="1" value="Test1"/> <value id="2" value="Test2"/> <value id="3" value="Test3"/> </filter> <filter id="2" name="Filter2"> <value id="1" value="Test4"/> <value id="2" value="Test5"/> <value id="3" value="Test6"/> </filter> </filters> </category> </Result> 

PHP:

 $xml = simplexml_load_file("http://xml.com"); foreach($xml->category->filters as $filters){ foreach($filters->children() as $child){ echo $child['value']; } } 

I'm trying to get the filters value, but nothing shows with the code i have. I saw something about xpath but don't know if it's applicable in this situation. Do you have any clue?

--

When the XML looks like this:

<Result xmlns="" xmlns:xsi="" totalResultsAvailable="0" totalResultsReturned="0" schk="true" totalLooseOffers="0" xsi:schemaLocation=""> <details> <ID></ID> <applicationVersion>1.0</applicationVersion> <applicationPath/> <date>2016-05-23T12:17:16.369-03:00</date> <elapsedTime>17</elapsedTime> <status>success</status> <message>success</message> </details> <subCategory id="1"> <thumbnail url="http://image.google.com/test.jpg"/> <name>Subcategory</name> </subCategory> <subCategory id="2"> <thumbnail url="http://image.google.com/test2.jpg"/> <name>Subcategory2</name> </subCategory> </Result> 

Then am able to do this:

foreach($xml->subCategory as $subCategory){ $categoryId = $subCategory['id']; $categoryName = $subCategory->name; } 
4
  • SimpleXML stores the XML root node in the object variable, so $xml in your case refers directly to the <cateogry> node. You should use foreach ($xml->filters as $filters) instead of $xml->category->filters See also stackoverflow.com/questions/17367434/… Commented May 23, 2016 at 20:42
  • Ok @MichaelBerkowski i didn't know about that. My xml starts with: "<Result xmlns="" xmlns:xsi="w3.org/2001/XMLSchema-instance" schk="true" totalLooseOffers="0" xsi:schemaLocation="">". Is that the root? Cause i changed to what you suggested and nothin shows yet Commented May 23, 2016 at 20:50
  • Please edit your post to show the complete XML structure if there are additional parent nodes wrapping <category> because <Result> may indeed be the root. I will reopen the question if the outcome is different. Commented May 23, 2016 at 20:51
  • @MichaelBerkowski i have updated the post. Commented May 23, 2016 at 21:14

1 Answer 1

2

The elements you reference as $child in the inner loop actually point to the <filter> nodes, not the children <value> nodes you are attempting to target attributes for. So this really is just a matter of extending the outer foreach loop to iterate over $xml->category->filters->filter rather than its parent $xml->category->filters.

// Iterate the correct <filter> node, not its parent <filters> foreach ($xml->category->filters->filter as $filter) { foreach($filter->children() as $child){ echo $child['value'] . "\n"; } } 

Here it is in demonstration: https://3v4l.org/Rqc4Y

Using xpath, you can target the inner nodes directly.

$values = $xml->xpath('//category/filters/filter/value'); foreach ($values as $value) { echo $value['value']; } 

https://3v4l.org/vPhKE

Both of these examples output

Test1 Test2 Test3 Test4 Test5 Test6 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! But what if i want to display who is the parent of which element? Like: From the Filter1 we have Test1, 2 and 3..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.