473,394 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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->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!
Sep 23 '06 #1
1 10697
Hi Rebecca,
You're getting the fatal error because you're trying to delete a child
which doesn't exist on the <xml_thingnode. <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 <languagesnode 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--
Sep 23 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: JehanNYNJ | last post by:
I need to clear out the HTML contents of a <span> element and then re-add that element as a blank span. Here is the code... var elem = document.getElementById(spanId); var deletedElem =...
2
by: Stewart | last post by:
Originally posted in comp.lang.javascript: Newsgroups: comp.lang.javascript From: "Stewart" Date: 23 Aug 2005 02:50:04 -0700 Local: Tues, Aug 23 2005 10:50 am Subject: FireFox, RemoveChild,...
5
by: Markus Stehle | last post by:
Hi all! I want to remove a node from a nodelist by using RemoveChild. I'm using the following code: XmlNode root = Xml.DocumentElement; XmlNodeList items = root.SelectNodes("Item"); XmlNode...
6
by: snazzy | last post by:
I have a problem where using innerHTML to rewrite a DIV or removeChild to kill a DIV that either of them, if excuted before the page is done loading will stop the page in its tracks. ...
6
by: VK | last post by:
I must be missing something very obvious, but my nightly head doesn't work anymore. Press "Insert" button to add <insnodes after each <br>. Now press "Delete" - only even <insare being removed....
5
by: tader | last post by:
Hi, so i got script like that var div_box = document.createElement("div"); div_box.setAttribute("id", "music_box"); div_box.style.top = (Number(cords) - 10) + "px"; div_box.style.left =...
10
by: r_ahimsa_m | last post by:
Hello, On index.html page I have a table with id="property_fields". <table id="property_fields" name="property_fields" border="0"> It contains set of rows with the following IDs: var...
5
by: r_ahimsa_m | last post by:
Hello, I am lerning HTML/CSS/JavaScript. I created HTML page with table "property_fields" containing 24 rows ('tr' elements). I want to remove last 23 rows: var table =...
7
by: ChuckB via WebmasterKB.com | last post by:
Hello. I'm trying to remove an additional radio selection if another radio button is selected. There's suppose to be two additional selections if one particular button is chosen. Then if the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.