[Site map]
You are here ---
> Newbies Paradise
> Tutorials
> Unofficial
> Programming
> XML
> Reading a tutorial
XML - What is it ?
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.
What is it?
- XML is an acronym standing for eXtendable Markup Language. As you can see, the makers cheated on the name.
- XML is plain text, so any application can be made to collect data from 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.
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
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:
- save a file as .xml
- right click it and say "open with" then chick "Firefox" (or internet explorer etc)
So much work

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: XML1
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: XML1
2
3
4
5 | <gum>
<type>
<price>$1.99</price>
<flavor>grape</flavor>
</gum>
|
These examples will be explained in the next chapter
There are a few very important things about XML to remember:
- in XML, you invent your own tags. xHTML wouldn't allow you to write a tag like <flavorofgum> to say what flavor of gum something was, but that's what XML is made for; to describe something like gum using tags like <flavor> and <price>
- XML is not meant to display things in web browser, so keep in mind, if your code looks ugly in the browser, that's just fine
- XML is everywhere. as a web developer, its probably beneficial to learn XML because it has so many practical uses
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
- names can only contain letters, numbers, and other characters.
- they cannot contain the words XML (or Xml or XMl ect)
- they cannot contain punctuation characters (eg. ! @ # $ % ^ & *)
- cannot contain spaces
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: XML1
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:
- all elements must have a closing tag
- all tags must be nested properly, this means they are closed in the opposite order they are opened: <child><sub-child></sub-child></child> and <child><sub-child></child></sub-child>
- tags are case sensitive. this meant <Gum> is not the same as <gum>
- all XML documents must be completely inside a root element
- you must use entity's in yore documents to write 'special' characters inside an element
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).
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: XML1
2
3 | <type flavor="grape">
...
</type>
|
but it is better to say
Code: XML1
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:
- values must be in quotes
- you can create your own attributes (some could argue against that in xHTML
)
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.
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.
10 Newbies Connected |
8 Requests |
0.0602s (0.0467s)