homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / XML Development
Forum Library, Charter, Moderators: httpwebwitch

XML Development Forum

Get Attributes with javascript
Problem accessing attributes with javascript

 6:10 am on Jun 13, 2008 (gmt 0)

Hi I'm hoping someone can help me here i'm trying to access the attributes in the following XML file named data.xml:

<?xml version="1.0" ?>
<employee id="001" sex="M" email="Prem@igy.com">Premshree Pillai
<employee id="002" sex="F" email="KS32@alltoeasy.net">Kumar Singh
<employee id="003" sex="M" email="Ran Kapoor">Ran Kapoor

i can load the file and i can get to the employee element but when i try to get the attributes i get the following error in firebug:

xmlDoc.getElementsByTagName("employee").attributes has no properties

here is my javascript code:

var xmlDoc=null;
if (window.ActiveXObject){//IE code
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
else {// code for mozilla, firefox, opera, etc.
xmlDoc.onload = printData;

//displaying the results
function printData(){
var employeeNode = xmlDoc.getElementsByTagName("employee");
var idNode = xmlDoc.getElementsByTagName("employee").attributes.getNamedItem("id").value;
var sexNode = xmlDoc.getElementsByTagName("employee").attributes.getNamedItem("sex").value;
var emailNode = xmlDoc.getElementsByTagName("employee").attributes.getNamedItem("email").value;
var imageNode = xmlDoc.getElementsByTagName("image");

please help



 1:13 pm on Jun 13, 2008 (gmt 0)

getElementsByTagName("employee") should return an array of all the matched elements. An array doesn't have any attributes, so try this instead:



 1:15 pm on Jun 13, 2008 (gmt 0)

I didn't test this, but it's probably closer to what you want. note the for loop that iterates through the array constructed by getElementsByTagName

//displaying the results
function printData(){
var employeeNodeArray = xmlDoc.getElementsByTagName("employee");
for(employeeNode in employeeNodeArray){
var idNode = employeeNode.attributes.getNamedItem("id").value;
var sexNode = employeeNode.attributes.getNamedItem("sex").value;
var emailNode = employeeNode.attributes.getNamedItem("email").value;

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / XML Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved