I'm traversing an XML file and displaying certain attributes and node values in an HTML form. After the form is submitted, I'd like to insert the values of form fields into the XML file directly.
Basically, I'm building an "online xml editor" for specific nodes and attributes.
I'm using DOMDocument to load the XML, I find all the attributes & node values that I want to change. What I need now is to save unique identifiers for each edited node so that after posting I can save form fields into XML.
I can store XPATH into an array like this
array_push($edited_images, $image->getNodePath());
... but I don't know how to access these nodes on the next page. I would need some kind of method to do this:
$doc = new DOMDocument();
...
$node_to_edit = $doc->selectOneNodeBySpecificXPATH($edited_images[0]);
$node_to_edit->->setAttribute($a, 'FORM FIELD CONTENT FROM PAGE');
I can't find a method that would help me to select a single node using XPATH though. Is there a better unique identifier for a node for DOMDocument? Or a better general approach for what I need?
Thanks for any ideas.
To select a single node using XPath, use DOMXPath
:
$xpath = new DOMXpath($doc);
$node = $xpath->query($edited_images[0])->item(0);
To select a single node using the stored XPath, use DOMXPath
and it's query
function:
$xpath = new DOMXpath($doc);
$node = $xpath->query($edited_images[0]);
$node && $node = $node->item(0);
As it returns a DOMNodeList
, you need to select the first one if successful. If $node
still evaluates false
do the error handling.
The other thing is to use IDs, but it looks like that you don't have any.