/    Sign up×
Community /Pin to ProfileBookmark

parsing xml with jquery – can’t get output correct

I’m parsing an xml doc with jquery and am having trouble getting it to parse correctly. I think the easiest way to explain everything is just to show you what I have, what I want my output to be, and what I’m currently getting…

XML FILE:

[code]
<Results>
<System systemName=”app” />
<System systemName=”app2″ />
<System systemName=”app3″>
<server servername=”server1″>
<File>filepath.log</File>
<result>Fail</result>
</server>
<server servername=”server2″>
<File>filepath to .log</file>
<result>Fail</result>
</server>
<server servername=”server3″>
<File>filepath to .log</file>
<result>Fail</result>
</server>
<server servername=”server4″>
<File>filepath to .log</file>
<result>Fail</result>
</server>
</System>
<System systemName=”app4″>
<server servername=”server11″>
<File>filepath to .log</file>
<result>Fail</result>
</server>
<server servername=”server12″>
<File>filepath to .log</file>
<result>Fail</result>
</server>
<server servername=”server13″>
<File>filepath to .log</file>
<result>Fail</result>
</server>
<server servername=”server14″>
<File>filepath to .log</file>
<result>Fail</result>
</server>
</System>
<System systemName=”app5″ />
<System systemName=”app6″ />
</Results >
[/code]

my .js

[code]
$(document).ready(function()
{

$.ajax({
type: “GET”,
url: “servers.xml”,
dataType: “xml”,
success: function(xml) { parseXml(xml); }
});
});

function parseXml(xml)
{

//find every System node and print the childNode data
$(xml).find(“System”).each(function()
{
$(“#output”).append(“AppName:”+$(this).attr(“systemName”) + “<br />”);
$(“#output”).append(“ServerName:”+$(this).find(“serverName”).attr(“servername”)+ “<br />”);
$(“#output”).append(“filepath:”+$(this).find(“File”).text() + “<br />”);
$(“#output”).append(“Fail?:”+$(this).find(“result”).text() + “<br /> <br />”);
(“servername”));
});

}
[/code]

output:

[code]
AppName:app
ServerName:undefined
filepath:
Fail?:

AppName:app2
ServerName:undefined
filepath:
Fail?:

AppName:app3
ServerName:server1
filepath:filepath.logfilepath.logfilepath.logfilepath.log
Fail?:FailFailFailFail

…cut for length
[/code]

the problem is that my code isn’t seeing the different tags for <file></file> and <fail></fail>.

For example, output for AppName: app3 should be:
(keep in mind that filepath.log is different for each server, just used it here as an example).

[code]
AppName:app3
ServerName:server1
filepath:filepath.log
Fail?:Fail
ServerName:server2
filepath:filepath.log
Fail?:Fail
ServerName:server3
filepath:filepath.log
Fail?:Fail
ServerName:server4
filepath:filepath.log
Fail?:Fail
[/code]

THANKS!

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@mcruauthorAug 11.2009 — figured it out... thought i would share incase anyone searches the forums for the same problem.

just included the below .js in my .html page. 'output' is the id of a div tag in my html page that displays the info.

[CODE]
$(document).ready(function()
{

$.ajax({
type: "GET",
url: "http://navigator.tradearca.com:90/Release_Management/VV/testxml/servers_bak.xml",
dataType: "xml",
success: function(xml) { parseXml(xml); }
});
});

function parseXml(xml)
{

//find every Tutorial and print the author
$(xml).find("System").each(function()
{
$("#output").append($(this).attr("systemName") + "<br />");
$(this).find("server").each(function()
{
$("#output").append($(this).attr("servername") + "<br />");
$("#output").append($(this).find("File").text() + "<br />");
$("#output").append($(this).find("result").text() + "<br />");
});

$("#output").append("<br />");
});

}

[/CODE]
×

Success!

Help @mcru 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.17,
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,
)...