homepage Welcome to WebmasterWorld Guest from 54.235.16.159
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / XML Development
Forum Library, Charter, Moderators: httpwebwitch

XML Development Forum

    
How to access xml attributes using Javascript?
tjhorne




msg:701826
 3:28 pm on Mar 28, 2005 (gmt 0)

I am trying to access the values of attributes in my XML document with Javascript, here is an example of my XML file:

<Objects>
<User id="EEB5604C" name="C: SPaget">
<Email Value="spaget@mediageneral.com"/>
</Objects>

Please let me know if you have any ideas...

Thanks.

 

brucec




msg:701827
 5:51 pm on Mar 28, 2005 (gmt 0)

You will need to use the DOM for attributes. Try the following syntax:

document.getElementByTagname(TAG NAME IN QUOTES).attributes.getNamedItem(ATTRIBUTE NAME IN QUOTES).value;

I know its a pain with the DOM, but it works for me.

For example:
For XML <person name="Bruce">, your javascript would be:

document.getElementByTagname("person").attributes.getNamedItem("name").value;

I tried it on my machine and it works :)

Bruce

tjhorne




msg:701828
 6:46 pm on Mar 28, 2005 (gmt 0)

Could you send me your sample file?

Here is what I have:

<script type="text/javascript" for="window" event="onload">
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("note2.xml")

to.innerText=document.getElementByTagname("person").attributes.getNamedItem("name").text;
</script>

XML File (note2.xml):
<person name="Todd" />

I am just trying to get the name to display in the webpage, any help would be greatly appreciated, thanks again for your help.

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