XML is a way of structuring datas, to allow an easy parsing from different system or programs.
That being said,
XML can only be datas. There can be no logic in it, or in form of data to be used later by another language.
If you want to directly format XML in another output, you need to associate it with an
XSL engine. The XSL will be the one which will transform the XML in wathever you want.
I'll stick to XML, but feel free to ask.
There are many XML reader available.
Some use a DOM type interface (the browser DOM being a XML representation of the page, after all) and an Events based implementation (Saxon, to name him), that will read the XML from top to bottom, and trigger events when it reach a node.
XML parser are very present in the java world, but as I said before, you can found a XML parser implementation in any language, even javascript.
I'll talk about the web browser DOM implementation mostly, as it's the one I'm familiar with.
The code I'll give you here will be javascript, and run in both IE6/7 and firefox.
You can find a really good explaination of every functions I'll use on the Mozilla Developer center :
http://developer.mozilla.org/en/docs/DOM:element
There is the structure of a typicall XML file:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
<listing>
<disk id="1">
<name>The improbable duo</name>
<artists>
<name>Daft Punk</name>
<name>Metallica</name>
</artists>
</disk>
<disk id="2">
....
</disk>
</listing>
</root>
When you read this file, you easily can relate it to something that you understand, and that's the force of XML.
It's humanly readable, and it's understandable by computers as well.
In XML, there are node(s), children(s) and paths
When you want to get datas from an XML file, like this one, you first need an entrance point.
We usually take the root element, but it can be anything.
For my example, we will fetch all the cd names from this listing.
First, we need to take a entry point. That will be the parent of the listing.
Code:
var root=document.getElementsByTagName('root')[0];
The getElementsByTagName is pretty explaining itself.
The trick is, as it return an array of elements, we take the first one, as we know it will be ours.
From there, we use getElementsByTagName() to return us a list of elements disks. We cannot try to get the elements "name", as there are discs names, and artists names:
Code:
var aryDiscs=root.getElementsByTagName('disc');
And now, we simply parse every discs to get their names:
Code:
var aryDiscsNames=new Array();
var discName="";
var discId=null;
for(var cpt=0;cpt<aryDiscs.length;cpt++){
discId=aryDiscs[cpt].getAttribute('id');
for(var cptChilds=0; cptChilds< aryDiscs[cpt].childNodes.length; cptChilds++){
if(aryDiscs[cpt].childNodes[cptChilds].nodeName=='name'){
discName=aryDiscs[cpt].childNodes[cptChilds].nodeValue;
}
}
You should check the mozilla reference, you will find every methods and properties you need.
Just a reminder: In object oriented programming, a
function embedded in an object is called a method, and
a variable embedded in an object is called a property.
As being javascript objects programming, you call or read them by the
Code:
element.method();
var x=element.property;
element.property="something"
I'll stop here for now.
Tell me if you have more specific questions, or if you don't get some dark corners.
I'll do my best to explain it.
The fact is, it's complicated, and hard to explain it in simple words...