The Little Guy Posted November 5, 2010 Share Posted November 5, 2010 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? Quote Link to comment Share on other sites More sharing options...
OldWest Posted November 5, 2010 Share Posted November 5, 2010 Can you post up the XML? And also clarify when you want to achieve as an end result? Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted November 5, 2010 Author Share Posted November 5, 2010 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. Quote Link to comment Share on other sites More sharing options...
OldWest Posted November 5, 2010 Share Posted November 5, 2010 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. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted November 5, 2010 Author Share Posted November 5, 2010 um... didn't really answer my question... I know how to get a node value, but I don't know how to update one. Quote Link to comment Share on other sites More sharing options...
OldWest Posted November 5, 2010 Share Posted November 5, 2010 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); } Quote Link to comment Share on other sites More sharing options...
OldWest Posted November 5, 2010 Share Posted November 5, 2010 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(); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.