UNPKG

epubjs

Version:

Render ePub documents in the browser, across many devices

45 lines (43 loc) 5.12 kB
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head><title>The Butler Did It</title><link rel="stylesheet" href="core.css" type="text/css"/><meta name="generator" content="DocBook XSL Stylesheets V1.74.0"/></head><body><div class="sect1" title="The Butler Did It"><div class="titlepage"><div><div><h1 class="title"><a id="I_sect124_id827679"/>The Butler Did It</h1></div></div></div><p>This chapter is one of the longest in this book and deals with many APIs and concepts. Part of the reason for this is that there has been a great deal of evolution of XML tools over time in order to support working with XML at different levels of abstraction. We’re going to introduce you to the APIs that we think remain important and useful in this chapter and to some extent we’ll do this by starting at the bottom level and working our way up. First we’ll cover basic XML concepts and low-level APIs such as the event-driven <a id="I_indexterm24_id827694" class="indexterm"/><a id="I_indexterm24_id827699" class="indexterm"/>SAX (Simple API for Java) and model-building DOM (Document Object Model). We’ll also discuss related technologies such as XML Schema validation, XPath queries, and XSL (Extensible Style Sheet) transformation. Later in this chapter, we’ll discuss the higher-level JAXB, Java XML Binding API, for mapping plain Java objects directly to XML and back.</p><p>This means that for some of you, the most useful material may be toward the end of this chapter where we cover the high-level tools. So we want to reassure you that things get more interesting as the chapter progresses. When we reach the section on JAXB we’ll see that we can take plain old Java objects (POJOs) and write them to XML by adding (in the simplest case) a one-line annotation. The following snippet shows a Java <code class="literal">Person</code> and <code class="literal">Address</code> class and the corresponding XML that they would map to by default.</p><a id="I_programlisting24_id827733"/><pre class="programlisting"><code class="nd">@XmlRootElement</code> <code class="kd">public</code> <code class="kd">class</code> <code class="nc">Person</code> <code class="o">{</code> <code class="kd">public</code> <code class="n">String</code> <code class="n">name</code><code class="o">;</code> <code class="kd">public</code> <code class="n">Address</code> <code class="n">address</code><code class="o">;</code> <code class="kd">public</code> <code class="kt">int</code> <code class="n">age</code><code class="o">;</code> <code class="o">}</code> <code class="kd">public</code> <code class="kd">class</code> <code class="nc">Address</code> <code class="o">{</code> <code class="kd">public</code> <code class="n">String</code> <code class="n">city</code><code class="o">,</code> <code class="n">street</code><code class="o">;</code> <code class="kd">public</code> <code class="kt">int</code> <code class="n">number</code><code class="o">,</code> <code class="n">zip</code><code class="o">;</code> <code class="o">}</code> <code class="o">&lt;</code><code class="n">person</code><code class="o">&gt;</code> <code class="o">&lt;</code><code class="n">name</code><code class="o">&gt;</code><code class="n">Pat</code> <code class="n">Niemeyer</code><code class="o">&lt;/</code><code class="n">name</code><code class="o">&gt;</code> <code class="o">&lt;</code><code class="n">address</code><code class="o">&gt;</code> <code class="o">&lt;</code><code class="n">city</code><code class="o">&gt;</code><code class="n">St</code><code class="o">.</code> <code class="n">Louis</code><code class="o">&lt;/</code><code class="n">city</code><code class="o">&gt;</code> <code class="o">&lt;</code><code class="n">street</code><code class="o">&gt;</code><code class="n">Java</code> <code class="n">St</code><code class="o">.&lt;/</code><code class="n">street</code><code class="o">&gt;</code> <code class="o">&lt;</code><code class="n">number</code><code class="o">&gt;</code><code class="mi">1234</code><code class="o">&lt;/</code><code class="n">number</code><code class="o">&gt;</code> <code class="o">&lt;</code><code class="n">zip</code><code class="o">&gt;</code><code class="mi">54321</code><code class="o">&lt;/</code><code class="n">zip</code><code class="o">&gt;</code> <code class="o">&lt;/</code><code class="n">address</code><code class="o">&gt;</code> <code class="o">&lt;</code><code class="n">age</code><code class="o">&gt;</code><code class="mi">42</code><code class="o">&lt;/</code><code class="n">age</code><code class="o">&gt;</code> <code class="o">&lt;/</code><code class="n">person</code><code class="o">&gt;</code></pre><p>But before we go there, let’s take a step back and talk about the motivation and “rules” of XML documents and some of the ways we can parse and generate them.</p></div></body></html>