Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting data in XML file with PHP DOM

Tags:

dom

php

xml

I was trying to insert new data into an existing XML file, but it's not working. Here's my xml file:

<list>
    <activity>swimming</activity>
    <activity>running</activity>
<list>

Now, my idea was making two files: an index page, where it displays what's on the file and provides a field for inserting new elements, and a php page which will insert the data into the XML file. Here's the code for index.php:

<html>
<head><title>test</title></head>
</head>

<?php
    $xmldoc = new DOMDocument();
    $xmldoc->load('sample.xml', LIBXML_NOBLANKS);

    $activities = = $xmldoc->firstChild->firstChild;
    if($activities!=null){
        while(activities!=null){
            echo $activities->textContent.'<br/>';
            activities = activities->nextSibling.
        }
    }
?>

<form name='input' action='insert.php' method='post'>
    insert activity:
    <input type='text' name='activity'/>
    <input type='submit' value='send'/>
</form>
</body>
</html

and here's the code for insert.php:

<?php
    header('Location:index.php');
    $xmldoc = new DOMDocument();
    $xmldoc->load('sample.xml');

    $newAct = $_POST['activity'];

    $root = $xmldoc->firstChild;

    $newElement = $xmldoc->createElement('activity');
    $root->appendChild($newElement);
    $newText = $xmldoc->createTextNode($newAct);
    $newElement->appendChild($newText);

    $xmldoc->save('sample.xml');
?>

The user is to access index.php, where he would see a list of the current activities present in the XML file, and a text field below where he can insert new activities. Upon clicking the send button, the page would call insert.php, which contains a code that opens the XML file in a DOM tree, inserts a new node under the root node and calls back the index.php page, where the user should be able to see the list of activities, his new activity there under the others. It is not working. When i click on the button to submit a new entry, the pages refreshes and apparently nothing happens, the XML is the same as before. What did i do wrong? Also, i'd like to know if there's a better way of doing it.

like image 345
liewl Avatar asked Oct 11 '08 20:10

liewl


People also ask

How do I add data to an XML file?

To insert data into an XML column, use the SQL INSERT statement. The input to the XML column must be a well-formed XML document, as defined in the XML 1.0 specification. The application data type can be an XML, character, or binary type.

Can I write PHP code in XML file?

If you run the XML file as a PHP script, then yes. The output of the script will be valid XML, but the file that contains the inline PHP will most likely not be valid XML. Save this answer.

Does PHP support dom?

The built-in DOM parser makes it possible to process XML documents in PHP.

What is DOM document in PHP?

The DOMDocument::getElementsByTagName() function is an inbuilt function in PHP which is used to return a new instance of class DOMNodeList which contains all the elements of local tag name.


2 Answers

is your code block copy and pasted from your existing files? if so i see two potential issues:

<form name='input' action'insert.php' method='post'> // should be:
<form name="input" action="insert.php" method="post">

note: you're missing action="insert.php", which would cause the form to just reload itself without submitting, which is the behaviour you describe.

secondly, make sure you have write permission to "sample.xml". you can confirm if you're actually writing anything:

print 'I wrote '.$xmldoc->save('sample.xml').' bytes of data';
like image 71
Owen Avatar answered Sep 27 '22 23:09

Owen


Final Solution

sample.XML

<list>
    <activity>swimming</activity>
    <activity>running</activity>
    <activity>Jogging</activity>
    <activity>Theatre</activity>
    <activity>Programming</activity>
</list>

index.php

<html>
<head><title>test</title></head>
</head>

<?php
    $xmldoc = new DOMDocument();
    $xmldoc->load("sample.xml", LIBXML_NOBLANKS);

    $activities = $xmldoc->firstChild->firstChild;
    if($activities!=null){
        while($activities!=null){
            echo $activities->textContent."<br/>";
            $activities = $activities->nextSibling;
        }
    }
?>

<form name="input" action="insert.php" method="post">
    insert activity:
    <input type="text" name="activity"/>
    <input type="submit" value="send"/>
</form>
</body>
</html>

insert.php

<?php
    header('Location:index.php');
    $xmldoc = new DOMDocument();
    $xmldoc->load('sample.xml');

    $newAct = $_POST['activity'];

    $root = $xmldoc->firstChild;

    $newElement = $xmldoc->createElement('activity');
    $root->appendChild($newElement);
    $newText = $xmldoc->createTextNode($newAct);
    $newElement->appendChild($newText);

    $xmldoc->save('sample.xml');
?>
like image 31
Amdad Hossain Avatar answered Sep 27 '22 23:09

Amdad Hossain