1

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!

Owl
  • 689
  • 5
  • 15
  • 29
  • First write the code how to access the *item* element you want the children from. Store it in a variable. Call the children() method on it. As you wrote you know how the first part works, and you know about the children() method so it's basically only putting those two together. – hakre Apr 17 '13 at 07:50
  • possible duplicate of [SimpleXML get element content based on attribute value](http://stackoverflow.com/questions/4736417/simplexml-get-element-content-based-on-attribute-value) – hakre Apr 17 '13 at 07:51
  • But I might not understand your question fully, maybe shed some light. Do you mean to get all descendants? – hakre Apr 17 '13 at 07:59
  • possible duplicate of [Implementing condition in XPath and XQuery](http://stackoverflow.com/questions/3448005/implementing-condition-in-xpath-and-xquery) – kittycat Apr 21 '13 at 20:50

2 Answers2

6

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

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

S.Visser
  • 4,645
  • 1
  • 22
  • 43
  • Thank you it works fine! I knew it was simple but my brain just didn't want to output it. I generate the path before with the $id variable and call it like this `foreach ($xml->xpath($path) as $item)` – Owl Apr 17 '13 at 09:16
  • And in case, somebody else need it, here how to access the values of the property nodes as well: `$path = '//item[@id="'.$id.'"]'; foreach ($xml->xpath($path) as $item) { foreach ($item->children() as $child) { foreach ($child->children() as $value) { echo $child['name'] ." = ".$value."
    "; } } }`
    – Owl Apr 17 '13 at 09:24
1

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

hakre
  • 193,403
  • 52
  • 435
  • 836