Jump to content


This topic is now archived and is closed to further replies.


writing tags in xml?

Recommended Posts

hey everyone

I'm making a file, that can write news text into an xml-file. All my text for my page is in the xml-file, and the structure kind of looks like this:


I have a piece of code (using the dom-functions) and I can easily create new tags within the root-tag. The problem is, that I want to put it inside the news-tag, so I can organize it. I've tried looking around in php.net/dom, but can't really fiure it out. Should be easy anyway. Here is my code:


$dom = new DomDocument();

$item = $dom->createElement("item");
$overskrift = $dom->createElement("overskrift", "I am a headline");
$tekst = $dom->createElement("tekst", "I am a text");
$linebreak = $dom->createTextNode(chr(10));


What I want to do is sort of like $dom->documentElement->news->append....
But that doesn't work. Can you help me?

  - Rune

Share this post

Link to post
Share on other sites

I wrote something like this, but in a existing xml file, here is the code:

function create_element($element, $content, $attribute, $attr_val) {
global $xml;
$elem = $xml->create_element($element);
if ($attribute != '') {
$elem->set_attribute($attribute, $attr_val);
return $elem;

$xml = domxml_open_file($path_to_file);
$Root = $xml->document_element();
$new_element = $xml->create_element("comment");
        //I set two attributes for the new element
        //Append two new childs for the new component (create_element is a function that returns the new created component, look upper)
        //This thing can help you to add a new element where you want
$new_element->append_child(create_element("contr", $_POST['contributor'], '', ''));
$new_element->append_child(create_element("string", $_POST['comment'], '', ''));
$nodes = $Root -> child_nodes();
foreach ($nodes as $subNode) {
if ($subNode -> node_name() == 'comments')
$subNode -> append_child($new_element);//here you append the new element to what element you want

The structure of the xml file is:
    //here is where I add the new element
    <comment date="" time="">

Hope it helps;

Share this post

Link to post
Share on other sites


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.