xpath获取所有元素
我正在使用 simplexml 来解析 xml,我可以得到单个节点,但不是我想要的所有节点,
示例
$xmlObject = simplexml_load_file($xml_file); // load the xml file
$cols = $xmlObject->RESULTSET->ROW->COL;
foreach ( $cols as $COL) {
echo $COL->DATA;
}
只给我第一行的第一个列,我尝试过
$xmlObject = simplexml_load_file($xml_file); // load the xml file
$cols = $xmlObject->xpath('*//COL');
foreach ( $cols as $COL) {
echo $COL->DATA;
}
,但没有得到
任何结果,知道我可能做错了什么吗?
<?xml version="1.0" encoding="UTF-8"?>
<FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult">
<RESULTSET FOUND="445">
<ROW MODID="45" RECORDID="1">
<COL>
<DATA>Integrating Ethanol</DATA>
</COL>
<COL>
<DATA/>
</COL>
<COL>
<DATA>train track and gas pump</DATA>
</COL>
<COL>
<DATA/>
</COL>
<COL>
<DATA/>
</COL>
<COL>
<DATA>1.jpg</DATA>
</COL>
<COL>
<DATA/>
</COL>
</ROW>
<ROW MODID="29" RECORDID="3">
<COL>
<DATA>Keeping in Balance</DATA>
</COL>
<COL>
<DATA>21</DATA>
</COL>
<COL>
<DATA>book cover of Sweet Invisible Body</DATA>
</COL>
<COL>
<DATA/>
</COL>
</ROW>
</RESULTSET>
</FMPXMLRESULT>
I am using simplexml to parse xml and I can get single node but not all nodes i want
example
$xmlObject = simplexml_load_file($xml_file); // load the xml file
$cols = $xmlObject->RESULTSET->ROW->COL;
foreach ( $cols as $COL) {
echo $COL->DATA;
}
only gives me the first col of the first row, i tried
$xmlObject = simplexml_load_file($xml_file); // load the xml file
$cols = $xmlObject->xpath('*//COL');
foreach ( $cols as $COL) {
echo $COL->DATA;
}
and got nothing back
any idea what I might be doing wrong ?
<?xml version="1.0" encoding="UTF-8"?>
<FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult">
<RESULTSET FOUND="445">
<ROW MODID="45" RECORDID="1">
<COL>
<DATA>Integrating Ethanol</DATA>
</COL>
<COL>
<DATA/>
</COL>
<COL>
<DATA>train track and gas pump</DATA>
</COL>
<COL>
<DATA/>
</COL>
<COL>
<DATA/>
</COL>
<COL>
<DATA>1.jpg</DATA>
</COL>
<COL>
<DATA/>
</COL>
</ROW>
<ROW MODID="29" RECORDID="3">
<COL>
<DATA>Keeping in Balance</DATA>
</COL>
<COL>
<DATA>21</DATA>
</COL>
<COL>
<DATA>book cover of Sweet Invisible Body</DATA>
</COL>
<COL>
<DATA/>
</COL>
</ROW>
</RESULTSET>
</FMPXMLRESULT>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试使用以下忽略命名空间的 XPath 表达式:
此表达式选择名为“COL”的所有节点,无论该节点位于哪个命名空间中。
You can try with the following XPath expression which ignores the namespace:
This expression selects all nodes with name 'COL' no matter in what namespace the node is in.
正确的 xpath 是
没有
我认为你的问题可能是 XML 命名空间,当我通过使用 Google 找到的这个工具运行 XML 时,它给我带来了问题: http://www.xmlme.com/XpathTool.aspx
The right xpath for this would be
without the
I think your problem might be that XML namespace, which was causing the problem for me when I ran the XML through this tool that I found using Google: http://www.xmlme.com/XpathTool.aspx