XML Basics
XML (eXtensible Markup Language) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable.
Basic XML Example
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies.</description>
</book>
</catalog>
XML Syntax Rules
- XML documents must have a root element
- All elements must have a closing tag
- XML tags are case sensitive
- Elements must be properly nested
- Attribute values must be quoted
- Special characters must be escaped (&, <, >, ", ')
Types of XML
Well-Formed XML
XML that follows all syntax rules is called "well-formed".
<note>
<to>Alice</to>
<from>Bob</from>
<body>Don't forget the meeting!</body>
</note>
Valid XML
XML that is validated against a DTD or XML Schema is called "valid".
<!DOCTYPE note SYSTEM "Note.dtd">
<note>
<to>Alice</to>
<from>Bob</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>
XML Namespaces
Used to avoid element name conflicts.
<root xmlns:h="http://www.w3.org/TR/html4/"
xmlns:f="http://www.w3schools.com/furniture">
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>Coffee Table</f:name>
<f:width>80</f:width>
</f:table>
</root>
XML with CDATA
CDATA sections contain text that should not be parsed.
<script>
<![CDATA[
function matchwo(a, b) {
if (a < b && a < 0) {
return 1;
} else {
return 0;
}
}
]]>
</script>
XML Technologies
- XPath - Language for navigating XML documents
- XSLT - Language for transforming XML documents
- XQuery - Language for querying XML data
- XML Schema - Alternative to DTD for defining XML structure
- DOM - Document Object Model for programmatic access
- SAX - Simple API for XML (event-based parsing)