/    Sign up×
Community /Pin to ProfileBookmark

how to use javascipt to grab data from xml file?

hi all
i made a xml file as following. i want to make 1 input field for user. It lets you enter the xml file name (with Get button), as many field or element names from the xml file as you want (with the Add button), and then constructs the HTML table that accesses those elements of the xml file. so could anyone show me an example on it? thanks so much.

<?xml version=”1.0″ ?>

<!DOCTYPE CATALOG SYSTEM “lab.dtd”>
<body>
<employee>
<ID>222</ID>
<SSN>234-57-2736</SSN>
<salary>$45,000</salary>
<name>Betty</name>

</employee>
<employee>
<ID>321</ID>
<SSN>12-32-2234</SSN>
<salary>$48,000</salary>
<name>Nick</name>
</employee>

<employee>
<ID>124</ID>
<SSN>267-56-0978</SSN>
<salary>$55,000</salary>
<name>Mike</name>
</employee>
<employee>
<ID>475</ID>
<SSN>789-34-3456</SSN>
<salary>$75,000</salary>
<name>John</name>

</employee>
<employee>
<ID>222</ID>
<SSN>167-07-3467</SSN>
<salary>$55,000</salary>
<name>Kenny</name>
</employee>
</body>

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@fg5authorNov 01.2004 — no one can help me on this?
Copy linkTweet thisAlerts:
@TheBearMayNov 01.2004 — This is doesn't contain everything you wanted, and is a wee bit rough, but should give you the basics of what you'll need:

[code=php]
var recNo=0;
var xmldso = document.createElement("XML");
if (navigator.appName == "Netscape") xmldso = document.implementation.createDocument("", "", null);
xmldso.async = false;
try {
xmldso.load("lab.xml");
}
catch (e){
alert(e.name+":n"+e.message);
}
xmlrecord=xmldso.getElementsByTagName("employee");

function x(elemName){
try{
return xmlrecord[recNo].getElementsByTagName(elemName).item(0).firstChild.nodeValue;
}
catch(e){
return "";
// alert(e.name+":n"+e.message+"nelemName="+elemName+" recNo="+recNo);
}
}

function movefirst(){
recNo=0;
buildTable();
}

function movenext(){
recNo++;
if (x("ID")=="")recNo--;
buildTable();
}

function moveprevious(){
recNo--;
if (x("ID")=="") recNo++;
buildTable();
}
...[/code]
Copy linkTweet thisAlerts:
@fg5authorNov 02.2004 — thanks thebearmay. but I can't use php for it. so anyone know how to do it by using javascipt?
Copy linkTweet thisAlerts:
@JPnycNov 02.2004 — That WAS javascript.
×

Success!

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