homepage Welcome to WebmasterWorld Guest from 54.161.192.130
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

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
CosmicDistortion

5+ Year Member



 
Msg#: 3673706 posted 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" ?>
<customer>
<employee id="001" sex="M" email="Prem@igy.com">Premshree Pillai
<image>employee001.jpg</image>
</employee>
<employee id="002" sex="F" email="KS32@alltoeasy.net">Kumar Singh
<image>employee002.jpg</image>
</employee>
<employee id="003" sex="M" email="Ran Kapoor">Ran Kapoor
<image>employee003.jpg</image>
</employee>
</customer>

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");
xmlDoc.load("data.xml");
printData();
}
else {// code for mozilla, firefox, opera, etc.
xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.load("data.xml");
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

 

httpwebwitch

WebmasterWorld Administrator httpwebwitch us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3673706 posted 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:

xmlDoc.getElementsByTagName("employee")[0].attributes
xmlDoc.getElementsByTagName("employee")[1].attributes
xmlDoc.getElementsByTagName("employee")[2].attributes
xmlDoc.getElementsByTagName("employee")[3].attributes
etc.

httpwebwitch

WebmasterWorld Administrator httpwebwitch us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3673706 posted 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