Owl*_*Owl 1 php xml children simplexml
我有一个问题,答案肯定很简单,但是我缺乏理解。
我有一个具有以下外观的xml文件(简短示例)
<item id="1234">
<property name="country_id">
<value>4402</value>
</property>
<property name="rc_maintenance_other">
</property>
<property name="claim_right_shareholder">
</property>
<property name="charges_other">
</property>
<property name="other_expenses_heating">
</property>
<property name="unpaid_bills_amount">
</property>
<property name="iv_person_phone">
<value>03-6756711</value>
</property>
</item>
<item id="9876">
...
</item>
Run Code Online (Sandbox Code Playgroud)
我的问题是,我想从id为1234的一项中读取所有属性,并在数组中读取其属性和值(如果存在)。
我知道如何使用xpath访问特定项。(感谢这个精彩的stackoverflow社区:))
但是,如何仅对特定项目使用children()函数?
像这样
foreach ($item[id="1234"]->children() as $property) {
Run Code Online (Sandbox Code Playgroud)
非常感谢!
希望对您有所帮助。
码
$xml = new SimpleXMLElement('<item id="1234">
<property name="country_id">
<value>4402</value>
</property>
<property name="rc_maintenance_other">
</property>
<property name="claim_right_shareholder">
</property>
<property name="charges_other">
</property>
<property name="other_expenses_heating">
</property>
<property name="unpaid_bills_amount">
</property>
<property name="iv_person_phone">
<value>03-6756711</value>
</property>
</item>');
foreach ($xml->xpath('//item[@id="1234"]') as $item)
{
foreach ($item->children() as $child) {
echo $child['name'] ."\n";
}
}
Run Code Online (Sandbox Code Playgroud)
输出量
Run Code Online (Sandbox Code Playgroud)country_id rc_maintenance_other claim_right_shareholder charges_other other_expenses_heating unpaid_bills_amount iv_person_phone
示例:http://sandbox.onlinephpfunctions.com/code/4e0ddba2ed273ab4a20dc9379ea9ed0d669a4c0d