Go to the menu - Go to the contents

[Site map] You are here --- > Newbies Paradise > Tutorials > Unofficial > Programming > XML > Reading a tutorial

XML - What is it ?

Avatar
Author : yabberyabber
Creation : : on 04/07/2008 05:29:21 AM
Last modification: : on 04/24/2008 12:11:46 PM
Rate and comment this tutorial
Print this tutorial
You are about to read a tutorial written by a member of this website. Although the author probably invested every effort to make this tutorial's content correct, we can not guarantee that the information on this page is 100% exact. Please keep this in mind when reading this tutorial. ;o)
Many people here know (x)html, and html is very good for displaying data. But xml is meant to describe data. It may seem confusing why you would need to just describe data, but xml is very practical. This tutorial assumes that you have a basic understanding of xHTML.
Chapter Contents :

What is xml? And why is it important?

What is it?



XML is very similar to xHTML. In both, you have tags which are surrounded by the < sign and the > sign and they both have attributes. But that does not mean that XML can replace xHTML.

So why is it important?

XML is hard to explain. It doesn't do anything, it holds data so other software can do things with it. xHTML is meant to display data, but you cannot use html do do something like send data to an application (you could, but it would not be practical). You could do this with a data format that just 'flew out of the sky' and is only used by that application, or you could use XML so that other applications can also easily access this information.

Before we begin...

Writing XML is a lot like writing xHTML. You need a text editor, and you need a web browser (you don't need this but more on this later). Now, a lot of people like to use notepad to edit XML, but if you've been a good little newbie and have read m@teo21's tutorial on XHTML/CSS, you would have downloaded notepad++, a modern marvel. All you have to do in notepad++ is click the language drop-down menu, then go all the way down and click xml (should be second to the bottom) then presto! You're writing XML :D

now on to web browsers. You don't need them, but they help to tell you if your XML is valid. The xHTML/CSS tutorial should have taught you how to view xHTML documents in a web browser, and it should be exactly the same for XML. Just in case you forgot, here's how:



So much work :wow: but now all you have to do to change it is save and click refresh on your browser.

I am going to give you a sample XML document so you can try:

This is correct: (note how the browser tells you it is correct)
Code: XML
1
2
3
4
5
6
<gum>
  <type>
    <price>$1.99</price>
    <flavor>grape</flavor>
  </type>
</gum>


And here is an incorrect code: (also note how the browser tells you it is incorrect)
Code: XML
1
2
3
4
5
<gum>
  <type>
    <price>$1.99</price>
    <flavor>grape</flavor>
</gum>


These examples will be explained in the next chapter

The very basics

There are a few very important things about XML to remember:



Because tags are names by you, they needed to write some rules. They couldn't let people write a tag called 'magic>pumpkin' (would look like <magic<pumpkin> ) or else the application would see the > and think the tag ended. So here they are


Let's take a look at a very basic xml file

Code: XML
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<gum>
  <type>
    <price>$1.99</price>
    <flavor>grape</flavor>
  </type>
  <type>
    <price>$1.99</price>
    <flavor>strawberry</flavor>
  </type>
  <type>
    <price>$1.50</price>
    <flavor>mint</flavor>
  </type>
</gum>


Looks familiar, doesn't it? In this example, we have 3 types of gum: grape, strawberry, and mint. The example includes price of gum, and the flavor. You can see that we have many elements (an element is a tag) inside other elements. This brings up something called an xml tree.

In this example the tree's roots are the 'root' element <gum>. All XML documents have a root elements, the root is a single element that all the other elements are in. Inside you can see the child elements of <gum> which are <type>. And inside the type elements are the <price> and <flavor> elements which are sub-childs of <gum>, and childs of <type>. So here's the basic structure of an xml document:

Code: XML
1
2
3
4
5
6
<root>
  <child>
    <sub-child>
    </sub-child>
  </child>
</root>


It's kind of like a tree: it has roots, branches (children), and leaves (sub-children). But the main problem with the tree is that trees don't get smaller than leaves (well, not if you want to get all technical about it) and in XML documents you can have children, sub-children, sub-sub-children, sub-sub-sub-children and so on.

Now that you have a taste of xml, it is time to go over syntax. The basic rules are identical to those of xHTML. For those who don't remember, here they are:

Comments

Comments are very simple in xml, so i wont dwell on them to long. as in xHTML, comments are surrounded on both sides by a <!-- and a -->. Everything between these two will be ignored and turn green (they will also be in small text).

Attributes, just like in html

What about attributes? We use them in xHTML so why not in xml?

Well, I never said there were no attributes in XML, in fact, i never said anything about them at all.

The thing about attributes in xml is: there are better ways to get it done (some people may disagree but the majority works around them). Use elements instead.

You could say
Code: XML
1
2
3
<type flavor="grape">
...
</type>


but it is better to say
Code: XML
1
2
3
4
5
6
<type>
<flavor>
grape
</flavor>
...
</type>


If you really must use attributes, the rules are the same as they are in xHTML:

Quiz

which xml tag has a valid name?
is this a valid code?

Code: XML - Show / hide line numbering
  1. <fish>
  2.   <goldfish>
  3.     <price>
  4.       $5.00
  5.     </price>
  6.     <color>
  7.       gold
  8.     </color>
  9.   </goldfish>
  10. </fish>
in the beginning of the chapter, we saw this incorrect code
Code: XML - Show / hide line numbering
  1. <gum>
  2.   <type>
  3.     <price>$1.99</price>
  4.     <flavor>grape</flavor>
  5. </gum>

what is wrong with this code?


Now that you know xml, you can learn ways to display XML data, like xsl or Dom. you can even use XML in applications that you make (if you decide to make some one day). The most important thing about XML is that it is very adaptable and you can use it to help you in any situation.
Author : yabberyabber
Rate and comment this tutorial
Print this tutorial

Change your template | Learn more about NP | Site map | Terms of use | Rules | RSS feed | XHTML 1.0 | CSS 2.0
Powered by Simple IT SARL: Contact us

There is nothing else to read, you have to go up now !

Do you want to be published here? Contact us.

Number of Newbies connected 10 Newbies Connected | SQL requests 8 Requests | Page loading delay 0.0602s (0.0467s)