Java Programming Java & XML
Java Programming
Java & XML
XML (eXtensible Markup Language), like HTML, is a markup language for marking up the structure of a text document. It is a subset of Standard General Markup Language (SGML). XML is not a programming language like Java or C#. It is developed and maintained by World-Wide Web Consortium (W3C) @ www.w3c.org. (W3C also maintains specifications for HTTP, HTML, XHTML, CSS, among others.)
XML is a family of technologies, which includes:
- XML: the markup language itself.
- DTD (Document Type Definition) and XML Schema: define the structure of an XML document,
- XSL (XML Style sheet Language), XSLT (XSL Transformation), XSL-FO (XSL Formatting Objects): for presentation.
- XPath, XLink, XPointer, XQuery: for locating, linking and query document.
- DOM (Document Object Model), SAX (Simple API for XML): XML parser.
- XQL (XML Query Language), XSQL: for querying databases.
- and more...
I assume that you have some basic understanding of the HTML
XML Documents
XML (eXtensible Markup Language) is a markup language for marking up the structure of a text document. Unlike HTML:
- XML has no pre-defined tags. You create your own tags for your specific applications. XML tag names are supposed to be self-describing.
- XML's tags are used to mark the meaning of the content, rather than the appearance or presentation of the content.
- XML has strict syntax rules, and is case sensitive.
Like HTML, XML uses markup tags to markup so-called elements. There are two kinds of elements:
- Non-empty element (or container element): The content is enclosed within a pair of matching start-tag and end-tag, in the form of
<element-name>contents</element-name>. The end-tag is preceded by a forward slash "/" and has the same name as the start-tag. For example,<title>Java for dummies</title>. - Empty element (or standalone element): Empty element has no data content. You can use a shorthand notation by closing the start tag with a forward slash “
/” and omit the end tag, in the form of<empty-element-name />. Note that it has the same meaning as<empty-element-name></empty-element-name>. For examples,<out_of_print>,<preferred_customer>.
An XML document exhibits a tree structure. It has one (and only one) root element, and the elements are properly nested. XML documents are text-based, human readable, and are meant to be self-describing. The following is an example of an XML document for a bookstore:
<?xml version="1.0" encoding="UTF-8"?>
<!-- bookstore.xml -->
<bookstore>
<book ISBN="0123456001">
<title>Java For Dummies</title>
<author>Tan Ah Teck</author>
<category>Programming</category>
<year>2009</year>
<edition>7</edition>
<price>19.99</price>
</book>
<book ISBN="0123456002">
<title>More Java For Dummies</title>
<author>Tan Ah Teck</author>
<category>Programming</category>
<year>2008</year>
<price>25.99</price>
</book>
<book ISBN="0123456010">
<title>The Complete Guide to Fishing</title>
<author>Bill Jones</author>
<author>James Cook</author>
<author>Mary Turing</author>
<category>Fishing</category>
<category>Leisure</category>
<language>French</language>
<year>2000</year>
<edition>2</edition>
<price>49.99</price>
</book>
</bookstore>
| XML declaration XML comment Root element start-tag (one and only one root) Child element start-tag (with an attribute in name="value" pair) (proper nesting of child elements) Child element end-tag Second child element start-tag Second child element end-tag Third child element start-tag Third child element end-tag Root element end-tag |
In the above example,
<bookstore> is the root element. It has one child element <book>, which in turn has several children (<title>, <author>, <category>, etc). Some of the elements such as <author> may appear more than once to carry multiple values. The element start-tag may contain so-called attributes in the form of attribute_name="attribute_value" pairs. The following figure shows the appearance of opening an XML document on a web browser. Observe the tree structure. You can click on the "+" and "-" sign to expand and collapse a portion of the tree.
Another example of an XML address book is as follows:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- addressbook.xml -->
<address_book>
<person>
<name>Tan Ah Teck</name>
<tel type="mobile" preferred="yes">98888888</tel>
<tel type="home">68888888</tel>
<email preferred="yes">tan@abc.com</email>
<email>teck@xyz.com</email>
<address type="home">1 Happy Ave</address>
</person>
<person>
<name>Mohd Ali</name>
<tel type="mobile" preferred="yes">97777777</tel>
<tel type="office">67777777</tel>
<email>ali@abc.com</email>
</person>
</address_book>
XML Syntax
There are currently two versions of XML specifications: XML 1.0 and XML 1.1, maintained by W3C.
- Element is the basic unit of an XML document. Each XML element must have a start-tag and end-tag. The tags are enclosed in angle brackets, e.g.,
<title>...</title>. Unlike HTML, closing tag is mandatory. Empty-element tag (or standalone tag) must be properly closed, e.g.,<out_of_print />. - An XML element includes its start-tag, enclosing character data and/or child elements, and the end-tag.
- The element's name can contain letters, numbers, and other Unicode characters, but NOT white spaces. The name must start with a letter, underscore "
_", or colon ":", but cannot start with certain reserved words such asxml. - Each XML document must have one (and only one) root element.
- XML elements must be properly nested. For example,
<book><title>...</book></title>is incorrectly nested. - XML is case sensitive. For example,
<book>and<Book>are considered two different tags. - The start-tag may contain attributes in the form of
attribute_name="attribute_value"pairs. Attributes are used to provide extra information about the element. Unlike HTML, theattribute_valueof an XML attribute must be properly quoted (either in double quotes or single quotes). - Certain characters, such as
<,>, which are used in XML syntax, must be replaced with so-called entity references in the form of&name;. XML has five pre-defined entity references:<(<),>(>),&(&),"("), and'('). - XML comment takes the form of
<!-- comment texts -->, which is the same as HTML. - Unlike HTML, white spaces in the text are preserved. New-line is represented by a Line Feed (
LF) character (0AH).
Well-Form XML Documents
An XML document is well-formed, if its structure meets the XML specification, i.e., it is syntactically correct. A well-formed XML document exhibits a tree-like structure, and can be processed by an XML processor. For example, the tree structure of the "bookstore.xml" is as follows:

