Skip to content
Advertisement

How to parse xml subtree in php

Hello have worten some php code to parse a xml file as long if i use seperate fields this works like a charm but how i can call a subtree ?

like eg :

<item>
  <items>
    <date>
      <from>2020-03-23</from>
      <until>2020-03-27</until>
    </date>
  </items>
</item>
```

Advertisement

Answer

I assume with subtree you mean an element within an element etc.

PHP offers a built in solution for this: simplexml_load_string

$xml = <<<BLOCK
<item>
  <items>
    <date>
      <from>2020-03-23</from>
      <until>2020-03-27</until>
    </date>
  </items>
</item>
BLOCK;

var_dump(simplexml_load_string($xml));

You can read a file directly aswell by using simplexml_load_file.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement