0

i have an xml structure:

<root>
...
   <B ID="444">
        <C></C>  
        <D>Anything</D>
        <E>
            <child1 /*expected outcome came in here*/> /*sometimes here*/ </child1>
            <child2>data2 <!-instead of here-></child2>
            <child3>data3</child3>
            <child4>data4 /* and also here instead*/</child4>
        </E>
    </B>
...
</root>

i made an xpath query to fetch //*B, where child2==data2 and child4==data4 like this:

$query = "//*[@ID=444]//*['data2' and 'data4']";
$result = $xml->xpath($query);

it worked fine. but now, i wanna update data1 and data3; after finding a match, using only simplexml API.

here's what i did, but not working rightly:

if(!empty($result)){
    $result[0]['data2'] = "Blabla";
    $result[0]['data4'] = "Blaaaaaaaa";
    return $xml->saveXML($File;
}

what am i doing wrong ? or how is it better done with simplexml ? any idea will be apprciated.

2
Contributors
1
Reply
6
Views
2 Years
Discussion Span
Last Post by pritaeas
0

I think it should be something like this:

$query = "//*[@ID=444]//*[text() = 'data2']";

since you want to find the text value of a node. Am a bit rusty in xpath, so you'll have to try.

Have a look at this tool

Edited by pritaeas

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.