PHP XPath:如何获取元素内容?
我需要一些 PHP/Xpath 帮助。谁能给我示例代码以供我遵循?
以下是来自 Amazon Web Service 的与书籍类别相关的 XML 片段(在本例中,该书属于 2 个类别)。我尝试遍历这棵树以我想要的格式获取所需的信息,但没有取得太大成功(正在使用 SimpleXMLElement)。
我需要的结果是一个嵌套的关联数组,如下所示:
[0]
- '11079' => 'Politics'
- '53' => 'Nonfiction'
- '1000' => 'Subjects'
- '283155' => 'Books'
[1]
- '11232' => 'Social Sciences'
- '53' => 'Nonfiction'
- '1000' => 'Subjects'
- '283155' => 'Books'
这是 XML:
<BrowseNodes>
<BrowseNode>
<BrowseNodeId>11079</BrowseNodeId>
<Name>Politics</Name>
<Ancestors>
<BrowseNode>
<BrowseNodeId>53</BrowseNodeId>
<Name>Nonfiction</Name>
<Ancestors>
<BrowseNode>
<BrowseNodeId>1000</BrowseNodeId>
<Name>Subjects</Name>
<IsCategoryRoot>1</IsCategoryRoot>
<Ancestors>
<BrowseNode>
<BrowseNodeId>283155</BrowseNodeId>
<Name>Books</Name>
</BrowseNode>
</Ancestors>
</BrowseNode>
</Ancestors>
</BrowseNode>
</Ancestors>
</BrowseNode>
<BrowseNode>
<BrowseNodeId>11232</BrowseNodeId>
<Name>Social Sciences</Name>
<Ancestors>
<BrowseNode>
<BrowseNodeId>53</BrowseNodeId>
<Name>Nonfiction</Name>
<Ancestors>
<BrowseNode>
<BrowseNodeId>1000</BrowseNodeId>
<Name>Subjects</Name>
<IsCategoryRoot>1</IsCategoryRoot>
<Ancestors>
<BrowseNode>
<BrowseNodeId>283155</BrowseNodeId>
<Name>Books</Name>
</BrowseNode>
</Ancestors>
</BrowseNode>
</Ancestors>
</BrowseNode>
</Ancestors>
</BrowseNode>
</BrowseNodes>
I need some PHP/Xpath help. Can anyone give me sample code to follow?
The following is a snippet of XML from Amazon Web Service pertaining to the categories of a book (in this case, the book falls under 2 categories). I've tried walking this tree to get the information I need in the format I want, but was not having much success (was using SimpleXMLElement).
The result I need is a nested, associative array that looks like this:
[0]
- '11079' => 'Politics'
- '53' => 'Nonfiction'
- '1000' => 'Subjects'
- '283155' => 'Books'
[1]
- '11232' => 'Social Sciences'
- '53' => 'Nonfiction'
- '1000' => 'Subjects'
- '283155' => 'Books'
Here's the XML:
<BrowseNodes>
<BrowseNode>
<BrowseNodeId>11079</BrowseNodeId>
<Name>Politics</Name>
<Ancestors>
<BrowseNode>
<BrowseNodeId>53</BrowseNodeId>
<Name>Nonfiction</Name>
<Ancestors>
<BrowseNode>
<BrowseNodeId>1000</BrowseNodeId>
<Name>Subjects</Name>
<IsCategoryRoot>1</IsCategoryRoot>
<Ancestors>
<BrowseNode>
<BrowseNodeId>283155</BrowseNodeId>
<Name>Books</Name>
</BrowseNode>
</Ancestors>
</BrowseNode>
</Ancestors>
</BrowseNode>
</Ancestors>
</BrowseNode>
<BrowseNode>
<BrowseNodeId>11232</BrowseNodeId>
<Name>Social Sciences</Name>
<Ancestors>
<BrowseNode>
<BrowseNodeId>53</BrowseNodeId>
<Name>Nonfiction</Name>
<Ancestors>
<BrowseNode>
<BrowseNodeId>1000</BrowseNodeId>
<Name>Subjects</Name>
<IsCategoryRoot>1</IsCategoryRoot>
<Ancestors>
<BrowseNode>
<BrowseNodeId>283155</BrowseNodeId>
<Name>Books</Name>
</BrowseNode>
</Ancestors>
</BrowseNode>
</Ancestors>
</BrowseNode>
</Ancestors>
</BrowseNode>
</BrowseNodes>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以用困难的方式做到这一点。
您可以在此处查看结果。
You can do it the hard way.
You can see the result here.
想通了:
Figured it out: