1

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

0

1 Answer 1

1

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>";
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.