这是我要解析的页面 (我给的api链接只是一个开发测试,所以可以公开) http://api.scribd.com/api?method=docs.getList&api_key=2apz5npsqin3cjlbj0s6m
我正在寻找的输出是这样的(现在)
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
我已经尝试了我可以在互联网上找到的所有东西,但没有什么效果好。我有这个脚本
<?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;
}
?>
它的输出如下:
#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 =
我需要重大帮助我真的卡住了,不知道该怎么做。请帮帮我。日Thnx
答案 0 :(得分:0)
我建议您将XML数据加载到新的SimpleXmlElement对象中,因为这将允许您对文档运行xpath查询。
你需要对其工作原理进行一些研究,但这里有一些指示......
像这样执行xpath:
// $xml is a SimpleXMLElement object
$xml = simplexml_load_file('/path/to/file');
$nodes = $xml->xpath('/xpathquery');
单个/表示根节点(在您的情况下为rsp)。双斜杠表示任何匹配的节点。例如// title将返回所有标题。 xpath查询的每个结果都是SimpleXMLElements数组。您可以像这样从中获取数据:
# 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;
结果编号的最后一个例子可能不起作用,但它是沿着这些界限。
答案 1 :(得分:0)
使用“$ x-&gt; childNodes”,您只能获得直接的孩子。您可能想查看php文档: http://php.net/manual/en/class.domdocument.php
答案 2 :(得分:0)
快速而肮脏,应该是这样的方式:
<?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";
}
这将以您请求的格式输出:
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