/    Sign up×
Community /Pin to ProfileBookmark

How to parse xml into javascript

Hey there,

I was wondering how you can parse a xml-File with javascript implemented in a html-file to get informations out of the xml-file to work with (the xml-file contains various geocoordinates and I want to use a Googlemaps API for Javascript to display them on the map. Therefore I need to know how to do this). Do you have a solution with using firefox instead of iE?

Kind regards,

Kotton

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@rnd_meAug 03.2011 — do u has teh codez?
Copy linkTweet thisAlerts:
@KottonauthorAug 03.2011 — Well, theres this suggestion for iE:

&lt;html&gt;<br/>
&lt;head&gt;<br/>
&lt;title&gt;Read XML in Microsoft Browsers&lt;/title&gt;<br/>
&lt;script type="text/javascript"&gt;<br/>
var xmlDoc;<br/>
function loadxml()<br/>
{<br/>
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");<br/>
xmlDoc.async = false;<br/>
xmlDoc.onreadystatechange = readXML;<br/>
xmlDoc.load("employee.xml");<br/>
}

<i> </i><CODE>function readXML()
<i> </i>{
<i> </i> if(xmlDoc.readyState == 4)
<i> </i> {
<i> </i> //Using documentElement Properties
<i> </i> //Output company
<i> </i> alert("XML Root Tag Name: " + xmlDoc.documentElement.tagName);

<i> </i> //Using firstChild Properties
<i> </i> //Output year
<i> </i> alert("First Child: " + xmlDoc.documentElement.childNodes[1].firstChild.tagName);

<i> </i>//Using lastChild Properties
<i> </i>//Output average
<i> </i>alert("Last Child: " + xmlDoc.documentElement.childNodes[1].lastChild.tagName);

<i> </i>//Using nodeValue and Attributes Properties
<i> </i>//Here both the statement will return you the same result
<i> </i>//Output 001
<i> </i>alert("Node Value: " + xmlDoc.documentElement.childNodes[0].attributes[0].nodeValue);
<i> </i>alert("Node Value: " + xmlDoc.documentElement.childNodes[0].attributes.getNamedItem("id").nodeValue);

<i> </i>//Using getElementByTagName Properties
<i> </i>//Here both the statement will return you the same result
<i> </i>//Output 2000
<i> </i>alert("getElementsByTagName: " + xmlDoc.getElementsByTagName("year")[0].attributes.getNamedItem("id").nodeValue);

<i> </i>//Using text Properties
<i> </i>//Output John
<i> </i>alert("Text Content for Employee Tag: " + xmlDoc.documentElement.childNodes[0].text);

<i> </i>//Using hasChildNodes Properties
<i> </i>//Output True
<i> </i>alert("Checking Child Nodes: " + xmlDoc.documentElement.childNodes[0].hasChildNodes);
<i> </i> }</CODE>
}<br/>
&lt;/script&gt;<br/>
&lt;/head&gt;

&lt;body onload="loadxml();"&gt;

&lt;/body&gt;<br/>
&lt;/html&gt;

and the xml example:

&lt; ?xml version="1.0" encoding="UTF-8" ?&gt;<br/>
&lt;company&gt;<br/>
&lt;employee id="001" &gt;John&lt;/employee&gt;<br/>
&lt;turnover&gt;<br/>
&lt;year id="2000"&gt;100,000&lt;/year&gt;<br/>
&lt;year id="2001"&gt;140,000&lt;/year&gt;<br/>
&lt;year id="2002"&gt;200,000&lt;/year&gt;<br/>
&lt;/turnover&gt;<br/>
&lt;/company&gt;

But I dont know how to use it for a browser like FF. I even tried this with iE, but it
s not alerting anything. Hope you can help me.

Kind regards,

Kotton
×

Success!

Help @Kotton 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 6.16,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...