SimpleXML& PHP:提取XML文档的一部分&转换为数组
考虑以下 XML:
<?xml version="1.0" encoding="UTF-8"?>
<OS>
<data>
<OSes>
<centos>
<v_5>
<i386>
<id>centos5-32</id>
<name>CentOS 5 - 32 bit</name>
<version>5</version>
<architecture>32</architecture>
<os>centos</os>
</i386>
<x86_64>
<id>centos5-64</id>
<name>CentOS 5 - 64 bit</name>
<version>5</version>
<architecture>64</architecture>
<os>centos</os>
</x86_64>
</v_5>
<v_6>
<i386>
<id>centos6-32</id>
<name>CentOS 6 - 32 bit</name>
<version>6</version>
<architecture>32</architecture>
<os>centos</os>
</i386>
<x86_64>
<id>centos6-64</id>
<name>CentOS 6 - 64 bit</name>
<version>6</version>
<architecture>64</architecture>
<os>centos</os>
</x86_64>
</v_6>
</centos>
<ubuntu>
<v_10>
<i386>
<id>ubuntu10-32</id>
<name>Ubuntu 10 - 32 bit</name>
<version>10</version>
<architecture>32</architecture>
<os>ubuntu</os>
</i386>
<amd64>
<id>ubuntu10-64</id>
<name>Ubuntu 10 - 64 bit</name>
<version>10</version>
<architecture>64</architecture>
<os>ubuntu</os>
</amd64>
</v_10>
</ubuntu>
</OSes>
</data>
</OS>
从上面的 XML 文档中,我想提取以下 5 个元素节点
并将它们作为数组。我尝试执行以下操作:
<?php
require_once "xml.php";
try {
$xml = new SimpleXMLElement($xmlstr);
foreach($xml->xpath(' //id | //name | //version// | //architecture | //os ') as $record) {
echo $record;
}
} catch(Exception $e){
echo $e->getMessage();
}
上面的代码有效,但每个记录都是一个单独的对象。我希望有人将所有 5 个元素节点合并为一个数组元素。像这样的事情:
$osList = Array( [0] => Array(
["id"] => "<id>",
["name"] => "<name>",
["version"] => "<version>",
....
)
.....
);
语法不正确,但你明白了。知道怎么做吗?
Consider the following XML:
<?xml version="1.0" encoding="UTF-8"?>
<OS>
<data>
<OSes>
<centos>
<v_5>
<i386>
<id>centos5-32</id>
<name>CentOS 5 - 32 bit</name>
<version>5</version>
<architecture>32</architecture>
<os>centos</os>
</i386>
<x86_64>
<id>centos5-64</id>
<name>CentOS 5 - 64 bit</name>
<version>5</version>
<architecture>64</architecture>
<os>centos</os>
</x86_64>
</v_5>
<v_6>
<i386>
<id>centos6-32</id>
<name>CentOS 6 - 32 bit</name>
<version>6</version>
<architecture>32</architecture>
<os>centos</os>
</i386>
<x86_64>
<id>centos6-64</id>
<name>CentOS 6 - 64 bit</name>
<version>6</version>
<architecture>64</architecture>
<os>centos</os>
</x86_64>
</v_6>
</centos>
<ubuntu>
<v_10>
<i386>
<id>ubuntu10-32</id>
<name>Ubuntu 10 - 32 bit</name>
<version>10</version>
<architecture>32</architecture>
<os>ubuntu</os>
</i386>
<amd64>
<id>ubuntu10-64</id>
<name>Ubuntu 10 - 64 bit</name>
<version>10</version>
<architecture>64</architecture>
<os>ubuntu</os>
</amd64>
</v_10>
</ubuntu>
</OSes>
</data>
</OS>
From the XML document above, I want to extract following 5 element node
<id>
<name>
<version>
<architecture>
<os>
And have them as a array. I tried doing the following:
<?php
require_once "xml.php";
try {
$xml = new SimpleXMLElement($xmlstr);
foreach($xml->xpath(' //id | //name | //version// | //architecture | //os ') as $record) {
echo $record;
}
} catch(Exception $e){
echo $e->getMessage();
}
the above code works but each record is an separate object. I want someone to consolidate all 5 elements nodes as one array element. something like this:
$osList = Array( [0] => Array(
["id"] => "<id>",
["name"] => "<name>",
["version"] => "<version>",
....
)
.....
);
syntax isn't correct but you get the idea. any idea how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可能有帮助
this might help
通过按照其他人的建议修改 xpath,我得出了这个结论。它与一个辅助函数配合使用来重新格式化每个 xpath 结果节点,并使用 array_reduce 来迭代结果。然后它返回转换后的结果(Demo):
输出:
我还选择转换原始 xpath 结果放入两个级别的数组中,每次在当前级别内已经存在一个键时,将当前条目移动到新条目(Demo< /a>):
结果那么是这样的:
By modifying the xpath as others suggested as well, I came to this conclusion. It works with one helper function to re-format each xpath result node and uses
array_reduce
to iterate over the result. It then returns the converted result (Demo):Output:
I also opted for converting the original xpath result into an array of two levels, each time within the current level a key already exists, move the current entry to a new entry (Demo):
Result is like this then:
试试这个:
我的结果:
如果你使用 PHP >= 5.3 (当然你是,为什么你不呢)你可以省略讨厌的 tmp 函数定义并使用很酷的匿名函数进行映射:
Try this:
My result:
If you're using PHP >= 5.3 (ofcourse you are, why whouldn't you) you can omit the nasty tmp function definitions and use cool anonymous functions for the mapping: