XML/DOM deleting childnodes/hierarchy

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tsuki@gmx.li

    XML/DOM deleting childnodes/hierarchy

    Hello,

    I have a problem concerning DOM and XML.

    The structure of my xml is the following:
    <?xml version="1.0" encoding="utf-8"?>
    <website>
    <language1>
    <seite>
    <bereich1>
    <text></text>
    <link>
    <link></link>
    </link>
    <link>
    <link></link>
    </link>
    <link>
    </link>
    </bereich1>
    <bereich2>
    </bereich2>
    </seite>
    </language1>
    </website>

    if I change and save the contents of the link-tags, there is no
    problem.
    (to change the value of a link, I simply select them by getElementsByID
    and loop them in a for-loop).

    But if I have to delete one of them, it seems that the hierarchy
    somehow causes trouble, a "not found error" occurs.
    If there is only a list of links, which doesn't contain other links,
    the deleting works.

    My question: why does the principle of the for-looping work with
    changing the values of the link, but doesnt work when i want to delete
    them?

    The code for deleting:

    $DeleteIt=(int) $DeleteIt;
    if($Sprache=="D eutsch") $parent =
    $dom->getElementsByT agName('languag e1')->item(0);
    else if ($Sprache=="Eng lisch") $parent =
    $dom->getElementsByT agName('languag e2')->item(0);
    $parent2=$paren t->getElementsByT agName('seite')->item($Seite) ;
    $parent3=$paren t2->getElementsByT agName('bereich 1')->item(0);
    $toDelete=$pare nt3->getElementsByT agName('link')->item($DeleteIt );
    $okay = $parent3->removeChild($t oDelete);

    if I replace the last two lines by:

    $toDelete=$pare nt3->getElementsByT agName('link')->item($DeleteIt )->nodeValue;
    echo $toDelete;

    the value of the link that has to be deleted is given - this works.


    Can anyone help me solving the problem or explaining why the one works
    and the other doesn't?

  • p.lepin@ctncorp.com

    #2
    Re: XML/DOM deleting childnodes/hierarchy


    tsuki@gmx.li wrote:
    <?xml version="1.0" encoding="utf-8"?>
    <website>
    <language1>
    <seite>
    <bereich1>
    <text></text>
    <link>
    <link></link>
    </link>
    <link>
    <link></link>
    </link>
    <link>
    </link>
    </bereich1>
    <bereich2>
    </bereich2>
    </seite>
    </language1>
    </website>
    [...]
    But if I have to delete one of them, it seems that the
    hierarchy somehow causes trouble, a "not found error"
    occurs. If there is only a list of links, which doesn't
    contain other links, the deleting works.
    [...]
    $DeleteIt=(int) $DeleteIt;
    if($Sprache=="D eutsch") $parent =
    $dom->getElementsByT agName('languag e1')->item(0);
    else if ($Sprache=="Eng lisch") $parent =
    $dom->getElementsByT agName('languag e2')->item(0);
    $parent2=$paren t->getElementsByT agName('seite')->item
    ($Seite);
    $parent3=$paren t2->getElementsByT agName('bereich 1')->
    item(0);
    $toDelete=$pare nt3->getElementsByT agName('link')->
    item($DeleteIt) ;
    $okay = $parent3->removeChild($t oDelete);
    >
    if I replace the last two lines by:
    >
    $toDelete=$pare nt3->getElementsByT agName('link')->item(
    $DeleteIt)->nodeValue;
    echo $toDelete;
    >
    the value of the link that has to be deleted is given -
    this works.
    >
    Can anyone help me solving the problem or explaining why
    the one works and the other doesn't?
    I haven't run any tests, but: removeChild() is
    removeChild(), not removeDescendan t(). So if you're trying
    to delete a link node that is a descendant of a bereich1
    node, but NOT a child of that same bereich1 node, it
    wouldn't (and shouldn't) work. What you probably need is
    something along these lines (not tested, and I don't
    remember the DOM bindings in the PHP5 DOM XML module off
    the top of my head):

    $okay = $toDelete -parentNode ->
    removeChild ( $toDelete ) ;

    --
    Pavel Lepin

    Comment

    Working...