"How's it going?"

"Great. Can't complain. Today Bilaabo told me about JavaScript. Not everything, of course, but more than just a little. Of course, I still haven't written anything in JS, but I don't think it would be difficult."

"And Ellie told me about JSON serialization. And you explained the Jackson framework and how to set up 'polymorphic deserialization' using annotations."

"No way! You're smart now, Amigo! A real stud!"

"And then some!"

"OK. Let's get to work. Today we have a new, interesting topic: XML."

XML, serialization into XML - 1

"XML is a standard for representing data that can be easily read by humans—and even more easily by programs. Here's an example XML file:"

XML
<data>
<owner first="Adam" last="Shelton">
<address>London</address>
</owner>
<cat name="Missy" age="15"/>
</data>

"The foundation of XML is tags. A tag is a word in angle brackets (greater-than and less-than signs). There are opening and closing tags. Each opening tag has exactly one corresponding closing tag. Opening tags can have attributes."

"Tags can be nested inside a tag, thereby creating an element tree. The top-level tag is called the root: it has child tags, which in turn have their own child tags."

"Here are some examples:"

Tag Description
<data> Opening data tag
</data> Closing data tag
<cat name = "Missy" age = "15"> A tag with attributes. Attribute values are wrapped in quotes
<data>
<owner>
<cat name = "Missy"/>
</owner>
</data>
Nested tags.
<cat name = "Missy" age = "15" /> A self-closing tag.
Such tags don't need closing tags.
And they can't have child tags.
<info>
Any kind of information can go here
</info>
A tag can contain text data
<info>
Any kind
<data xxx = "yyy">
</data>
of information
<data 2xxx = "yyy"/>
can go here
</info>
A tag may contain text data interspersed with other tags.

"It looks easy. What kinds of tags are there?"

"Any kind. There are no reserved tags. XML is a language for describing any data. People come up with the tags that meet their needs and agree on how to use them."

"Essentially, XML is a way to write data as an element tree that a computer can understand."

"I think I get it now. By the way, I have a question."

"JSON is used to send data from a browser to a server, but where is XML used?"

"In the same places where JSON is used: for storing and sending data."

"Okay, let's continue."

"Imagine that you have one shared XML file that stores data for a program being written by twenty people. Each of them comes up with their own tags, and they quickly start interfering with each other."

"To ensure that tags are unique, prefixes were invented. This is how they look:"

Tags Description
<animal:cat> A cat tag with the animal prefix
<animal:cat>
</animal:cat>
<zoo:cat>
</zoo:cat>
Two cat tags with different prefixes.
<animal:cat zoo:name = "MX"> A cat tag with the animal prefix. A name attribute with the zoo prefix.

"Prefixes are also called namespaces. If we call them namespaces, then the last description in the table becomes 'A cat tag with the animal namespace. A name attribute with the zoo namespace.'"

"By the way, do you remember that in Java each class has a short name and a long unique name that includes the package name, which is also specified when importing the package?"

"Yep."

"Well, prefixes also have a unique long name, and it is also specified when imported:"

Example
<data xmlns:soap="http://cxf.apache.org/bindings/soap">
<soap:item>
<soap:info/>
</soap:item>
</data>

"'xmlns:soap' means 'XMLnamespace SOAP'"

"You can also set the unique name of tags that have no prefix:"

Example
<data xmlns = "http://www.springframework.org/schema/beans"
xmlns:soap = "http://cxf.apache.org/bindings/soap"
xmlns:task = "http://www.springframework.org/schema/task">
<soap:item>
<soap:info/>
<task:info/>
</soap:item>
</data>

"'xmlns=…' sets the namespace for the empty prefix. In other words, it sets the namespace for tags without a prefix, such as data in the example above."

"You can have as many namespaces in a document as you want, but each must have a unique name."

"I see. Why do these namespaces have such strange unique names?"

"They usually indicate a URL that points to a document that describes the namespace and/or its XML tags."

"You dumped a lot of information on me today. What else is there?"

"There's still a little more."

"First, XML has a header. It is a special line that describes the XML version and file encoding. "It usually looks like this:"

"It usually looks like this:"

Example
<?xml version = "1.0" encoding = "UTF-8"?>
<data xmlns:soap = "http://cxf.apache.org/bindings/soap">
<soap:item>
<soap:info/>
</soap:item>
</data>

"You can also add comments to XML. To start a comment, use '<!--'. To end it, use '-->'."

Example
<?xml version = "1.0" encoding = "UTF-8"?>
<data xmlns:soap = "http://cxf.apache.org/bindings/soap">
<soap:item>
<!-- <soap:info/> -->
</soap:item>
<!-- This is also a comment  -->
</data>

"I understand so far."

"Certain symbols (< > " &) have special meaning in XML, so they can't be used elsewhere. We can get around this restriction with an escape sequence—a set of characters used to represent other characters/symbols. Here are some of them:"

Escape sequence Symbol that it replaces
&amp; &
&quot; «
&lt; <
&gt; >
&apos;

"And here is an example of code embedded in XML:"

Java code Java code in XML
if (a < b)
System.out.println("a is minimum");
<code>
if (a &lt; b)
 System.out.println(&quot;a is minimum&quot;);
</code>

"Uh... That doesn't look pretty."

"You'll recall that in Java some characters are also escaped? For example, "\". And that this sequence must also be written twice when writing to a String? So this is a common occurrence."

"OK."

"That's all I have for today."

"Hooray. I can finally take a break."