[thelist] Creating objects with JS, XML, and the DOM DOM DOM...

Ben Joyce ben.joyce at gmail.com
Fri Aug 10 05:03:45 CDT 2007


hello list.

ok, cut to the chase...

I have some XML

<?xml version="1.0"?>
<NewDataSet>
	<Table>
		<PROFILE_ID>1</PROFILE_ID>
		<FIRST_NAME>ben</FIRST_NAME>
	</Table>
</NewDataSet>

and a JS object:

function User()
{
    this.FirstName = "";
    this.Surname = "";
}

So, I load the XML into a document object and populate the user object from that

var node=xml.getElementsByTagName('FIRST_NAME');
User.FirstName=node[0].childNodes[0].text;

Then, I can use User.FirstName to access my data rather than repeating
the whole "getElementsByTagName" thing each time.  However, this seems
a bit long-winded... I'm guessing there might be an easier way to read
the value of the FIRST_NAME from the XML object rather the User object
- just not sure how!

Do I even need to bother with a User object?  Would I be better off
just storing the XML object and getting data direct from there?

Any thoughts appreciated.

Ben

-- 
http://www.flickr.com/photos/benbenbenbenben



More information about the thelist mailing list