SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Parsing single xml nodes

    Running into a stumbling block. How would i parse singular nodes against what id's they are and retrieving their values? here's an example xml block (how to parse the item nodes)

    Code:
    <root>
    
    <container>
    <item id="first" value="this is the value" />
    <item id="second" value="different value" />
    <item id="third" value="last value" />
    </container>
    
    </root>

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Depends on the language you are using and what kind of object you have your xml dom loaded into.

    For example using microsoft's XMLDOM object you could do
    var myNode = dom.selectSingleNode("//root/container/item[@id='second']")

    to get the second item node. Other xml handling objects do things differently.

  3. #3
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    once you load the xml into an xml document, accessing the nodes is like accessing html nodes. You can use getElementsByTagName etc.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>Untitled Document</title>
            <link rel="stylesheet" type="text/css" media="all" />
            <style type="text/css"></style>
            <script type="text/javascript">
                function loadXML(text) {
                    try {
                        var xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
                            xmlDoc.async = "false";
                            xmlDoc.loadXML(text);
                    } catch(e) {
                        try {
                            var parser = new DOMParser();
                            var xmlDoc = parse.parseFromString(text, "text/xml");
                        } catch(e) {
                            alert(e.message);
                            return;
                        }
                    }
                    return xmlDoc;
                }
    
                var doc = null;
    
                window.onload = function() {
                    var txt = '<root><container><item id="first" value="this is the value" /><item id="second" value="different value" /><item id="third" value="last value" /></container></root>';
                    doc = loadXML(txt);
    
                    var items = doc.getElementsByTagName('item');
                    alert(items.length + ' items');
                    for(var i = 0; i < items.length; i++) {
                        alert('ID: ' + items[i].getAttribute('id') + '\nValue: ' + items[i].getAttribute('value'));
                    }
                }
            </script>
        </head>
        <body>
            
        </body>
    </html>


  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    @gRoberts: I think he was after a way of finding only the self-closed elements
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yea, i'm having issues with getting the self closed objects, and only grabbing the value that matches the id. I'm using xpath as the means to parse the xml i'm receiving.

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    About the best you can do is with the hasChildNodes() method.
    You won't be able to tell the difference between a self-closing element and an element with no children, but it's about the best solution there is.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure what stage you are at with this, but assuming you have loaded the XML into a DOMDocument that in some way supports XPath (should be possible in Mozilla using DOM.evaluate??) I don't see why you can't use the xpath query I posted to get your item, and from there get the value attribute.

    thing.xpathevaluator("//root/container/item[@id='second']").getAttribute("value");

    something like that?

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    I think that he's wanting to restrict the container elements to only the self-closing ones. Why, I'm not sure about.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

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