appending a new node

Discussion in 'XML' started by Kleist, Sep 30, 2005.

  1. Kleist

    Kleist Guest

    Hello,

    I use DOM XML PHP functions to build a document. Is it
    possible to insert a new node as sibling right after the
    selected by xpath node? There is a function insert_before()
    but it seems that it appends a child?

    TIA,

    K.
    Kleist, Sep 30, 2005
    #1
    1. Advertising

  2. Kleist wrote:


    > I use DOM XML PHP functions to build a document. Is it possible to
    > insert a new node as sibling right after the selected by xpath node?
    > There is a function insert_before() but it seems that it appends a child?


    In PHP 5 it should work alike
    node->parentNode->insertBefore(newNode, node->nextSibling);

    as in the following complete example

    <?php
    $xmlDocument = new DOMDocument('1.0', 'UTF-8');
    $loaded = $xmlDocument->loadXML('
    <gods>
    <god name="Kibo" />
    <god name="Xibo" />
    </gods>');

    $newGod = $xmlDocument->createElement('god');
    $newGod->setAttribute('name', 'Maho');

    $xpathEvaluator = new DOMXPath($xmlDocument);
    $oldGod = $xpathEvaluator->query('/gods/god[@name = "Kibo"]',
    $xmlDocument)->item(0);
    if ($oldGod != NULL) {
    $oldGod->parentNode->insertBefore($newGod, $oldGod->nextSibling);
    }

    header('Content-Type: application/xml');
    echo $xmlDocument->saveXML();
    ?>

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Sep 30, 2005
    #2
    1. Advertising

  3. Kleist

    Kleist Guest

    Martin Honnen wrote:

    >
    > In PHP 5 it should work alike



    Unfortunally I have only PHP 4.3.1 on my server, and this
    will not do. But anyway thanks for replay
    Kleist, Sep 30, 2005
    #3
  4. Kleist wrote:
    > Unfortunally I have only PHP 4.3.1 on my server, and this
    > will not do. But anyway thanks for replay
    >


    The following looks a bit cumbersome but appears to do the trick:

    <?php

    if (!$dom = domxml_open_mem('<doc><child id="0" /><child id="2" /></doc>'))
    {
    echo "Error while parsing the document\n";
    exit;
    }

    $xpath = xpath_new_context($dom);
    $root = $dom->document_element();

    $element = $dom->create_element('child');
    $element->set_attribute('id', 1);

    $nodeset = xpath_eval($xpath, '//child[@id=0]');
    $node = $nodeset->nodeset[0];

    if ($sibling = $node->next_sibling()) {
    $root->insert_before($element, $sibling);
    } else {
    $node->append_sibling($element);
    }

    echo "<pre>";
    $xmlfile = $dom->dump_mem();
    echo htmlentities($xmlfile);
    echo "</pre>";

    ?>


    JW
    Janwillem Borleffs, Oct 2, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wolf
    Replies:
    0
    Views:
    550
  2. Replies:
    0
    Views:
    1,442
  3. Tjerk Wolterink
    Replies:
    2
    Views:
    1,418
    Dimitre Novatchev
    Aug 24, 2006
  4. remlostime
    Replies:
    4
    Views:
    352
    Thomas J. Gritzan
    Sep 5, 2008
  5. Geagleeye

    how to creating new node inside a xml node

    Geagleeye, Aug 2, 2006, in forum: ASP General
    Replies:
    2
    Views:
    197
    Geagleeye
    Aug 3, 2006
Loading...

Share This Page