Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
🧠 Introduction
XML is not just a standalone data format. Its true power
lies in its ability to integrate with various technologies and
environments. This chapter covers how XML works with:
🌳 1. DOM vs SAX Parsing
✅ DOM (Document Object Model)
Example in Python (DOM with xml.dom.minidom):
python
from
xml.dom.minidom import parse
doc
= parse("books.xml")
titles
= doc.getElementsByTagName("title")
for
t in titles:
print(t.firstChild.nodeValue)
✅ SAX (Simple API for XML)
Example in Python (SAX with xml.sax):
python
import
xml.sax
class
BookHandler(xml.sax.ContentHandler):
def startElement(self, name, attrs):
if name == "title":
self.inTitle = True
else:
self.inTitle = False
def characters(self, content):
if self.inTitle:
print("Title:", content)
parser
= xml.sax.make_parser()
parser.setContentHandler(BookHandler())
parser.parse("books.xml")
Feature |
DOM |
SAX |
Memory |
High |
Low |
Access |
Random |
Sequential |
Modifiable |
Yes |
No |
Speed |
Slower for
large XML |
Faster for
large XML |
🔍 2. XPath: Navigating
XML
XPath (XML Path Language) is used to navigate
XML documents and extract specific data.
✅ Basic Syntax
Expression |
Result |
/catalog/book |
Selects all
<book> elements under <catalog> |
//title |
Selects all
<title> elements |
book[@id='101'] |
Selects book with
attribute id=101 |
//book[price>50] |
Selects books
where price > 50 |
✅ XPath in Python
python
import
xml.etree.ElementTree as ET
tree
= ET.parse('books.xml')
root
= tree.getroot()
for
book in root.findall('./book[price>30]'):
print(book.find('title').text)
📊 3. XQuery: Advanced XML
Query Language
✅ Basic XQuery Example
xquery
for
$b in doc("books.xml")/catalog/book
where
$b/price > 50
return
$b/title
🔁 4. XSLT: Transforming
XML
XSLT (Extensible Stylesheet Language Transformations)
allows converting XML into:
✅ Sample Transformation
books.xml:
xml
<catalog>
<book>
<title>XML Basics</title>
<price>39.99</price>
</book>
</catalog>
books.xsl:
xml
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template
match="/catalog">
<html><body>
<h2>Book Catalog</h2>
<xsl:for-each
select="book">
<p>
<xsl:value-of
select="title"/> -
<xsl:value-of
select="price"/>
</p>
</xsl:for-each>
</body></html>
</xsl:template>
</xsl:stylesheet>
📋 XSLT Use Cases
Use Case |
Benefit |
Convert XML → HTML |
Render in browsers |
XML → plain text |
Reports and
summaries |
XML → another XML |
Format translation
(e.g., UBL to custom) |
🧬 5. Integrating XML with
Programming Languages
✅ Java + XML
Using DOM:
java
DocumentBuilderFactory
factory = DocumentBuilderFactory.newInstance();
DocumentBuilder
builder = factory.newDocumentBuilder();
Document
doc = builder.parse("books.xml");
NodeList
titles = doc.getElementsByTagName("title");
Using
JAXB (Java Architecture for XML Binding):
java
@XmlRootElement
public
class Book {
public String title;
public String author;
}
✅ Python + XML
python
import
xml.etree.ElementTree as ET
tree
= ET.parse("books.xml")
root
= tree.getroot()
✅ JavaScript + XML (Browser)
javascript
const
parser = new DOMParser();
const
xml = parser.parseFromString(xmlText, "application/xml");
const
titles = xml.getElementsByTagName("title");
🌐 6. XML on the Web
✅ SOAP (XML-based Web Services)
xml
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<getUser>
<id>123</id>
</getUser>
</soap:Body>
</soap:Envelope>
SOAP uses XML as the communication format between client and
server.
✅ REST with XML
Although JSON is more common, some REST APIs still return or
accept XML:
http
GET
/api/product/1
Accept:
application/xml
Response:
xml
<product>
<name>Laptop</name>
<price>999.99</price>
</product>
🔌 7. Databases and XML
✅ Native XML Databases
✅ XML Columns in RDBMS
sql
SELECT
XmlCol.value('(/book/title)[1]', 'VARCHAR(100)') FROM Books;
🧠 8. Real-World
Integration Scenarios
Integration Type |
Example Scenario |
Web API (SOAP) |
Enterprise-level
e-commerce or banking |
Android App Config |
AndroidManifest.xml,
layout XMLs |
RSS Feeds |
Publishing headlines |
Document Formats |
.docx, .odt,
.svg, .xhtml |
Inter-app Messaging |
Custom XML messages in
finance (FIXML, FpML) |
🧪 9. Practice Exercise
Convert this XML to HTML using XSLT.
Input XML:
xml
<employees>
<employee>
<name>John</name>
<role>Manager</role>
</employee>
</employees>
XSLT:
xml
<xsl:template
match="/employees">
<html><body>
<xsl:for-each
select="employee">
<div>
<b><xsl:value-of
select="name"/></b> -
<xsl:value-of
select="role"/>
</div>
</xsl:for-each>
</body></html>
</xsl:template>
✅ Summary Table
Tool/Tech |
Purpose |
DOM |
Tree-based parsing |
SAX |
Stream-based
parsing |
XPath |
Navigate XML nodes |
XQuery |
Query large
XML datasets |
XSLT |
Transform XML into
other formats |
Java/Python/XML |
Application
and backend integration |
SOAP/REST |
Web service data
transport |
Databases |
Store/query
XML as structured data |
A: XML stands for eXtensible Markup Language.
A: Yes, <Tag> and <tag> are treated as different elements.
A: Absolutely. That's why it's called "extensible."
A: XML stores and structures data, while HTML displays it.
A: Its structured format and readability make it ideal for settings/configs.
A: Yes. Many enterprise and legacy APIs use SOAP, which is XML-based.
A: Not at all. While JSON is preferred for web APIs, XML is widely used in enterprise, publishing, and government systems.
A: You can validate it using DTD or XSD files or an XML validator tool.
A: Not directly. It needs to be base64 encoded first.
A: Notepad++, VS Code, XMLSpy, Eclipse, and Oxygen XML Editor are popular.
Please log in to access this content. You will be redirected to the login page shortly.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Comments(0)