tags:

views:

45

answers:

3

I have the following XML data which is generated by a webservice

<?xml version="1.0" encoding="UTF-8"?> 
<rsp xmlns="http://worldcat.org/xid/isbn/" stat="ok">
      <isbn   oclcnum="263710087 491996179 50279560 60857040 429386124 44597307" lccn="00131084" form="AA BC" year="2002" lang="eng" ed="1st American ed." title="Harry Potter and the goblet of fire"  author="J.K. Rowling."  publisher="Scholastic Inc."  city="New York [u.a.]"    url="http://www.worldcat.org/oclc/263710087?referer=xid"&gt;9780439139601&lt;/isbn&gt;

</rsp>

I need to read the data in the 'isbn' tag, more specifically, the value of the property 'title'. How would I do this in PHP.

Thanks

A: 

Using SimpleXML, or DOM. Here are some usage examples : http://www.php.net/manual/en/simplexml.examples-basic.php

greg0ire
Preferably SimpleXML
Steven1350
+1  A: 

I have solved this myself.

$xmldata= file_get_contents("http://xisbn.worldcat.org/webservices/xid/isbn/9780439139601?method=getMetadata&amp;format=xml&amp;fl=*");
$xml= new SimpleXMLElement($xmldata);
print $xml->isbn[0]['title'];
Steven1350
this depends on the number of isbn tags you have in your xml .. $xml->isbn[0]['title']; will only get you title for first isbn tag element.I think you should use something like Gordon suggested.
youssef azari
+3  A: 

With DOM

$dom = new DOMDocument;
$dom->load('books.xml'); // or from URL    
foreach($dom->getElementsByTagName('isbn') as $node) {
    echo $node->getAttribute('title');
}

With SimpleXml:

$sxe = simplexml_load_file('filename.xml'); // or from URL
foreach($sxe->isbn as $node) {
    echo $node['title'];
}

Just my 2c why you want to use DOM: SimpleXML appears simple indeed, but simplicity in this case means lack of control. DOM isn't much harder to use and can do more. DOM is an Interface Standard defined by the W3C and can be found implemented in many languages, so it pays to know the API. True, it might be a bit more verbose than SimpleXML but it's also ultimately more powerful. If you have worked with DOM for some time, you don't want to go back.

Gordon