/    Sign up×
Community /Pin to ProfileBookmark

Java script to read a value from XML file

Hi

I need to read the value of tag(<tr:Name>) which occurs in below xml, multiple times. Can anyone help me with java script code to read an XML file and pull all the values corresponding to a particular tag((<tr:Name>)). Below is the xml file.

[code]
<?xml version=”1.0″ encoding=”UTF-8″?>
<tr:TaskReport xmlns:tr=”http://www.xyz.com/2009/TaskReport”>
<tr:FormatVersion>1</tr:FormatVersion>
<tr:DefaultProfileDB>
<tr:Info>
<tr:Path>C:Documents and SettingsAll UsersApplication DataTest</tr:MutatorPath>
<tr:Name>test1</tr:Name>
</tr:Info>
<tr:Info>
<tr:Path>C:Documents and SettingsAll UsersApplication DataTest</tr:MutatorPath>
<tr:Name>test2</tr:Name>
</tr:Info>
<tr:DefaultProfileDB>
<tr:LocalProfileDB>
<tr:Info>
<tr:Path>C:Documents and SettingsAll UsersApplication DataLocalTest</tr:MutatorPath>
<tr:Name>test3</tr:Name>
</tr:Info>
<tr:Info>
<tr:Path>C:Documents and SettingsAll UsersApplication DataLocalTest</tr:MutatorPath>
<tr:Name>test4</tr:Name>
</tr:Info>
</tr:LocalProfileDB>
</tr:TaskReport >
[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KorDec 15.2009 — That looks like an XML transformation schema not an XML file. It does not look like XSLT neither...
Copy linkTweet thisAlerts:
@rnd_meDec 15.2009 — Hi

I need to read the value of tag(<tr:Name>) which occurs in below xml, multiple times. Can anyone help me with java script code to read an XML file and pull all the values corresponding to a particular tag((<tr:Name>)). Below is the xml file.[/QUOTE]




[CODE]x='<?xml version="1.0" encoding="UTF-8"?>
<tr:TaskReport xmlns:tr="http://www.xyz.com/2009/TaskReport">
<tr:FormatVersion>1</tr:FormatVersion>
<tr:DefaultProfileDB>
<tr:Info>
<tr:Path>C:\Documents and Settings\All Users\Application Data\Test</tr:MutatorPath>
<tr:Name>test1</tr:Name>
</tr:Info>
<tr:Info>
<tr:Path>C:\Documents and Settings\All Users\Application Data\Test</tr:MutatorPath>
<tr:Name>test2</tr:Name>
</tr:Info>
<tr:DefaultProfileDB>
<tr:LocalProfileDB>
<tr:Info>
<tr:Path>C:\Documents and Settings\All Users\Application Data\Local\Test</tr:MutatorPath>
<tr:Name>test3</tr:Name>
</tr:Info>
<tr:Info>
<tr:Path>C:\Documents and Settings\All Users\Application Data\Local\Test</tr:MutatorPath>
<tr:Name>test4</tr:Name>
</tr:Info>
</tr:LocalProfileDB>
</tr:TaskReport >'

var t=document.createElement("div");
t.innerHTML=x;
var results=[];
alert([].slice.call(t.getElementsByTagName("tr:Name")).map(function(a)a.textContent)) //shows: "test1,test2,test3,test4"
[/CODE]


tested in FF3.5
×

Success!

Help @Pankaj_Kumar spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...