Stage 2 · Topic 2 - JSP · Session 8: Working with XML
XML stands for Extensible Markup Language. It is derived from SGML (Standard Generalized Markup Language) and it is a text-based markup language.
| Property | Meaning |
|---|---|
| Platform-independent | the same file is meaningful on Windows, Linux and macOS |
| Language-independent | Java, C#, Python and PHP all read the same file |
| Carries data | XML stores and transports data — it does not display it |
XML is a markup language like HTML which is used to store and transport data. It is self-descriptive: the document contains the sender's information, the receiver's information, a heading and a message body.
“The XML does not DO anything — it is just information wrapped in tags.”
Why do we need it? Because the structure of the data is embedded with the data, so there is no need to pre-build the structure to store it — XML understands its own structure. The document is well-formed and properly marked up.
| HTML | XML | |
|---|---|---|
| Syntax and semantics | predefined by the specification | the author defines both |
| Purpose | create a visible presentation for the user | store and transport data |
| Well-formed | not required | required |
| Tags | predefined (<p>, <table>) |
user-defined (<book>, <name>) |
| Who displays it | the browser, on its own | needs a stylesheet or a program — see s8-05 |
“Predefined” is the difference that matters in practice. HTML
tells the browser what <h1> means; XML makes the
browser ask what is a <price>? — and the
answer has to come from somewhere. That somewhere is XSLT or your own
code.
<?xml version="1.0" encoding="UTF-8"?>. The
encoding part is not decoration; it tells the parser how to
read the bytes. Change it to something the file is not actually in and
the parse fails.
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>ZARA</to>
<from>NUHA</from>
<body>Books are due on Friday</body>
</note>
| Rule | Shown above by |
|---|---|
| an XML declaration first | <?xml ... ?> |
| exactly one root element | <note> |
| every start tag is closed | </note> |
| tags nest in pairs | the four children sit inside <note> |
| the structure is the description | no DTD or schema is needed to know that this is a note addressed to somebody |