...

View Full Version : XML help (noob here)



capt_nemo777
09-02-2008, 02:06 AM
(based from the tutorial in w3schools.com)
let's say the user created this xml script


<note>
<to>Tove</to>
<from>Jani</from>
<body>Don't forget me this weekend!</body>
</note>

and it should output this
http://img294.imageshack.us/img294/8776/xmlme9.jpg
and then let's say the user decided to add an element later on in the script like

<note>
<date>2008-01-10</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

w3cshool said, the application will not crash because it can still detect the <to>, <from>, and <body> elements in the XML document and produce the same output.

so my question now is..what's gonna happen with the <date> ? where did it go ?
why was it not detected at all ? (im new to this thing) :confused:

Samhain13
09-02-2008, 09:56 PM
A link to the tutorial would be helpful because we need to find out how what script handles which output. :)

capt_nemo777
09-03-2008, 01:16 AM
A link to the tutorial would be helpful because we need to find out how what script handles which output. :)

here is a link to the tutorial sir.. i hope you can help me configure this out..their explanation is not that clear for a slow learner to understand :confused:

Samhain13
09-03-2008, 01:35 AM
Are you using this tutorial? http://w3schools.com/xml/xml_dom.asp

In your first post, you mention changing the XML file by adding an "date" element. But it doesn't show anywhere, right? That's because you'd also need to change the javascript a bit so that it knows what to do with the new "date" element.

You'll need to add this to the javascript:

document.getElementById("date").innerHTML=
xmlDoc.getElementsByTagName("date")[0].childNodes[0].nodeValue;

And then add something like this to the HTML:

<p>Date <span id="date"></span></p>

Rough explanation:
The javascript part, in plain English says "get the node value of the date element of xmlDoc (your XML file) and put it into the span that's identified as 'date'".

So, aside from editing the XML, the javascript also needs to know what to do with the new element AND in the HTML, there needs to be a place where the javascript can throw in what it finds. If there's no <span id="date"></span>, javascript will have no where to put what it found in the new <date></date> element. That's why you didn't see anything happening.

Sorry if that was confusing, but try it out and hopefully you'll see what I mean. Cheers! :D

capt_nemo777
09-03-2008, 02:25 AM
i don't understand..i just plunge to xml directly without learning javascript.. basic xml though

Samhain13
09-03-2008, 03:02 AM
Alright. Let's try again...

In that tutorial, there are basically 3 things involved: XML, Javascript and HTML.
XML is where you put your stuff.
Javascript reads the XML and gets your stuff from it.
HTML is where Javascript puts the stuff that it got from your XML.

So, simply changing the XML isn't going to change what you see in your browser because what's shown in the browser is only the HTML. We have to write additional instructions in the Javascript too. And we have to prepare the HTML in a certain way so that the Javascript can follow our new instructions.

If you follow my previous post, you'll get something like this new file (the changes I wrote about are in bold letters):

<html>
<head>
<script type="text/javascript">
function parseXML()
{
try //Internet Explorer
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
catch(e)
{
try //Firefox, Mozilla, Opera, etc.
{
xmlDoc=document.implementation.createDocument("","",null);
}
catch(e)
{
alert(e.message);
return;
}
}
xmlDoc.async=false;
xmlDoc.load("note.xml");

document.getElementById("date").innerHTML=
xmlDoc.getElementsByTagName("date")[0].childNodes[0].nodeValue;
document.getElementById("to").innerHTML=
xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
document.getElementById("from").innerHTML=
xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;
document.getElementById("message").innerHTML=
xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;
}
</script>
</head>

<body onload="parseXML()">
<h1>W3Schools Internal Note</h1>
<p>Date: <span id="date"></span></p>
<p><b>To:</b> <span id="to"></span><br />
<b>From:</b> <span id="from"></span><br />
<b>Message:</b> <span id="message"></span>
</p>
</body>
</html>

You can try this with your XML file that has that new "date" element. But you should know that if you remove the date element or add another new element, this will no longer work.

Of course, as you progress, you'll find that there's a cleaner way of writing the code that I posted. But you'll have to make do with this for now. :D

capt_nemo777
09-03-2008, 03:12 AM
geeeezzz.... duduguin ang ilong ko nito... things in my head all mixed up now..am stucked with php coding with xml extension while i don't know all about xml..now here comes javascripting LOL :eek:

Samhain13
09-03-2008, 03:19 AM
Hah! Pinoy ka din pala! :P

Take it easy, learn just one thing at a time. The thing you're trying to learn has AJAX written all over it. And AJAX is not really for beginners. Practice XML and HTML first, they're not so unlike each other anyway. Then dive into Javascript. Hehehe!

capt_nemo777
09-03-2008, 03:24 AM
nyahaha..salamat sa tip boss .... ok boss ill try to learn xml and html combination first..

macwiz
09-10-2008, 11:15 PM
nyahaha..salamat sa tip boss .... ok boss ill try to learn xml and html combination first..

What are you trying to accomplish anyways?

And you don't learn XML. There XML can't do anything. It's just tags with data. YOU make it up. All you need is this:
<tag name>Data</same tag name>

That is XML.

The issue you are having is displaying the data.

gillberk
09-25-2008, 11:06 PM
XML stands for EXtensible Markup Language and it was designed to transport and store data.Xml is the most common tool for data transmissions between all sorts of applications, and becomes more and more popular in the area of storing and describing information.If you need to display dynamic data in your HTML document, it will take a lot of work to edit the HTML each time the data changes.With XML, data can be stored in separate XML files.
--------------------------------------
gillberk

ETHICAL SEO (http://www.drivenwide.com)

praveena
09-29-2008, 12:17 PM
Hi freinds, please help me to display page number in XMl.

Samhain13
10-11-2008, 11:48 PM
And you don't learn XML. There XML can't do anything. It's just tags with data. YOU make it up. All you need is this:
<tag name>Data</same tag name>

That is XML.

Just for kicks. Your example is wrong and that is NOT XML.

This would make it right though:


<tag name="attribute value">Data</tag>

Hence, you do have to learn XML if you want to use it; because even if in many case, you'll be inventing tag names anyway, there are rules that need to be followed. :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum