DOM removeChild Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rebecca Tsukalas

    #1

    DOM removeChild Problem

    Hello,

    I have a problem concerning removeChild. This is the XML structure I use with php:

    <xml_thing
    <language1
    <site>bla1</site
    <site>bla2</site
    <site>bla3</site
    </language1
    <language2
    </language2
    </xml_thing

    now, if I want to delete the whole language1 I use the following code:
    $toDelete=$xml_ dom->getElementsByT agName('languag e1')->item(0);
    $okay = $xml_dom->removeChild($t oDelete);


    but if I try to delete a site like that:
    $toDelete=$xml_ dom->getElementsByT agName('site')->item(0);
    $okay = $xml_dom->removeChild($t oDelete);

    i get a Fatal error: Uncaught exception 'DOMException' with message 'Not Found Error'

    with google i found a hint how to solve the problem, it said that the removeChild method refers to the current selected node and that I have to use the method having selected the concerning Node. But how can I do this? Probably amateurish I tried to do it like this way:

    $toDelete=$xml_ dom->getElementsByT agName('languag e1')->item(0);
    $toDelete=$toDe lete->getElementsByT agName('site')->item(0);

    normally I use SimpleXML to handle XML-files, but in SimpleXML there is no way to delete a child so I had to use DOM, which I think is very complicated - however I would be very thankful if someone could explain the above mentioned problem to me. thank you!
  • myloforreal@yahoo.com.au

    #2
    Re: DOM removeChild Problem

    Hi Rebecca,
    You're getting the fatal error because you're trying to delete a child
    which doesn't exist on the <xml_thingnod e. <site is a child of
    <language1>.

    $language1Node = $xmldoc->getElementsByT agName("languag e1")->item(0);
    $firstsiteNode = $language1Node->firstChild;
    $language1Node->removeChild($f irstSiteNode);

    I've been using the DOM Functions for nearly 12 months on a project i'm
    due to release at the end of this month and let me tell you, it's been
    a helluva'n evolution.

    You may find a lot of your code with
    $node->firstChild->nextSibling->nextSibling and stuff like that. It's
    can quite hard to read - consider making several functions called

    removeLanguage( $languagesNode, $lang_name){
    for( $i = 0 ; $i < $languagesNode->length ; $i++ ){
    if( strcmp($languag esNode->item($i)->nodeValue,$lan g_name)==0)
    $languagesNode->removeChild($l anguagesNode->item($i) ;
    }
    }

    What this does is that you've passed in your languages root node and
    the name of the language. On a match of the nodeValue (and the name of
    the language you want to delete) it'll remove that child by doing a
    string comparison.

    A handy function would also be
    getLanguagesNod e($xmldoc){
    return $xmldoc->getElementsByT agName("languag es");
    }

    It may also be handy to review the structure of your document so that
    it looks more like
    <rootNode>
    <languages>
    <language>
    <site>
    <site>
    <site>
    </language>
    <language>
    ...
    ...
    </languages>
    <moreNodes/>
    </rootNode>

    This way, all the languages will be in the <languagesnod e rather than
    calling each node languageX where X is akin to an id.

    Hope this helps,
    Mylo

    P.S. I just found this post by chance whilst waiting for a d/l to
    finish. I've never used google groups and was just curious, so i don't
    know what the normal etiquette is in here, later

    Rebecca Tsukalas wrote:
    Hello,
    >
    I have a problem concerning removeChild. This is the XML structure I use with php:
    >
    <xml_thing>
    <language1>
    <site>bla1</site>
    <site>bla2</site>
    <site>bla3</site>
    </language1>
    <language2>
    </language2>
    </xml_thing>
    >
    now, if I want to delete the whole language1 I use the following code:
    $toDelete=$xml_ dom->getElementsByT agName('languag e1')->item(0);
    $okay = $xml_dom->removeChild($t oDelete);
    >
    >
    but if I try to delete a site like that:
    $toDelete=$xml_ dom->getElementsByT agName('site')->item(0);
    $okay = $xml_dom->removeChild($t oDelete);
    >
    i get a Fatal error: Uncaught exception 'DOMException' with message 'Not Found Error'
    >
    with google i found a hint how to solve the problem, it said that the removeChild method refers to the current selected node and that I have to use the method having selected the concerning Node. But how can I do this? Probably amateurish I tried to do it like this way:
    >
    $toDelete=$xml_ dom->getElementsByT agName('languag e1')->item(0);
    $toDelete=$toDe lete->getElementsByT agName('site')->item(0);
    >
    normally I use SimpleXML to handle XML-files, but in SimpleXML there is no way to delete a child so I had to use DOM, which I think is very complicated - however I would be very thankful if someone could explain the above mentioned problem to me. thank you!
    ------=_NextPart_000_ 0009_01C6DF04.B 9E25940
    Content-Type: text/html; charset=iso-8859-1
    Content-Transfer-Encoding: quoted-printable
    X-Google-AttachSize: 3098
    >
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML><HEAD>
    <META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
    <META content="MSHTML 6.00.2900.2963" name=GENERATOR>
    <STYLE></STYLE>
    </HEAD>
    <BODY>
    <DIV><FONT face=Arial size=2>Hello,</FONT></DIV>
    <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
    <DIV><FONT face=Arial size=2>I have a problem concerning removeChild.&nb sp;This
    is the&nbsp;XML structure I use with php:</FONT></DIV>
    <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
    <DIV><FONT face=Arial size=2>&lt;xml_ thing&gt; <BR>&nbsp;&nbsp ;&lt;language1& gt;
    <BR>&nbsp;&nbsp ;&nbsp;&nbsp;&l t;site&gt;bla1& lt;/site&gt;
    <BR>&nbsp;&nbsp ;&nbsp;&nbsp;&l t;site&gt;bla2& lt;/site&gt;
    <BR>&nbsp;&nbsp ;&nbsp;&nbsp;&l t;site&gt;bla3& lt;/site&gt;
    <BR>&nbsp;&nbsp ;&lt;/language1&gt; <BR>&nbsp;&nbsp ;&lt;language2& gt;
    <BR>&nbsp;&nbsp ;&lt;/language2&gt; <BR>&lt;/xml_thing&gt;&n bsp; </FONT></DIV>
    <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
    <DIV><FONT><FON T face=Arial size=2>now, if I want to delete the whole language1
    I use the following code:</FONT></DIV>
    <DIV><FONT face=Arial><FON T
    size=2><STRONG> $toDelete=$xml_ dom-&gt;getElements ByTagName('lang uage1')-&gt;item(0);<BR >$okay
    = $xml_dom-&gt;removeChild ($toDelete);</STRONG><BR></FONT></FONT></DIV>
    <DIV><FONT face=Arial size=2></FONT></FONT><FONT
    color=#0000bb></FONT>&nbsp;</DIV>
    <DIV><FONT face=Arial size=2>but if I try to delete a site like
    that:</FONT></DIV>
    <DIV><FONT face=Arial
    size=2><STRONG> $toDelete=$xml_ dom-&gt;getElements ByTagName('site ')-&gt;item(0);<BR >$okay
    = $xml_dom-&gt;removeChild ($toDelete);</STRONG></FONT></DIV>
    <DIV><STRONG><F ONT face=Arial size=2></FONT></STRONG>&nbsp;</DIV>
    <DIV><FONT face=Arial size=2>i get a <STRONG>Fatal error: Uncaught exception
    'DOMException' with message 'Not Found Error'</STRONG</FONT></DIV>
    <DIV><STRONG><F ONT face=Arial size=2></FONT></STRONG>&nbsp;</DIV>
    <DIV><FONT face=Arial size=2>with google i found a hint how to solve the
    problem, it said that the removeChild method refers to the current selected node
    and that I have to use the method having selected the concerning Node. But how
    can I do this? Probably amateurish&nbsp ;I tried to&nbsp;do it like this
    way:</FONT></DIV>
    <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
    <DIV><STRONG><F ONT face=Arial
    size=2>$toDelet e=$xml_dom-&gt;getElements ByTagName('lang uage1')-&gt;item(0); </FONT></STRONG></DIV>
    <DIV><STRONG><F ONT face=Arial
    size=2>$toDelet e=$toDelete-&gt;getElements ByTagName('site ')-&gt;item(0); </FONT></STRONG></DIV>
    <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
    <DIV><FONT face=Arial size=2>normally &nbsp;I use SimpleXML to handle XML-files,
    but in SimpleXML there is no way to delete a child so I had to use DOM, which I
    think is very complicated - however I would be very thankful if someone could
    explain the above mentioned problem to me. thank you!</FONT></DIV></BODY></HTML>
    >
    ------=_NextPart_000_ 0009_01C6DF04.B 9E25940--

    Comment

    Working...