Adding Nodes to XML (PHP)

AreptArcare

New Member
My XML starts like this:\[code\]<?xml version="1.0"?><users> <user> <name/> <nazwisko/> </user> <user> <name/> <nazwisko/> </user></users>\[/code\]And my PHP file for editing XML file is:\[code\]<?phpfunction deleteNode($dom, $index) { $splittedIndexes=split(":", $index); $node = null; $parent = null; foreach ($splittedIndexes as $number) { if ($node == null) { $node = $dom->documentElement; } else { $parent = $node; $node = $node->childNodes->item($number); } } $parent->removeChild($node);}function addNode($dom, $index, $child) {}function printRek($nodeList,$level,$parentIndex) { for ($i = 0; $i < $nodeList->length; $i++) { $node = $nodeList->item($i); $indent=""; if ($node->nodeType == XML_ELEMENT_NODE) { for ($j = 0; $j < $level; $j++) { $indent = $indent."-"; } $myIndex=$parentIndex.":".$i; print <<<BLOK<div>{$indent}{$node->nodeName}<form action="" method="post" style="display: inline;"><input name="delete" type="text" value="http://stackoverflow.com/questions/15464815/{$myIndex}"><input type="submit" value="http://stackoverflow.com/questions/15464815/Usun"></form><form action="" method="post" style="display: inline;"><input name="add" type="text" value=""><input name="index" type="text" value="http://stackoverflow.com/questions/15464815/{$myIndex}"><input type="submit" value="http://stackoverflow.com/questions/15464815/Dodaj podelement"></form></div>BLOK; printRek($node->childNodes,$level+2,$myIndex); } }}$dom=new DOMDocument();$xml="test.xml";$dom->load($xml);$dom->formatOutput=true;header("Content-type: text/html");if (isset($_REQUEST["delete"])) { deleteNode($dom,$_REQUEST["delete"]);}if (isset($_REQUEST["add"])) { addNode($dom, $_REQUEST["index"], $_REQUEST["add"]);}$root=$dom->documentElement;echo $root->nodeName."<br/>";printRek($root->childNodes,2,0);$dom->save($xml);?>\[/code\]I want to add function that creates additional nodes under any given node. All nodes have indexes that look like 0:0:0, 0:0:1, 0:0:2 etc. so function has to get index from the and append it with a child node.Any solutions for that problem? Thanks!
 
Back
Top