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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.