Jump to content

Update XML Node


The Little Guy

Recommended Posts

How do I update an xml node?

 

Currently I have this:

$xml = simplexml_load_file($xmlfile);
$user = $xml->xpath('/root/users/user/name');
$usr = $user[0][0]->asXML();
$usr = $name;

 

I don't know if that I right or not, but I do know that if it is right, it isn't updating the xml file. Am I on the right track, or not? What can I do?

Link to comment
https://forums.phpfreaks.com/topic/217818-update-xml-node/
Share on other sites

XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
<title>Users</title>
<instructions>
	<rootdir>Users</rootdir>
	<phpfiles>
		<file>users.php</file>
	</phpfiles>
</instructions>
<users>
	<user>
		<name>root</name>
		<password>5f4dcc3b5aa765d61d8327deb882cf99</password>
		<permissions>
			<access>CREATE_USR</access>
		</permissions>
	</user>
</users>
</root>

 

In the name tag, I would like to replace it with something else, something that is entered in into the command line.

Link to comment
https://forums.phpfreaks.com/topic/217818-update-xml-node/#findComment-1130568
Share on other sites

This should be a rather simple operation to run. I am not sure about running on CLI (command line interface), but I would start with something using SimpleXML like:

 

require_once 'xml_string.php';
if(!$xml=simplexml_load_string($xmlstr)){
    trigger_error('Error reading XML string',E_USER_ERROR);
}
$message=(string)$xml->user[5]->name=='Alex Jones'?'User
found!':'User not found!';
echo $message;

 

Of course you will need to modify this to suite your needs, but should get you on the right path. Hope this helps.

Link to comment
https://forums.phpfreaks.com/topic/217818-update-xml-node/#findComment-1130570
Share on other sites

um... didn't really answer my question...

 

I know how to get a node value, but I don't know how to update one.

 

Does something like this help?

 

function replaceNodeText($objXml, $objNode, $strNewContent){
    /*
    This function replaces a node's string content with strNewContent
    */
    $objNodeListNested = &$objNode->childNodes;
    foreach ( $objNodeListNested as $objNodeNested ){
         if ($objNodeNested->nodeType == XML_TEXT_NODE)$objNode->removeChild ($objNodeNested);
    }
    
    $objNode->appendChild($objXml->createTextNode($strNewContent));
}

$objXml= new DOMDocument();
$objXml->loadXML('<root><node id="1">bla</note></root>');
$objXpath = new domxpath($objXml);

$strXpath="/root/node[@id='1']";
$objNodeList = $objXpath ->query($strXpath);
foreach ($objNodeList as $objNode){
//pass the node by reference
replaceNodeText($objXml, &$objNode, $strImportedValue);
}

 

Link to comment
https://forums.phpfreaks.com/topic/217818-update-xml-node/#findComment-1130587
Share on other sites

This may or may not work for your needs, but might give some food for thought:

 

 

require_once 'xml_string.php';
if(!$xml=simplexml_load_string($xmlstr)){
    trigger_error('Error reading XML string',E_USER_ERROR);
}
$xml->name->'root'->name='New Value';
echo $xml->asXML();

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/217818-update-xml-node/#findComment-1130596
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.