I need help from selecting data from XML. I can't seem to figure out the way xpath works with specific data selecting. Here is my XML:
<?xml version="1.0"?>
<a>
<b>
<c>Value 1</c>
<d>Value 2</d>
</b>
<b>
<c>Value 3</c>
<d>Value 4</d>
</b>
</a>
And PHP code i'm trying to use would be:
<?php
$xml = simplexml_load_file("xml.xml");
$result = $xml->xpath('b/c | b/d');
foreach ($result as $val){
echo $val['c'] . $val['d'] . "<br>;
}
If i echo $val
, i get all values, however, i want to separate it so i have $val['c']
and $val['d']
, like when selecting specific data from MySQL table. Thank you in advance
You could just point to b
first, then inside the loop, each b
, point it to c
and d
:
$result = $xml->xpath('//b');
foreach ($result as $val) {
echo $val->c . $val->d . "<br>";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.