socox11 Posted February 8, 2011 Share Posted February 8, 2011 Hi all, I can't get this code to work, if you can tel me what I am doing wrong I would be gratefull. This is my XML file: <?xml version="1.0" encoding="UTF-8"?> <popis> <student> <prezime>Brkic</prezime> <ime>Ivica</ime> <index>D-142</index> </student> <student> <prezime>Cizek</prezime> <ime>Pero</ime> <index>D-143</index> </student> </popis> This is the form I wanna use to change nodes: <form action="zamjena.php" method="post" enctype="multipart/form-data"> <table> <tr> <td colspan="2"class="labelcell"><label for="ime">Ime:</label></td> <td colspan="2"class="fieldcell"><input type="text" id="ime" name="ime" tabindex="1"/></td> </tr> <tr> <td colspan="2"class="labelcell"><label for="prezime">Prezime:</label></td> <td colspan="2"class="fieldcell"> <input type="text" id="prezime" name="prezime" tabindex="2"/><br /> </td> </tr> <tr> <td colspan="2"class="labelcell"><label for="index">Index:</label></td> <td colspan="2"class="fieldcell"> <input type="text" id="index" name="index" tabindex="3"/> <br /> </td> </tr> <tr> <td colspan="2"class="labelcell"><label for="index">ID:</label></td> <td colspan="2"class="fieldcell"> <input type="text" id="id" name="id" tabindex="4"/> <br /> </td> </tr> <td colspan="4"><input type="submit" name="upload" class="box" value="Zamjeni" tabindex="5" /></td> </table> </form> "id" is the mark which node to change And this is code for changin nodes, I found it on: http://us.php.net/manual/en/domnode.replacechild.php : <?php // Create a new document fragment to hold the new <student> node $student = new DomDocument; $student_node = $student ->createElement('student'); // Add some children $student_node->appendChild($student->createElement('ime', '$_POST['ime']')); $student_node->appendChild($student->createElement('prezime', '$_POST['prezime']')); $student_node->appendChild($student->createElement('index', '$_POST['index']')); // Add the keywordset into the new document // The $student variable now holds the new node as a document fragment $student->appendChild($student_node); ?> Next, we need to locate the old node: <?php // Load the XML $dom = new DomDocument; $dom->loadXML($xml); // Locate the old student node $xpath = new DOMXpath($dom); $nodelist = $xpath->query('/popis/student'); $oldnode = $nodelist->item($_POST['id']); ?> We then import and replace the new node: <?php // Load the $student document fragment into the current document $newnode = $dom->importNode($student->documentElement, true); // Replace $oldnode->studentNode->replaceChild($newnode, $oldnode); // Display echo $dom->saveXML(); ?> thx for help Link to comment https://forums.phpfreaks.com/topic/227039-replace-child-in-xml-document/ Share on other sites More sharing options...
socox11 Posted February 8, 2011 Author Share Posted February 8, 2011 maybe someone have better idea?? anything? Link to comment https://forums.phpfreaks.com/topic/227039-replace-child-in-xml-document/#findComment-1171570 Share on other sites More sharing options...
BlueSkyIS Posted February 8, 2011 Share Posted February 8, 2011 i don't do anything with xml, but you need to remove quotes from around the $_POST variables. one example. $student_node->appendChild($student->createElement('ime', $_POST['ime'])); the code should not even compile with those quotes in there. are you not getting syntax errors? Link to comment https://forums.phpfreaks.com/topic/227039-replace-child-in-xml-document/#findComment-1171594 Share on other sites More sharing options...
socox11 Posted February 9, 2011 Author Share Posted February 9, 2011 sry i forgot to remove those... still doesnt work... Link to comment https://forums.phpfreaks.com/topic/227039-replace-child-in-xml-document/#findComment-1171761 Share on other sites More sharing options...
socox11 Posted February 9, 2011 Author Share Posted February 9, 2011 i solved it, problem was the path to node... http://www.w3.org/TR/xpath/ Link to comment https://forums.phpfreaks.com/topic/227039-replace-child-in-xml-document/#findComment-1171992 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.