You are here: Re: DOM removeChild Problem « PHP Programming Language « IT news, forums, messages
Re: DOM removeChild Problem

Posted by myloforreal on 09/23/06 13:17

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

$language1Node = $xmldoc->getElementsByTagName("language1")->item(0);
$firstsiteNode = $language1Node->firstChild;
$language1Node->removeChild($firstSiteNode);

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($languagesNode->item($i)->nodeValue,$lang_name)==0)
$languagesNode->removeChild($languagesNode->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
getLanguagesNode($xmldoc){
return $xmldoc->getElementsByTagName("languages");
}

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 <languages> node 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->getElementsByTagName('language1')->item(0);
> $okay = $xml_dom->removeChild($toDelete);
>
>
> but if I try to delete a site like that:
> $toDelete=$xml_dom->getElementsByTagName('site')->item(0);
> $okay = $xml_dom->removeChild($toDelete);
>
> 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->getElementsByTagName('language1')->item(0);
> $toDelete=$toDelete->getElementsByTagName('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.B9E25940
> 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.&nbsp;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;&lt;site&gt;bla1&lt;/site&gt;
> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;site&gt;bla2&lt;/site&gt;
> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;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;&nbsp; </FONT></DIV>
> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
> <DIV><FONT><FONT face=Arial size=2>now, if I want to delete the whole language1
> I use the following code:</FONT></DIV>
> <DIV><FONT face=Arial><FONT
> size=2><STRONG>$toDelete=$xml_dom-&gt;getElementsByTagName('language1')-&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;getElementsByTagName('site')-&gt;item(0);<BR>$okay
> = $xml_dom-&gt;removeChild($toDelete);</STRONG></FONT></DIV>
> <DIV><STRONG><FONT 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><FONT 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><FONT face=Arial
> size=2>$toDelete=$xml_dom-&gt;getElementsByTagName('language1')-&gt;item(0);</FONT></STRONG></DIV>
> <DIV><STRONG><FONT face=Arial
> size=2>$toDelete=$toDelete-&gt;getElementsByTagName('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.B9E25940--

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация