...

View Full Version : Retrieve specific values in a element in XML DOM



Beth Lee Chan
11-05-2004, 08:27 PM
Using Javascript code below:
var oxml = document.getElementById("xmlfile");
var oDom = oxml.XMLDocument;
var xy=oDom.getElementsByTagName("Name");

I was able to produce the below XML:

<root>
<Name Id=\"174\" userid=\"A2 \" level=\"1\" selected=\"1\"/>
<Name Id=\"175\" userid=\"A5 \" level=\"1\" selected=\"1\"/>
<Name Id=\"183\" userid=\"A9 \" level=\"1\" selected=\"0\"/>
<Name Id=\"110\" userid=\"B1 \" level=\"1\" selected=\"0\"/>
<Name Id=\"116\" userid=\"B16\" level=\"2\" selected=\"0\"/>
<Name Id=\"109\" userid=\"B25\" level=\"2\" selected=\"0\"/>
<Name Id=\"120\" userid=\"C29\" level=\"1\" selected=\"0\"/>
<Name Id=\"133\" userid=\"D03\" level=\"1\" selected=\"0\"/>
<Name Id=\"132\" userid=\"D3 \" level=\"1\" selected=\"0\"/>
</root>

What code do I need to write to traverse above XML code to retrieve those userid value only when the selected=1???? And display the userid "A2" and "A5" with window.confirm in Javascript?????

Thanks for all your help. Any suggestion is appreciate, I'm fairly new to the DOM world.

Thanks
Beth

liorean
11-05-2004, 08:47 PM
Well, if you want the list of elements that have a certain attribute set to a certain value, you can create an array containing those elements like this:
var
i=0,
elms=[],
elm;
while(elm=xy.item(i++))
if('1'==elm.getAttribute('selected'))
elms.push(elm);If you don't want that array but instead wants to directly handle them in some way, such as building the string for the confirm for the 'userid' attributes, you can place that code instead of elms.push(elm);.

Beth Lee Chan
11-06-2004, 04:10 AM
Thanks for your help. Your code really helped me.

Beth Lee Chan
11-09-2004, 02:31 PM
Another question for you. When I display the users id in the Javascript message prompt it display the user id with 2 characters including the extra space after the user id i.e. 'A2 ', 'A5 '. How do I go about in Javascript to remove the extra space after the user id???

liorean
11-09-2004, 05:45 PM
str=str.replace(/\s*$/,'');



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum