6

I just wanted to ask how I can insert a new node in an XML using PHP. my XML file (questions.xml) is given below

<?xml version="1.0" encoding="UTF-8"?>
<Quiz>
   <topic text="Preparation for Exam">
      <subtopic text="Science" />
      <subtopic text="Maths" />
      <subtopic text="english" />
   </topic>
</Quiz>

I want to add a new "subtopic" with the "text" attribute, that is "geography". How can I do this using PHP? Thanks in advance though. well my code is

<?php

$xmldoc = new DOMDocument();
$xmldoc->load('questions.xml');


$root = $xmldoc->firstChild;

$newElement = $xmldoc->createElement('subtopic');
$root->appendChild($newElement);
// $newText = $xmldoc->createTextNode('geology');
// $newElement->appendChild($newText);

$xmldoc->save('questions.xml');
?>
3
  • yup that was me .. no satisfactory reply . . Commented Mar 4, 2013 at 12:29
  • Because you didn't expect an explanation but a piece of code you can use. No reason to ask the same question again. Commented Mar 4, 2013 at 12:30
  • there was a reason .. see ..i got 2 valid responses after posting it again...so there was a reason for posting it again . Commented Mar 4, 2013 at 12:48

3 Answers 3

11

I'd use SimpleXML for this. It would look somehow like this:

// Open and parse the XML file
$xml = simplexml_load_file("questions.xml");
// Create a child in the first topic node
$child = $xml->topic[0]->addChild("subtopic");
// Add the text attribute
$child->addAttribute("text", "geography");

You can either display the new XML code with echo or store it in a file.

// Display the new XML code
echo $xml->asXML();
// Store new XML code in questions.xml
$xml->asXML("questions.xml");
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks man .. now that was a satisfactory and a very good reply . . i appreciate ur effort
5

The best and safe way is to load your XML document into a PHP DOMDocument object, and then go to your desired node, add a child, and finally save the new version of the XML into a file.

Take a look at the documentation : DOMDocument

Example of code:

// open and load a XML file
$dom = new DomDocument();
$dom->load('your_file.xml');

// Apply some modification
$specificNode = $dom->getElementsByTagName('node_to_catch');
$newSubTopic = $xmldoc->createElement('subtopic');
$newSubTopicText = $xmldoc->createTextNode('geography');
$newSubTopic->appendChild($newSubTopicText);
$specificNode->appendChild($newSubTopic);

// Save the new version of the file
$dom->save('your_file_v2.xml');

3 Comments

okay... i have edited the file ...my codings are there .. but my code is adding the new subtopic at the end ... and its not adding the text attribute :(
You have to create a new element $newSubTopic = $xmldoc->createElement('subtopic');, then create a new text node $subTopicContent = $xmldoc->createTextNode('geology');, and finally append the textnode in the new topic element, and then append te new topic element to your desire node.
Where have you defined xmldoc ?
-1

You can use PHP's Simple XML. You have to read the file content, add the node with Simple XML and write the content back.

1 Comment

yeah ... can u help me with this ... just a hint...i know how to read stuff using simplexml , but don't know how to add a new node using simple xml..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.