我正在寻找从大型 xml 文件中提取一些节点的解决方案(使用xmlstarlet http://xmlstar.sourceforge.net/),然后将此节点解析为php array。
elements.xml
<?xml version="1.0"?>
<elements>
<element id="1" par1="val1_1" par2="val1_2" par3="val1_3">
<title>element 1 title</title>
<description>element 1 description</description>
</element>
<element id="2" par1="val2_1" par2="val2_2" par3="val2_3">
<title>element 2 title</title>
<description>element 2 description</description>
</element>
</elements>
要使用xmlstarlet提取id="1" 的元素标签,我正在执行这个 shell 命令......
xmlstarlet sel -t -c "/elements/element[id=1]" elements.xml
这个shell命令输出类似这样的东西......
<element id="1" par1="val1_1" par2="val1_2" par3="val1_3">
<title>element 1 title</title>
<description>element 1 description</description>
</element>
如何将此 shell 输出解析为php 数组?
谢谢你。
我发现http://php.net/manual/en/function.simplexml-load-string.php非常有用。这个 SimpleXML 函数会将提取的 XML 片段转换为 SimpleXML 对象。