Dan
Dan

Reputation: 55

Modifying <string name="myname">myvalue</string> XML elements in PHP

I'm trying to programatically modify some XML elements using PHP. They have the form

<string name="myname1">myvalue1</string>
<string name="myname2">myvalue2</string>

etc. I do not have control over the incoming structure of this XML file. Is there a good way to edit these in PHP? I've tried using a DOMDocument, but I'm not having much luck making it work because the elements don't have IDs specified and doing a search by tag name gives me all the strings, not the one I want. Thanks for any pointers.

Upvotes: 0

Views: 65

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

DOMXPath solves this problem nicely:

$dom = new DOMDocument();
$dom->loadXML($input);
$xpath = new DOMXPath($dom);
$string = $xpath->query("//string[@name='myname1']")->item(0);
if( $string) {
    // do something
}

Upvotes: 2

Related Questions