【发布时间】:2014-12-03 18:45:40
【问题描述】:
需要帮助更新我之前所做的一些 simplexml 代码。我正在解析的 XML 文件以一种新的方式格式化,但我不知道如何导航它。
旧 XML 格式示例:
<?xml version="1.0" encoding="UTF-8"?>
<pf version="1.0">
<pinfo>
<pid><![CDATA[test1 pid]]></pid>
<picture><![CDATA[http://test1.image]]></picture>
</pinfo>
<pinfo>
<pid><![CDATA[test2 pid]]></pid>
<picture><![CDATA[http://test2.image]]></picture>
</pinfo>
</pf>
然后是新的 XML 格式(注意“类别名称”添加):
<?xml version="1.0" encoding="UTF-8"?>
<pf version="1.2">
<category name="Cname1">
<pinfo>
<pid><![CDATA[test1 pid]]></pid>
<picture><![CDATA[http://test1.image]]></picture>
</pinfo>
</category>
<category name="Cname2">
<pinfo>
<pid><![CDATA[test2 pid]]></pid>
<picture><![CDATA[http://test2.image]]></picture>
</pinfo>
</category>
</pf>
在 XML 中添加“类别名称”后,用于解析的旧代码下方:
$pinfo = new SimpleXMLElement($_SERVER['DOCUMENT_ROOT'].'/xml/file.xml', null, true);
foreach($pinfo as $resource)
{
$Profile_id = $resource->pid;
$Image_url = $resource->picture;
// and then some echo´ing of the collected data inside the loop
}
我需要添加什么或做完全不同的事情?我尝试使用 xpath、children 和按属性排序,但没有运气 - SimpleXML 对我来说一直是个谜 :)
【问题讨论】:
标签: php xml xpath xml-parsing simplexml