0
我之前使用过xpath来处理XML元素,但是我正努力为这个特定的XML获取正确的语法。使用PHP SimpleXml解析XML文档疑难解答
我试图解析监护人API响应。样本响应:
<response user-tier="approved" current-page="1" start-index="1" page-size="10" pages="1" total="10" status="ok">
<results>
<tag type="series" web-title="Cycling" section-name="Life and style" id="lifeandstyle/series/cycling" api- url="http://content.guardianapis.com/lifeandstyle/series/cycling" section-id="lifeandstyle" web- url="http://www.guardian.co.uk/lifeandstyle/series/cycling"/>
<tag type="keyword" web-title="Cycling" section-name="Sport" id="sport/cycling" api- url="http://content.guardianapis.com/sport/cycling" section-id="sport" web- url="http://www.guardian.co.uk/sport/cycling"/>
<tag type="keyword" web-title="Cycling" section-name="Life and style" id="lifeandstyle/cycling" api-url="http://content.guardianapis.com/lifeandstyle/cycling" section-id="lifeandstyle" web-url="http://www.guardian.co.uk/lifeandstyle/cycling"/>
<results>
<response>
这是我第一次尝试编码在PHP(我一直在使用卷曲连接):
$news_items = new SimpleXMLElement($result); //loads the result of the cURL into a simpleXML response
$news_items = $guardian_response->xpath('results');
foreach ($news_items as $item) { //for each statement every entry will load the news_item and the web_url for the document
$item_block = "<p class=\"web_title\">";
$item_block = "<p class=\"web_url\">";
}
它不获取任何,有没有在我的代码的任何缺陷?
2件东西跳出来。首先在第一行代码中,创建一个XML元素$ news_items,但是您正在对另一个元素运行查询。删除第1行,并将第3行更改为$ news_items = new SimpleXmlElement($ result) - > xpath('/ response/results/tag')。其次,你的xpath不会得到任何标签元素,指定/ response/results/tag来获得它们 – 2011-12-14 15:15:32