Structure of XML Documents
An XML document comprises of the following basic units:
- Element: includes the start-tag, the enclosing character data and/or nested elements, and the end-tag.
- Attribute: defined in the start-tag to provide extra information about the element, in the form of
attribute_name="attribute_value". - Entities References: in the form of
&name;, e.g.,<(<),>(>),&(&),"("), and'('). - Character References: in the form of
&#decimal-number;or&#xhex-code;for replacing any Unicode character, e.g., both©and©can be used for copyright symbol ©. - PCDATA (Parsed Character Data): Text between start-tag and end-tag that will be examined by the parser for entity references and nested elements.
- CDATA (Character Data): Text between start-tag and end-tag that will NOT be examined by the parser for entity references and nested tags.
CDATA Section
As mentioned, special characters (such as "
<", ">", "&") must be referenced through pre-defined entities (such as <, >, '). If your texts contain many special characters, it is cumbersome to replace all of them. Instead a "CDATA (character data) section" can be used. CDATA sections are delimited by "<[CDATA[" and "]]>". The XML processor ignores all the markup within the CDATA section except "]]>". CDATA sections are used for program codes, such as JavaScript or Perl codes.Processing Instruction (PI)
Processing Instruction (PI) tells an application to perform a specific task. It is a command to XML parser or an application program that uses the XML document. PI can be used for inserting non-XML statements, such as scripts, into the document, to be passed to an application for processing.
A PI begins with a "
<?" and ends with "?>". For example,<?xml-stylesheet href="zzz.xsl" type="text/xsl"?>
is a PI (in this case, a XML instruction) that attaches a XSL style sheet to the XML document for layout processing.
The XML Declaration (the first line of an XML document) is also a processing instruction.
<?xml version="1.0" encoding="ISO-8859-1"?>
XML is Extensible
XML is classified as an extensible language as it does not have a pre-defined set of tags. You can create and extend your own tags to suit your application.
You can also expand on an existing set of tags without breaking your existing applications. For example, in the bookstore example, we can define more tags such as
<number_of_pages>,<weight>, <dimension> for shipping purpose, without breaking the existing applications.
Comments
Post a Comment