Find value of XML sibling node

How do I get the sibling values of a node

If I know ref (GAS108) how do I get price ?

What code do I use ?

<property>
<id>1925</id>
<date>2017-05-10 14:49:05</date>
<ref>GAS108</ref>
<price>315000</price>
<price_freq>sale</price_freq>
<type><en>Terraced property</en></type>
<town>Santa Pola</town>
<province>Alicante</province>
<beds>3</beds>
<baths>1</baths>
</property>

When you post code on the forums, you need to format it so it will display correctly.

You can highlight your code, then use the </> button in the editor window, or you can place three backticks ``` (top left key on US/UK keyboards) on a line above your code, and three on a line below your code. I find this approach easier, but unfortunately some European and other keyboards don’t have that character.

Using SimpleXML you can query the XML with an xpath expression like so:

$xml = new SimpleXMLElement($xmlstr);

// Returns an array of the matched SimpleXMLElements
$result = $xml->xpath('//ref[text()="GAS108"]/parent::property/price');

A simpler expression doing the same thing is: //property[ref="GAS108"]/price

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.