Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using xml file in JS 1

Status
Not open for further replies.

MatanArie

Technical User
Nov 14, 2008
11
IL
Hi Everyone!
I've been searching everywhere for a solution to my problem. It doesn't seem to be too complicated, but I just can't find it.

I have the following code:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
<script type="text/javascript">
var data=new XML()
data=
<food>
<fruit>
<apple>Yum!</apple>
<orange>Great</orange>
<beet>Yuk!</beet>
</fruit>
<veg>
<onion id="Tears">Layers</onion>
<cabbage>Round</cabbage>
</veg>
</food>


document.write(data.fruit.orange+' ');
document.write(data.veg.cabbage+' ');
document.write(data.veg.onion.@id+' ');

</script>
</head>
<body>
</body>
</html>

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

It works fine, but I'd like to load the "xml" data from an external file and not have it on my html page.

Can someone please help me?
 
e4x does not provision a method to load via uri (yet?). You can do it via a detour through js object and convert it to e4x object - a seeminly unfortunately not very efficient manipulation. Like this.
[tt]
var data; //made global
if (document.implementation && document.implementation.createDocument) {
var xmldoc = document.implementation.createDocument("", "", null);
var xmlfile="[blue]yourfileurl.xml[/blue]";
xmldoc.onload=function() {cbf(xmldoc)};
xmldoc.load(xmlFile);
}
function cbf(xdoc) {
var xmlSerializer = new XMLSerializer();
var s=xmlSerializer.serializeToString(xdoc);
data=new XML(s);
with (document) {
open();
write(data.fruit.orange+' ');
write(data.veg.cabbage+' ');
write(data.veg.onion.@id+' ');
close();
}
}
[/tt]
 
Hi Tsuji!
Thanks so much for your help. I never expected to get a reply so soon.

Unfortunately I'm still getting the same results as before (a blank page). Please, if you can, help me out. Thanks!

Here's what I have now:

@@@@@@@@@@@ a_page.html @@@@@@@@@@

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
<script type="text/javascript">
var data; //made global
if (document.implementation && document.implementation.createDocument) {
var xmldoc = document.implementation.createDocument("", "", null);
var xmlfile="eee.xml";
xmldoc.onload=function() {cbf(xmldoc)};
xmldoc.load(xmlFile);
}
function cbf(xdoc) {
var xmlSerializer = new XMLSerializer();
var s=xmlSerializer.serializeToString(xdoc);
data=new XML(s);
with (document) {
open();
write(data.fruit.orange+' ');
write(data.veg.cabbage+' ');
write(data.veg.onion.@id+' ');
close();
}
}


</script>
</head>
<body>
</body>
</html>

@@@@@@@@@@@ eee.xml @@@@@@@@@@

<food>
<fruit>
<apple>Yum!</apple>
<orange>Great</orange>
<beet>Yuk!</beet>
</fruit>
<veg>
<onion id="Tears">Layers</onion>
<cabbage>Round</cabbage>
</veg>
</food>

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

That's the entire content of both docs. Maybe I need to declare the xml in some way?
They are in the same folder btw...

Much thanks!
Matan
 
I had a typo here, sorry!
>xmldoc.load(xmlFile);
[tt]xmldoc.load(xml[highlight]f[/highlight]ile);[/tt]
 
Wow!

It Works!

Yay!

Thanks so much!

Kudos and so on and so forth!

Matan
 
Hi Again Tsuji,
I hope I can steal a bit more of your time. I need just one more bit of help at the moment.
I'm trying to use the xml data to fill out the innerHTML of divs in my document. I've tried all kinds of ways that have worked for me when I was using xml from inside my html doc. But it just isn't working.
Could you have a quick look and tell me what I'm doing wrong?

Thanks again,
Matan

@@@@@@@@@@@ a_page.html @@@@@@@@@@

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
<script type="text/javascript">
var data; //made global
if (document.implementation && document.implementation.createDocument) {
var xmldoc = document.implementation.createDocument("", "", null);
var xmlfile="eee.xml";
xmldoc.onload=function() {cbf(xmldoc)};
xmldoc.load(xmlfile);
}
function cbf(xdoc) {
var xmlSerializer = new XMLSerializer();
var s=xmlSerializer.serializeToString(xdoc);
data=new XML(s);
with (document) {
open();
document.getElementById("Fruit_adjective").innerHTML = data.fruit.orange;
close();
}
}


</script>
</head>
<body>
I think friuts are <div id="Fruit_adjective">Yummy</div>!!
</body>
</html>

@@@@@@@@@@@ eee.xml @@@@@@@@@@

