PDA

View Full Version : removing elements based on their attributes.


mat106
08-17-2004, 11:57 PM
Hi.

I have an xml file of the following form:

<phonebook>
<person id="1">
...child nodes go here...
</person>
<person id="2">
...child nodes go here...
</person>
<person id="3">
...child nodes go here...
</person>
etc...
</phonebook>

What i want to do is to be able to remove any one of the <person> elements based on the value of its id attribute. I intend to do that by entering the id value into a form, assigning the id value to a variable in an .asp file and using the variable in conjuction with XML DOM.

The problem however is that i can't find any reference of how to use removechild() (if that's what i'm supposed to use) in order to remove the child element based on the value of its id attribute. Does anyone how this can be done???

Also, if there's a smarter way of doing what i've mentioned please let me know. Thanks.

brendon99
08-18-2004, 01:39 AM
Well, there are a couple of different ways of tackling this...here's mine. I use javascript, but you should be able to easily change the syntax to suit for your asp page.

// At this stage, get a handle to the xml file
var xmlPhone = new ActiveXObject('MSXML2.FreeThreadedDOMDocument')
xmlPhone.resolveExternals = true;
xmlPhone.preserveWhiteSpace = true;
xmlPhone.validateOnParse = false;
xmlPhone.async = false;
xmlPhone.load('phonebook.xml')

//Get the right id from the file - this will return an array set of "person"
var phone_details = xmlPhone.getElementsByTagName("person")

//Now go through the array, checking against your user_input_id
// Note that this syntax assumes that the user_input_id exists in the file, otherwise you'll need to code some error checking
var i=0
while ({user_input_id} != phone_details.item(i).getAttribute("id") )
i++

// Now we've find them - lets get the child nodes under it
var child_details = phone_details.item(i).getElementsByTagName("????")

// Once again, its returned an array of child details
// Now iterate through them, one at a time, removing them from the person element
for (var j=0;j<child_details.length;j++)
phone_details.item(i).removeChild(child_details.item(j))



Haven't test this, so there may be an error or two (or three!) in there, but it may give you some ideas to handle it

mat106
08-21-2004, 01:59 PM
Thanks brendon99. I've managed to convert it to vbscript and change it a bit to suit what i'm doing and it works fine!

holty
11-11-2005, 11:29 AM
Hi

I have exactly the same problem and would like to delete all the children nodes under an element (just like the person element in this example)

Anyone know how to delete the child nodes using vbscript?

here is the xml:

<jobs>
<vacancy id="1">
<summary>info goes here....</summary>
<date>info goes here....</date>
</vacancy>
<vacancy id="2">
<summary>info goes here....</summary>
<date>info goes here....</date>
</vacancy>
<vacancy id="3">
<summary>info goes here....</summary>
<date>info goes here....</date>
</vacancy>
etc...
</jobs>

So i would like to delete the following:

<vacancy id="2">
<summary>info goes here....</summary>
<date>info goes here....</date>
</vacancy>

Any ideas?