Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Coding Forum


You are currently viewing our Coding Forum as a guest. Please register to participate.
Login



Reply
XML how do u use it So confussed!i!
Old 06-02-2007, 10:03 AM XML how do u use it So confussed!i!
dansgalaxy's Avatar
Defies a Status

Posts: 6,522
Name: Dan
Location: Swindon
Trades: 0
Hi,

i would like to learn how to use xml in my scripts and software but im qute confussed about how it works,

Coud someone explain
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
 
Register now for full access!
Old 06-02-2007, 02:44 PM Re: XML how do u use it So confussed!i!
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
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...
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 06-02-2007, 03:20 PM Re: XML how do u use it So confussed!i!
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,023
Name: Forrest Croce
Location: Seattle, WA
Trades: 0
Quote:
Originally Posted by dansgalaxy View Post
i would like to learn how to use xml in my scripts and software but im qute confussed about how it works,
Why do you want to use it? What do you hope it will do for you? There are millions of ways XML is used, so if you expand on what you're trying to accomplish, that will help people not steer you down the wrong path.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Old 06-03-2007, 09:40 AM Re: XML how do u use it So confussed!i!
dansgalaxy's Avatar
Defies a Status

Posts: 6,522
Name: Dan
Location: Swindon
Trades: 0
Well, i just have heard and read about how great it is, and have seen how it is often used for templates in cms and applications like that, and would like to know how i can use it so when i do advance to that kind of thing i can.

I borrowed a hge book, and i been reading bits of it but im just quite confssed, i was under the impression u kind of wrote a parse which was like when it find <something> u tell it to do whatver with it, but not it seems im wrong... which is why im quite confused. :S

I dunno Lol...


From what i seem to get it seems to act as almost a db like thing?...

(btw i am planing to make/complete a Music library thing so maybe it could be cool, i i had something which produces a xml file for each album and then the software like reads that... i dunno)

Dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE

Last edited by dansgalaxy; 06-03-2007 at 09:43 AM..
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 06-03-2007, 02:15 PM Re: XML how do u use it So confussed!i!
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,023
Name: Forrest Croce
Location: Seattle, WA
Trades: 0
XML is a data transport format, not a database. I worked for a phone company and was in charge of their DB; it had 250 million records, and there were queries against that table that had to finish in less than five seconds for business reasons. That's impossible with XML. Now plenty of applications ( ie computer software, not uses or things to do ) use XML to persist smaller amounts of data, so you could think of it as a small scale DB, but that's more a side effect.

A CMS typically stores its information in a real database ( SQL Server, Oracle, MySQL, even MS Access or dBase/Clipper ), and has code that can take one record from the database, and turn it into either a web page or a Windows screen.

What you described about the transform where you declaratively grab an element ( ie <something> ) and do things with it is XSLT. You can use that to turn XML into either a different flavor of XML, into HTML, or something else. And if you have a small enough amount of data, you could just use XML/XSTL to do what you want.

But there's really no good reason to do things that way, other than the one you've discovered: people keep talking about how great it is.

A more garden-variety use for XML is web services and AJAX.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Old 06-03-2007, 02:38 PM Re: XML how do u use it So confussed!i!
dansgalaxy's Avatar
Defies a Status

Posts: 6,522
Name: Dan
Location: Swindon
Trades: 0
so is there really any advatges with xml which cant be done with mysql db html etc?

dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 06-03-2007, 05:55 PM Re: XML how do u use it So confussed!i!
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
XML is not a database.
You cannot do select, or update it.
It's just a plain text file, with a bunch of elements you can parse.

And trust me, There are many days I wish not to use XSL, as this is a particularly intolerant language.

I've digged for you a XML document with his XSL partner I realized for my work some times ago.
The text is in french, but it's not needed to understand it.

The XML is produced when comparing several courses given in a specific cursus in the university I work in.
The XSL is here to format it in a visual view, telling which subjects are held in which blocks.

The blocks can be nested to infinity, and every block can contain either blocks or "subject" (I don't know the right word in english...)
Each block can have attributes, and each subject can have attributes, teachers, external peoples, and study forms (course, test, field assignement...)

It's a pretty complicated computing that is done there, and it's not the final version, but you'll get a more precise idea why you should stick to a simplier language you already master.

http://www.webalis.com/xml/compare_lien.xml
http://www.webalis.com/xml/compare_lien.xsl

Open the XML file with IE, and it will apply the XSL to them, and present you an almost correct page (Some bits are meant to be processed outside, and still displayed with ie).
I don't know why the transform don't go in FF, but I think it's related to my apache config serving text/xml rather than application/xml.
But I couldn't correct it in the last hour, so beat it...
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 06-03-2007 at 05:56 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 06-03-2007, 06:11 PM Re: XML how do u use it So confussed!i!
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
Quote:
Originally Posted by dansgalaxy View Post
so is there really any advatges with xml which cant be done with mysql db html etc?

dan
Cars are still useful even though we have jets, sometimes you just need to go across town. They're totally different things.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to XML how do u use it So confussed!i!
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.28488 seconds with 12 queries