473,406 Members | 2,371 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,406 software developers and data experts.

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=="Deutsch") $parent =
$dom->getElementsByTagName('language1')->item(0);
else if ($Sprache=="Englisch") $parent =
$dom->getElementsByTagName('language2')->item(0);
$parent2=$parent->getElementsByTagName('seite')->item($Seite);
$parent3=$parent2->getElementsByTagName('bereich1')->item(0);
$toDelete=$parent3->getElementsByTagName('link')->item($DeleteIt);
$okay = $parent3->removeChild($toDelete);

if I replace the last two lines by:

$toDelete=$parent3->getElementsByTagName('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?

Oct 18 '06 #1
1 1957

ts***@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=="Deutsch") $parent =
$dom->getElementsByTagName('language1')->item(0);
else if ($Sprache=="Englisch") $parent =
$dom->getElementsByTagName('language2')->item(0);
$parent2=$parent->getElementsByTagName('seite')->item
($Seite);
$parent3=$parent2->getElementsByTagName('bereich1')->
item(0);
$toDelete=$parent3->getElementsByTagName('link')->
item($DeleteIt);
$okay = $parent3->removeChild($toDelete);

if I replace the last two lines by:

$toDelete=$parent3->getElementsByTagName('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 removeDescendant(). 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

Oct 18 '06 #2

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

Similar topics

10
by: AdamG | last post by:
I am trying hard for days now to add and delete rows to a table. I could really use some help... Each row contains two buttons (images) - 'add row' and 'delete row'. When the user clicks add...
1
by: ason | last post by:
Hi, I just tried to replace a single XmlNode with several ChildNodes. When doing this with the following code i found out that if you get the ChildNodes with 'ChildNodes' - property the nodes...
1
by: chris yoker via DotNetMonster.com | last post by:
hiya, I have a function that replaces the nodenames in an xmlFile. I use a while loop.The problem is, if the element has no childNodes, then it isn't replaced.I want ALL the elements to be...
5
by: Patrick Vanden Driessche | last post by:
Hi All, I'm currently writing an in-house Form validation framework (WinForms) which is based on 'Component'-inheriting object. So basically, I have a small hierarchy. FormValidator +--...
3
by: Q1tum | last post by:
Hi all, I have a problem with getting the amount of childs in a XML structure, the strucure is somewhat like the following: <?xml version="1.0" encoding="iso-8859-1"?> <cms> <num>21</num>...
1
by: yawnmoth | last post by:
Given an element ID, is there a way to figure out what index one would need to use in the parentNode's childNodes array to get at that element? For example... <body> <div id="parent"> <div...
5
by: Moses | last post by:
HI The Value for childNodes.length differs with mozilla and IE Is it problem with my coding..... I could not under stood............. The following is the details
2
by: willyWEB66 | last post by:
Hi everyone, I have this code in javascript where the XML file is loaded and displayed to an html using XSLT. It works fine in IE but not in Firefox. My problem is in the looping to the...
2
cameokid
by: cameokid | last post by:
Hi, Here is the problem which i am facing. I am trying to delete rows (using nodes) containing XML data. I am doing this to add new set of data from XML file by using a dropdown selection. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.