Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 17th, 2009, 06:57 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default CREATE JS OBJECT BASED ON XML

Hi,

I am trying to adapt a piece of code I found on the internet as part of a tutorial to transform an xml into a javascript object but am struggling to get it to read attributes as well.

Currently, it only reads node values but I would like it to read attribute values as well and add the attribute values to the object.

That is how I call the function below. I pass an empty object and an xml node

var ret = new Object();

var xmlElem = 'This is an xml document that looks like this:'

<AnchorDate>
<Month value="2">Month</Month>
<Year value="2007">Year</Year>
</AnchorDate>

res = setPropertiesRecursive(ret, xmlElem)

Then I can use the object like this:

res[i].Month
res[i].Year

But I need to have access to the attribute values as well like this:

res[i].Month.value
res[i].Year.value


I would appreciate very much if someone could help me to find a solution.

Cheers

C

function setPropertiesRecursive(obj, node) { if (node.childNodes.length > 0) { for (var i = 0; i < node.childNodes.length; i++) { if (node.childNodes[i].nodeType == 1 && node.childNodes[i].firstChild) { // If node has only one child // set the obj property to the value of the node if (node.childNodes[i].childNodes.length == 1) { obj[node.childNodes[i].tagName] = node.childNodes[i].firstChild.nodeValue; } // Otherwise this obj property is an array // Recurse to set its multiple properties else { obj[node.childNodes[i].tagName] = []; // Call recursively -- rinse and repeat // ============== setPropertiesRecursive( obj[node.childNodes[i].tagName], node.childNodes[i]); } } } }}
 
Old September 17th, 2009, 07:19 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default CREATE JS OBJECT BASED ON XML

Sorry, here is a more readable version of the function that got messed up in my last post.

Code:
function setPropertiesRecursive(obj, node) 
{
    if (node.childNodes.length > 0) 
    {
        for (var i = 0; i < node.childNodes.length; i++) 
        {
            if (node.childNodes[i].nodeType == 1 &&
                  node.childNodes[i].firstChild) 
            {
                // If node has only one child
                // set the obj property to the value of the node
                if (node.childNodes[i].childNodes.length == 1) 
                {
                    obj[node.childNodes[i].tagName] =
                        node.childNodes[i].firstChild.nodeValue;
                }
                // Otherwise this obj property is an array
                // Recurse to set its multiple properties
                else 
                {
                    obj[node.childNodes[i].tagName] = [];
                    // Call recursively -- rinse and repeat
                    // ==============
                    setPropertiesRecursive(
                        obj[node.childNodes[i].tagName],
                        node.childNodes[i]);
                }
            }
        }
    }
}





Similar Threads
Thread Thread Starter Forum Replies Last Post
CANNOT CREATE A DATAROW OBJECT FROM A DATASET OBJECT brucechess BOOK: Beginning VB.NET Databases 2 March 20th, 2009 10:14 AM
help to create dynamic listbox using xslt & JS kramsengg XSLT 7 June 10th, 2008 05:00 AM
XSLT in IE: adding js object to a stylesheet Vx BOOK: Professional Ajax ISBN: 978-0-471-77778-6 2 August 22nd, 2006 11:00 AM
Filtering XML data based on differnt XML ahmed123 XSLT 5 August 11th, 2006 09:15 AM
create a Line object ,Box object in CR at Runtime? thanhnt Pro VB 6 1 May 16th, 2005 06:51 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.