I’m trying to wrap my head around PHP and XML.
I’m trying to do something:
There is an XML document that I’m retrieving via cURL (also tried various PHP XML library parameters such as XMLReader::open($url) etc. The method of retrieval doesn’t matter; I can and have got this part working.
The problem is parsing the XML on the retrieved page.
Here is an example of the XML:
What I need to get from that page is the call number;
<datafield tag="060" ind1=" " ind2=" ">
<subfield code="a">WM 173.6 R823m</subfield>
</datafield>
author;
<datafield tag="100" ind1="1" ind2=" ">
<subfield code="a">Ross, Colin A.</subfield>
</datafield>
and title information;
<datafield tag="245" ind1="1" ind2="0">
<subfield code="a">Multiple personality disorder :</subfield>
<subfield code="b">diagnosis, clinical features, and treatment /</subfield>
<subfield code="c">Colin A. Ross.</subfield>
</datafield>
seems simple enough. However, for the life of me I can not seem to get any of the inbuilt PHP functions for working with XML to work (because I’m doing it wrong).
Here is an example I’ve tried:
//xml file retrieved via curl and saved to folder
$file="9780471615156.xml";
$xml = simplexml_load_file($file);
echo $xml->getName();//returns searchRetrieveResponse
foreach($xml->searchRetrieveResponse[0]->attributes() as $a => $b){
echo $a,'="',$b,"\"</br>";//nothing
}
foreach ($xml->searchRetrieveResponse[0]->children() as $child){
echo "Child node: " . $child . "<br />";//nothing
}
it returns the name of the first node, but I can’t get it to go any deeper.
NB: I’m running PHP 5+
as far as I tried the simpleXML can not read this XML. Try the example below, it will list an array which you can easily loop trought and find what you need simply by comparing keys/values you’re looking for.