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; }
$xmlin your case refers directly to the<cateogry>node. You should useforeach ($xml->filters as $filters)instead of$xml->category->filtersSee also stackoverflow.com/questions/17367434/…<category>because<Result>may indeed be the root. I will reopen the question if the outcome is different.