问题
here is the page i want to parse (the api link i gave is just a dev test so its ok to be public) http://api.scribd.com/api?method=docs.getList&api_key=2apz5npsqin3cjlbj0s6m
the output im looking for is something like this (for now)
Doc_id: 29638658
access_key: key-11fg37gwmer54ssq56l3
secret_password: 1trinfqri6cnv3gf6rnl
title: Sample
description: k
thumbnail_url: http://i6.scribdassets.com/public/images/uploaded/152418747/xTkjCwQaGf_thumbnail.jpeg
page_count: 100
ive tried everything i can find on the internet but nothing works good. i have this one script
<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("http://api.scribd.com/api?method=docs.getList&api_key=2apz5npsqin3cjlbj0s6m");
$x = $xmlDoc->documentElement;
foreach ($x->childNodes AS $item) {
print $item->nodeName . " = " . $item->nodeValue;
}
?>
its output comes out like this:
#text =
resultset =
29638658
key-11fg37gwmer54ssq56l3
1trinfqri6cnv3gf6rnl
Sample
k
http://i6.scribdassets.com/public/images/uploaded/152418747/xTkjCwQaGf_thumbnail.jpeg
DONE
100
29713260
key-18a9xret4jf02129vlw8
25fjsmmvl62l4cbwd1vq
book2
description bla bla
http://i6.scribdassets.com/public/images/uploaded/153065528/oLVqPZMu3zhsOn_thumbnail.jpeg
DONE
7
#text =
i need major help im really stuck and dont know what to do. please please help me. thnx
回答1:
I recommend you load the XML data into a new SimpleXmlElement object as this will allow you to run xpath queries against the document.
You will need to do a little research on how it works, but here's a few pointers...
Execute the xpath like so:
// $xml is a SimpleXMLElement object
$xml = simplexml_load_file('/path/to/file');
$nodes = $xml->xpath('/xpathquery');
A single / represents the root node (in your case rsp). A double slash represents any matching node. For example //title would return all titles. Each result of an xpath query is an array of SimpleXMLElements. You can get data from it like so:
# Untested
$xml = simplexml_load_file('/path/to/file');
$nodes = $xml->xpath('//result');
foreach ($result as $node) {
// Print out the value in title
echo $node->title;
}
// Print out the amount of results
echo $xml->rsp->attributes()->totalResultsAvailable;
The final example with the results numbers may not work, but it is along those lines.
回答2:
With "$x->childNodes" you get only direct childs. You might want to check php documentation: http://php.net/manual/en/class.domdocument.php
And I think that this one could be better:
http://si.php.net/manual/en/book.simplexml.php
回答3:
Quick and dirty, the way things are supposed to be:
<?php
$x = simplexml_load_file('http://api.scribd.com/api?method=docs.getList&api_key=2apz5npsqin3cjlbj0s6m');
foreach($x->resultset->result as $v) {
foreach((array)$v as $kk=>$vv) {
echo $kk.": ".trim($vv)."<br>\n";
}
echo "<br><br>\n";
}
This will output in the format you requested:
doc_id: 29638658
access_key: key-11fg37gwmer54ssq56l3
secret_password: 1trinfqri6cnv3gf6rnl
title: Sample
description: k
thumbnail_url: http://i6.scribdassets.com/public/images/uploaded/152418747/xTkjCwQaGf_thumbnail.jpeg
conversion_status: DONE
page_count: 100
doc_id: 29713260
access_key: key-18a9xret4jf02129vlw8
secret_password: 25fjsmmvl62l4cbwd1vq
title: book2
description: description bla bla
thumbnail_url: http://i6.scribdassets.com/public/images/uploaded/153065528/oLVqPZMu3zhsOn_thumbnail.jpeg
conversion_status: DONE
page_count: 7
来源:https://stackoverflow.com/questions/2615339/how-do-i-parse-an-xml-page-to-output-its-data-pieces-to-the-way-i-want