This is an old revision of the document!
Table of Contents
XML Example
<?xml version='1.0' encoding='UTF-8' ?> <!-- first XML example --> <message id="123456"> <text>Hello World!</text> </message>
- version: The version number of the XML document.
- encoding: Character encoding - 'ISO-8859-2' refers to Latin-2 encoding.
- comments: Comments can be placed between <!– –>, and they can span multiple lines.
- tagging data: Data is labeled using tags. For example, <name></name>, where <name> is the opening tag and </name> is the closing tag. If the tag doesn't contain data, the closing can be simplified like <name />.
- In this example, “Hello World!” is stored in the <text> tag.
- An XML file contains a root element (or root tag) to which all other elements belong, in this case <message>. Elements that contain other elements are called parent elements, and the elements within them are called child elements.
- Elements can contain an unlimited number of attributes (properties), such as the id attribute = “123456”. Attribute names are not limited in length, but every attribute name must start with a letter.
Special Characters in XML
In XML, special characters are represented as follows:
- & - &
- < - <
- > - >
- ' - '
- “ - "
CDATA Section
A CDATA section can contain embedded data, such as the following example:
<script language="JavaScript" type="text/javascript">
<![CDATA[
function sayHello() {
document.write("Hello World!");
}
]]>
</script>
XML Namespaces
Since creators of XML documents use their own vocabulary to build XML, name conflicts are possible. For example, the <student> tag might be too generic. Using namespace prefixes, the element can be specialized:
<miskolc:student>John Smith</miskolc:student>
Here, the qualified element is student and the namespace prefix is miskolc.
<?xml version="1.0" encoding="UTF-8"?>
<data xmlns:unimiskolc="www.uni-miskolc.hu">
<unimiskolc:file filename="aula.jpg">
<unimiskolc:description>Photo of the university hall</unimiskolc:description>
<unimiskolc:size width="200" height="100" />
</unimiskolc:file>
</adatok>
In this example, the namespace prefix unimiskolc is defined to avoid name conflicts, and it links the data to the www.uni-miskolc.hu namespace.
