使用simplexml和php访问某个节点的所有子节点

使用simplexml和php访问某个节点的所有子节点

问题描述:

I have a question and the answer is sure simple, but it just lacks of understanding from my side.

I have a xml file with following look (short example)

<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>

My problem is, I want to read all propertys from the one item with the id 1234 with their attribute and their value, if exists, in an array.

I know how to access the certain Item with xpath. (Thanks to this wonderful stackoverflow community :) )

But how can I use the children() function only to a certain item?

Like this

foreach ($item[id="1234"]->children() as $property) {

Thank you so much!

我有一个问题,答案肯定很简单,但它只是缺乏对我的理解。 p >

我有一个带有以下外观的xml文件(简短示例) p>

 &lt; item id =“1234”&gt; 
&lt; property name  =“country_id”&gt; 
&lt; value&gt; 4402&lt; / value&gt; 
&lt; / property&gt; 
&lt; property name =“rc_maintenance_other”&gt; 
&lt; / property&gt; 
&lt; property name =  “claim_right_shareholder”&gt; 
&lt; / property&gt; 
&lt; property name =“charges_other”&gt; 
&lt; / property&gt; 
&lt; property name =“other_expenses_heating”&gt; 
&lt; / property&gt;  
&lt; property name =“unpaid_bills_amount”&gt; 
&lt; / property&gt; 
&lt; property name =“iv_person_phone”&gt; 
&lt; value&gt; 03-6756711&lt; / value&gt; 
&lt; / property&gt  ; 
&lt; / item&gt; 
&lt; item id =“9876”&gt; 
 ... 
&lt; / item&gt; 
  code>  pre> 
 
 

我的问题是, 我想从id为1234的一个项目中读取所有属性 它们的属性及其值(如果存在)在数组中。 p>

我知道如何使用xpath访问某个Item。 (感谢这个精彩的stackoverflow社区:)) p>

但是如何才能将children()函数仅用于某个项? p>

喜欢这个 p>

  foreach($ item [id =“1234”]  - &gt; children()as $ property){
  code>  pre> 
 
  

非常感谢你! p> div>

I hope this can help you.

Code

$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'] ."
";
    }
}

Output

country_id
rc_maintenance_other
claim_right_shareholder
charges_other
other_expenses_heating
unpaid_bills_amount
iv_person_phone

Example: http://sandbox.onlinephpfunctions.com/code/4e0ddba2ed273ab4a20dc9379ea9ed0d669a4c0d

But how can I use the children() function only to a certain item?

The SimpleXMLElement::children()Docs method is used always to a certain element in Simplexml. So you can do that by just using it.

$element->children();

The manual coins it this way:

Finds children of given node