What is XML Full Form? | XML VS HTML
- XML (Extensible Markup Language) is a widely used language for structuring and storing data in a human-readable and machine-readable format.
- It is similar to HTML, but while HTML is used for presenting information on the web, XML is used to describe data.
What is XML used For?
- Self-Descriptive: XML documents are self-descriptive, meaning that they include information about the structure and content of the data they contain.
- Hierarchical Structure: XML documents are organized hierarchically, with nested elements representing parent-child relationships.
- Platform-Independent: XML is platform-independent, making it easy to share data between different systems.
- Extensible: Users can define their tags and data structures, making XML highly extensible.
- Text-Based: XML is human-readable and can be edited using a simple text editor.
XML Naming Rules
All XML elements and attributes must follow the naming rules:
- Names must start with a letter or underscore.
- Names can only contain letters, numbers, hyphens, underscores, and periods.
- Names are case-sensitive.
XML Document
An XML document consists of the following building blocks:
Elements
- XML elements are the fundamental components of an XML document. They are enclosed within angle brackets and can have attributes.
- Elements can be nested inside other elements.
Example:
1<person>
2 <name>John Doe</name>
3 <age>30</age>
4</person>
5
Attributes
- Attributes provide additional information about an element. They are name-value pairs and are defined within the start tag of an element.
Example:
1<person id="123" gender="male">
2 <name>John Doe</name>
3</person>
4
Text Content
- Text content is the data enclosed within an element. It can be any text or numeric value.
Example:
1description>This is some text content.</description>
XML VS HTML
HTML (Hypertext Markup Language) and XML serve different purposes:
- HTML is used for creating the structure and presentation of web pages. It is not as strict as XML and is designed for rendering in web browsers.
- XML is used for structuring and storing data. It is more rigid and focused on describing the content and relationships within the data.
XML Parser
- XML parsers are software components that read and interpret XML documents. They help extract and manipulate data from XML.
- Common XML parsers include DOM and SAX parsers in Java.
DTD (Document Type Definition)
- A DTD is used to define the structure and the legal elements and attributes of an XML document.
- It acts as a schema for validating XML documents.
Example of a DTD declaration within an XML document:
1<!DOCTYPE library [
2 <!ELEMENT library (book+)>
3 <!ELEMENT book (title, author, year)>
4 <!ELEMENT title (#PCDATA)>
5 <!ELEMENT author (#PCDATA)>
6 <!ELEMENT year (#PCDATA)>
7]>
8<library>
9 <book>
10 <title>Sample Book</title>
11 <author>John Doe</author>
12 <year>2022</year>
13 </book>
14</library>
15
XML with HTML
- XML can be embedded within HTML using the <xml> tag. This allows XML data to be displayed within an HTML page.
1<html>
2 <body>
3 <xml id="myData" src="data.xml"></xml>
4 <div>
5 <h3>XML Data</h3>
6 <div id="xmlContent"></div>
7 </div>
8 </body>
9</html>
10
XML with CSS
- CSS can be used to style XML data when it is integrated with HTML. Selectors can target specific XML elements for styling.
1#xmlContent h4 {
2 font-weight: bold;
3 color: #333;
4}
5
6#xmlContent p {
7 color: #666;
8}
9
XML with Javascript
1window.onload = function () {
2 var xmlData = document.getElementById("myData");
3 var xmlContent = document.getElementById("xmlContent");
4 var xmlDoc = xmlData.contentDocument;
5
6 var name = xmlDoc.getElementsByTagName("name")[0].textContent;
7 var age = xmlDoc.getElementsByTagName("age")[0].textContent;
8
9 xmlContent.innerHTML = "<h4>Name:</h4><p>" + name + "</p><h4>Age:</h4><p>" + age + "</p>";
10}
11
Conclusion
XML is a way to store and structure data, like a digital filing system. It helps computers understand and share information easily.