PHP:从特定的 XML 节点类型获取文本数组?
我对 PHP 或 XML 并不是完全陌生,但对于将 XML 与 PHP 结合起来我是 100% 的新手。 我有一个包含多个节点的 XML 字符串,但我唯一感兴趣的是 << 关键字> 节点数量不确定,每个节点都包含这样的短语:< 关键词>蓝钻首饰< /关键字> 例如,字符串看起来像这样:
<xml>
<pointless_node/>
<seq>
<keyword>diamond ring</keyword>
<keyword>ruby necklace</keyword>
<keyword>mens watch</keyword>
</seq>
<some_node/>
</xml>
我想要一个像这样的数组:
['diamond ring','ruby necklace','mens watch']
我尝试查看 PHP 手册,但感到困惑,不知道该怎么做。 有人可以指导我如何执行此操作吗? 我正在使用PHP4。
谢谢!
I am not totally new to PHP or XML but I am 100% new to paring XML with PHP. I have an XML string that has several nodes but the only ones I am insterested in are the < keyword > nodes which there are an uncertain number of each containing a phrase like so: < keyword >blue diamond jewelry< /keyword > for example say the string looked like this:
<xml>
<pointless_node/>
<seq>
<keyword>diamond ring</keyword>
<keyword>ruby necklace</keyword>
<keyword>mens watch</keyword>
</seq>
<some_node/>
</xml>
I would want an array like this:
['diamond ring','ruby necklace','mens watch']
I tried looking at the PHP manual and just get confused and not sure what to do. Can someone please walk me through how to do this? I am using PHP4.
THANKS!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当然,看看这个。
Sure, see this.
XML_Parser->xml_parse_into_struct() 可能就是您正在寻找的。
适用于 Php 版本 >= 4
http://se.php.net/xml_parse_into_struct
http://www.w3schools.com/PHP/func_xml_parse_into_struct.asp
XML_Parser->xml_parse_into_struct() might be what you're looking for.
Works for Php versions >= 4
http://se.php.net/xml_parse_into_struct
http://www.w3schools.com/PHP/func_xml_parse_into_struct.asp
我认为最简单的是:
I think the easiest is:
请参阅:http://www.php.net/simplexml-element-xpath
尝试以下 xpath 和数组构造
编辑:修改代码以反映 php 4 要求
编辑:修改为考虑到 xpath_new_context 的记录不全的行为(php 文档注释指出了错误)
see: http://www.php.net/simplexml-element-xpath
Try the following xpath and array construction
edit: modified code to reflect php 4 requirements
edit: modified to account for poorly documented behaviour of xpath_new_context (php docs comments point out the error)