<food>
<fruit>
<apple>Yum!</apple>
<orange>Great</orange>
<beet>Yuk!</beet>
</fruit>
<veg>
<onion id="Tears">Layers</onion>
<cabbage>Round</cabbage>
</veg>
</food>

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 
Sure that is a leap in the right direction - I privately feel mad for the use of document.write in this kind of situation.
[tt]
function cbf(xdoc) {
var xmlSerializer = new XMLSerializer();
var s=xmlSerializer.serializeToString(xdoc);
data=new XML(s);
[blue]document.getElementById("Fruit_adjective").innerHTML = data.fruit.orange;[/blue]
}
[/tt]
 
Hey Tsuji!
The code's working great. I've got it planted in my actual docs and it's working fine.

I was wondering if there was a straight forward way to add a variable into the xml data call. Something like:

var varZZZ = fruit;

document.getElementById("Fruit_adjective").innerHTML = data.varZZZ.orange;

 
It supports both dot (.) and bracket ([]) notations. Bracket notation will be apt to handle variable name.
>document.getElementById("Fruit_adjective").innerHTML = data.varZZZ.orange;
[tt]document.getElementById("Fruit_adjective").innerHTML = data[red][varZZZ][/red].orange;[/tt]
 
Hey Tsuji,
I just noticed that my thank you wasn't posted.

Well here it is:

Thank you!
 
Hello Again!

More issues with XML for your kind consideration.

my XML structure looks like this:

<food>
<Apple>
<name>Southern Green Apple</name>
<color>Green</color>
<taste>Good</taste>
<price>50</price>
</Apple>
<Pear>
<name>Rocky Mountain Pear</name>
<color>Green</color>
<taste>Sweet</taste>
<price>70</price>
</Pear>
<Crabapple>
<name>Wild Crabapple</name>
<color>Greenish</color>
<taste>Bad</taste>
<price>Free</price>
</Crabapple>
</food>

I'm parsing the XML using the same you assisted me with before, I'd now like to get a list of all the <name>s of my fruits. Without having to say: get name of Apple, get name of pear, get name of crabapple... just get names.
I've tried all manor of childnode and getelementbytag but nothing seems to be working (just bugging my JS)

Please help!
Thanks!
 
With your new xml (fruit's levels keep changing!), this will show up their names.
[tt]
var cnodes=data.*;
var sname="";
for (var i=0;i<cnodes.length();i++) {
sname+=cnodes.name()+"\n";
}
alert(sname);
[/tt]
I would suggest you go to "Mozilla Developer Center" and search for their E4X tutorial.
 
Hey, Thanks for the quick reply.

The Fruit XML keeps changing because I only use it as examples. The actual XML data I'm using is not edible ;)

Your script works well, except it's output is:

Apple,Pear,Crabapple

and I need

Southern Green Apple,Rocky Mountain Pear,Wild Crabapple

Thanks
 
Depends on what you mean by "name"! We can be here all year only for example after example, if you don't go to read the tutorial I suggest you do. This is what your name would mean.
[tt]
var cnodes=data.*;
var sname="";
for (var i=0;i<cnodes.length();i++) {
sname+=cnodes.nam[highlight]e+[/highlight]"\n";
}
alert(sname);
[/tt]
 
Ok, two more questions, don't worry, the XML is the same...

um... if I know the fruit's name is "Rocky Mountain Pear" how can I get it's parent ("Pear")?

second, how can I get the names of all the fruits with the Color Green? I'm assuming i need to find the Color notations, remove all of the ones that aren't green, find the parents of the green ones and then find those parent's children notations (name)...

But so far you've wowed me with the ease you make these scripts so I'm hoping whatever the solution will be I'll be smart enough to follow :)

Thanks Agian!
 
Ok, just those 2 and then I'm gonna go read the tutorial Master Tsuji I promise!

Thanks again for all your help!
 
>if I know the fruit's name is "Rocky Mountain Pear" how can I get it's parent ("Pear")?
>second, how can I get the names of all the fruits with the Color Green?

They are basically the same kind of query.

[1] If you know it's level being the grand-child of root, you can do this.
[tt]
try {
var cnodes=data.*.(name=="Rocky Mountain Pear");
for (var i=0;i<cnodes.length();i++) {
alert(cnodes.name());
}
} catch (e) {
alert(e.message);
}
try {
var cnodes=data.*.(color=="Green");
for (var i=0;i<cnodes.length();i++) {
alert(cnodes.name());
}
} catch (e) {
alert(e.message);
}
[/tt]
[2] If the level is not sure, can always have recourse to a loop.
[tt]
var cnodes=data..name;
for (var i=0;i<cnodes.length();i++) {
if (cnodes=="Rocky Mountain Pear") {
alert(cnodes.parent().name());
}
}
var cnodes=data..color;
for (var i=0;i<cnodes.length();i++) {
if (cnodes=="Green") {
alert(cnodes.parent().name());
}
}
[/tt]
I do not script a filter in [2] because there may have some bug (just my opinion) there and I try to avoid it in favor of a less efficient loop.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top