Learn XML with our Online free XML Tutorial for beginners

XML stands for Extensible Markup Language. Extensible means user can defined his own tag. XML was designed by W3C (World Wide Web Consortium). XML is not a programming language it is a text based markup language to store & define data. XML was derived from Standard Generalized Markup Language (SGML). It is open standard & free to use commercially. For beginners here we are presenting Online XML Tutorial. Learn XML, it’s easy.

Use & Advantages of XML

  • For large websites XML work behind the scene to simplify the creation of HTML documents.
  • XML is flexible to store & organize data.
  • Style-sheet can easily merge with XML to create any kind of look n feel.
  • XML can be used for reloading & off-loading data from databases.
  • Any kind of data type can be presented using XML.
  • XML is a lightweight & platform independent technology.

Introduction to XML Syntax

To start with a XML document the first statement is declaration. In declaration you need to define the version & encoding type for XML. Keep remember XML is case-sensitive. <?xml> is not same as <?XML>.

<?xml version="1.0" encoding="UTF-8"?>

Then in body you have tags & elements. Look at the example below.

<?xml version="1.0" encoding="UTF-8"?>
<employee>
<name>Biswabhusan Panda</name>
<organization>Vijayshanti Infotech (p) Ltd.</organization>
<mobile>9096266548</mobile>
</employee>

Here you can notice there are two kind of information. One is tag & other one is data. I mean under employee tag we have 3 elements name, organization & mobile. Each element contains some data. While reading XML this root node relationship helps in data binding.

Syntax Rule for XML

1. Element Syntax – While creating a element like tag never forget to close the tag. Always check start & end tag must be identical. For an example for element <name> there must be </name>. XML element name can contain any alphanumeric characters.

2. Nested Elements – XML supports nested elements. While creating maintain correct hierarchy.

3. Root Element – An XML document can have only one root element. Inside the root element you can have nested elements. Root Element is similar like database name. This is the unique entry through which you can retrieve data from your XML document.

4. Case-Sensitive – XML is case-sensitive. <name> is not equal to <Name>.

5. Rule for Attributes – Attribute specify single property of an element. Same attribute can’t have more then one value. Attributes comes with key value pair. While assigning a value to attribute use quotation mark. For an example encoding=”UTF-8″.

6. XML Text – To avoid character encoding problems don’t forget to declare encoding type in your XML file. Whitespace characters (line-breaks, blanks & tabs) must be ignored. Few characters are reserved by the XML syntax itself. So they cannot be used directly. For example < in place of this you can use & lt;.

7. Comments in XML – To add comment in your XML file you can use <-- Your Comments -->.

XML Tree

To prepare a valid XML document its mandatory to follow XML tree structure. Any XML document must contains a parent element which is called root. Followed by root element child & sub-child elements can be added. For an example if I want to display a persona information you can do like the below.

<person>
<name>Biswabhusan</name>
<designation>Sr. Process Lead</designation>
<address>
<city>Pune</city>
<state>Maharashtra</state>
<country>India</country>
</address>
</person>

Here person is the root element. Name, Designation & Address are the child elements. Under Address City, State & Country are the sub-child elements.

XML DTD (Document Type Definition)

XML is a data exchange platform. During data exchange DTD is the technique which helps to validate data for a well formatted XML. DTD can be declare in 2 ways Internally with a XML Document or Externally using a *.dtd file. Using DTD you can verify input data for your XML file. Look at the example below.

Internal DTD

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE employee [
<!ELEMENT employee (name,age,salary,description)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
<!ELEMENT salary (#PCDATA)>
<!ELEMENT description (#PCDATA)>
]>
<employee>
<name>Biswabhusan</name>
<age>32</age>
<salary>45,000</salary>
<description>This is only for Sr. Team Leads.</description>
</employee>

#PCDATA means parse-able text data.

External DTD

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "external.dtd">
<employee>
<name>Biswabhusan</name>
<age>32</age>
<salary>45,000</salary>
<description>This is only for Sr. Team Leads.</description>
</employee>

external.dtd

<!ELEMENT employee (name,age,salary,description)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
<!ELEMENT salary (#PCDATA)>
<!ELEMENT description (#PCDATA)>

Basic Structure of XML

An XML document consists of several elements, each defined by tags. The basic structure includes:

1. Declaration: The XML declaration defines the version and encoding. “`xml “`

2. Root Element: Every XML document must have a single root element that contains all other elements. “`xml “`

3. Child Elements: Elements nested within the root element. “`xml Tove Jani Reminder Don’t forget me this weekend! “`

4. Attributes: Elements can have attributes that provide additional information. “`xml Tove Jani “`

XML Syntax Rules

Understanding XML syntax is vital for creating valid XML documents. Here are the key rules:

1. Tags: Tags are case-sensitive. “ and “ are different.
2. Well-formed Documents: XML documents must be well-formed. This means:

– There must be one root element.
– All tags must be properly closed.
– Elements must be nested correctly.

3. Encoding: Special characters (like `<`, `>`, `&`, and `”`) must be escaped using predefined entities:

– `<` for `<` - `>` for `>`
– `&` for `&`
– `”` for `”`

XML vs. JSON

XML and JSON (JavaScript Object Notation) are both popular formats for data interchange, but they have distinct differences:

1. Syntax: XML uses a tag-based structure, whereas JSON is key-value paired.
2. Data Types: JSON supports various data types (strings, numbers, arrays, booleans), while XML treats everything as text.
3. Readability: JSON is generally considered easier to read and write, while XML is more verbose.
4. Namespaces: XML supports namespaces, allowing the use of identical tag names in different contexts, which is not possible in JSON.

Parsing XML

To work with XML data, you need to parse it. Parsing can be done using various programming languages. Here’s an example using Python:

## Using ElementTree in Python

“`python import xml.etree.ElementTree as ET

# Load XML from a file tree = ET.parse(‘library.xml’) root = tree.getroot()

# Iterate through books for book in root.findall(‘book’): title = book.find(‘title’).text author = book.find(‘author’).text print(f’Title: {title}, Author: {author}’) “`

Validating XML

XML validation ensures that the document adheres to a defined structure. This can be done using:

1. DTD (Document Type Definition): Specifies the structure and the legal elements. “`xml ]> “`

2. XSD (XML Schema Definition): Provides a more powerful way to define the structure and data types. “`xml “`

Tools for Working with XML

Several tools can assist in creating, editing, and validating XML documents:

1. XML Editors: Tools like XMLSpy, Oxygen XML Editor, and Notepad++ provide features for editing XML files.
2. Online Validators: Websites like W3C XML Validator can help validate XML documents against DTD or XSD.
3. Command-Line Tools: Tools like `xmllint` can be used for validation and pretty-printing XML files.

Conclusion

XML is a powerful tool for data representation and interchange. Its flexibility, extensibility, and self-descriptive nature make it a valuable asset in modern applications. Understanding its structure, syntax, and applications can significantly enhance your ability to work with data in various contexts. With the knowledge gained from this tutorial, beginners can confidently start using XML for their projects, ensuring effective data management and communication.