XML Tutorial

XML Tutorial: A Beginner's Guide

XML (Extensible Markup Language) is a structured format used to store and transport data. It is human-readable, platform-independent, and widely used in web services, databases, and configuration files.


1. XML Basics

XML Example

<?xml version="1.0" encoding="UTF-8"?>
<library>
    <book>
        <title>XML Basics</title>
        <author>John Doe</author>
        <year>2024</year>
    </book>
</library>

Key XML Rules

✅ Every XML document must have one root element.
✅ Tags must be properly closed (<title>XML</title>).
✅ XML is case-sensitive (<Title> ≠ <title>).
✅ Attribute values must be quoted (title="XML Basics").
✅ XML does not allow special characters like <, >, & without escaping them.


2. XML Structure

XML Elements

Elements are building blocks of XML.

<book>
    <title>XML Guide</title>
    <author>Jane Smith</author>
</book>
  • <book> is a parent element.
  • <title> and <author> are child elements.

XML Attributes

Attributes provide extra information.

<book title="XML Guide" author="Jane Smith"/>
  • "title" and "author" are attributes.

XML Comments

<!-- This is a comment -->
<book>
    <title>XML Basics</title>
</book>

3. XML Validation

Well-Formed XML

A well-formed XML follows syntax rules.
Use an XML Validator to check errors.

Validating XML with DTD

<!DOCTYPE library [
    <!ELEMENT library (book+)>
    <!ELEMENT book (title, author, year)>
    <!ELEMENT title (#PCDATA)>
    <!ELEMENT author (#PCDATA)>
    <!ELEMENT year (#PCDATA)>
]>
<library>
    <book>
        <title>XML Basics</title>
        <author>John Doe</author>
        <year>2024</year>
    </book>
</library>

Validating XML with XSD

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="library">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="book" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="title" type="xs:string"/>
                            <xs:element name="author" type="xs:string"/>
                            <xs:element name="year" type="xs:int"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

XSD ensures data validation.


4. XML Parsing

Using JavaScript to Parse XML

let parser = new DOMParser();
let xmlString = `<library><book><title>XML Basics</title></book></library>`;
let xmlDoc = parser.parseFromString(xmlString, "application/xml");
console.log(xmlDoc.getElementsByTagName("title")[0].textContent);

Using Python to Parse XML

import xml.etree.ElementTree as ET

xml_data = """<library>
    <book>
        <title>XML Basics</title>
        <author>John Doe</author>
    </book>
</library>"""

root = ET.fromstring(xml_data)
print(root.find(".//title").text)  # Output: XML Basics

5. XML in Web Services

XML in AJAX

let xhr = new XMLHttpRequest();
xhr.open("GET", "books.xml", true);
xhr.onreadystatechange = function () {
    if (xhr.readyState == 4 && xhr.status == 200) {
        let xmlDoc = xhr.responseXML;
        console.log(xmlDoc.getElementsByTagName("title")[0].textContent);
    }
};
xhr.send();

SOAP Web Service Example

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <getWeather>
            <city>New York</city>
        </getWeather>
    </soapenv:Body>
</soapenv:Envelope>

SOAP APIs use XML for communication between systems.


6. XML Applications

✔ Data Exchange (Web Services, APIs)
✔ Configuration Files (.xml for settings in apps)
✔ Databases (XML storage in SQL Server, Oracle)
✔ Web Feeds (RSS, Atom)
✔ Office Documents (.docx, .xlsx, .pptx)
✔ Mobile Development (Android UI XML files)


Next Steps

Would you like examples in a specific language (Java, Python, JavaScript) or a project-based tutorial?

Share on Google Plus

About It E Research

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment