1. I've started a massive SPAM purge, any accounts that have spammed are being removed, also for a short period new user signups are disallowed until the SPAM cleaning is finished.

Reading a vaild XML Content String

Discussion in 'Java Script/AJAX' started by neta, Aug 15, 2007.

Thread Status:
Not open for further replies.
  1. neta

    neta New Member

    Joined:
    Aug 15, 2007
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    using getElementsByTagName technique
    Code (Text):

    <script>
    var xmlstring = '<?xml version=\"1.0\"?>\
    <shoppingcart date="14-10-2005" total="123.45">\
        <item code="12345">\
            <name>Widget</name>\
            <quantity>1</quantity>\
        </item>\
        <item code="54321">\
            <name>Another Widget</name>\
            <quantity>2</quantity>\
        </item>\
    </shoppingcart>';

    // convert the string to an XML object
    var xmlobject = (new DOMParser()).parseFromString(xmlstring, "text/xml");
    // get the XML root item
    var root = xmlobject.getElementsByTagName('shoppingcart')[0];
    var date = root.getAttribute("date");
    alert("shoppingcart date=" + date);

    var items = root.getElementsByTagName("item");
    for (var i = 0 ; i < items.length ; i++) {
        // get one item after another
        var item = items[i];
        // now we have the item object, time to get the contents
        // get the name of the item
        var name = item.getElementsByTagName("name")[0].firstChild.nodeValue;
        // get the quantity
        var quantity = item.getElementsByTagName("quantity")[0].firstChild.nodeValue;
        alert("item #" + i + ": name=" + name + " quantity=" + quantity);
    }
    </script>
     
    Loading...
  2. midlandi

    midlandi Can you smell that?

    Joined:
    Jan 22, 2007
    Messages:
    4,722
    Likes Received:
    44
    Trophy Points:
    48
    Location:
    Uk
    It would be usefull to add an explanation of the use and implementation if the code you have shown.
  3. FromF2Last

    FromF2Last New Member

    Joined:
    Aug 21, 2007
    Messages:
    146
    Likes Received:
    0
    Trophy Points:
    0
    yeppo, deffo, i dont suppose anyone watched big brother did they?
Thread Status:
Not open for further replies.

Share This Page