Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Nov 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Retrieve specific values in a element in XML DOM

    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
    Last edited by Beth Lee Chan; 11-09-2004 at 02:26 PM.

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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:
    Code:
    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);.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    New to the CF scene
    Join Date
    Nov 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your help. Your code really helped me.

  • #4
    New to the CF scene
    Join Date
    Nov 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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???

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Code:
    str=str.replace(/\s*$/,'');
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •