Jump to content

Replace child in xml document


socox11

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.