Java XML and JSON Friesen Jeff download
Java XML and JSON Friesen Jeff download
https://textbookfull.com/product/java-xml-and-json-friesen-jeff/
https://textbookfull.com/product/java-xml-and-json-document-
processing-for-java-se-jeff-friesen/
https://textbookfull.com/product/java-xml-and-json-document-
processing-for-java-se-2nd-edition-jeff-friesen/
https://textbookfull.com/product/learn-java-for-android-
development-friesen-jeff/
https://textbookfull.com/product/pro-restful-apis-design-build-
and-integrate-with-rest-json-xml-and-jax-rs-patni/
The LEGO Castle Book Build Your Own Mini Medieval World
1st Edition Jeff Friesen
https://textbookfull.com/product/the-lego-castle-book-build-your-
own-mini-medieval-world-1st-edition-jeff-friesen/
https://textbookfull.com/product/beginning-xml-with-c-7-xml-
processing-and-data-access-for-c-developers-2nd-edition-bipin-
joshi-auth/
https://textbookfull.com/product/c-and-xml-primer-1st-edition-
jonathan-hartwell-auth/
https://textbookfull.com/product/cybersecurity-law-jeff-kosseff/
https://textbookfull.com/product/international-financial-
management-jeff-madura/
T HE E X P ER T ’S VOIC E ® IN JAVA
Java XML
and JSON
—
Jeff Friesen
Java XML and JSON
Jeff Friesen
Java XML and JSON
Jeff Friesen
Dauphin, Manitoba, Canada
ISBN-13 (pbk): 978-1-4842-1915-7 ISBN-13 (electronic): 978-1-4842-1916-4
DOI 10.1007/978-1-4842-1916-4
Library of Congress Control Number: 2016943840
Copyright © 2016 by Jeff Friesen
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part
of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations,
recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission
or information storage and retrieval, electronic adaptation, computer software, or by similar or
dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are
brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for
the purpose of being entered and executed on a computer system, for exclusive use by the purchaser
of the work. Duplication of this publication or parts thereof is permitted only under the provisions
of the Copyright Law of the Publisher’s location, in its current version, and permission for use must
always be obtained from Springer. Permissions for use may be obtained through RightsLink at the
Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol
with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only
in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of
the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they
are not identified as such, is not to be taken as an expression of opinion as to whether or not they are
subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of
publication, neither the authors nor the editors nor the publisher can accept any legal responsibility
for any errors or omissions that may be made. The publisher makes no warranty, express or implied,
with respect to the material contained herein.
Managing Director: Welmoed Spahr
Lead Editor: Steve Anglin
Technical Reviewer: Wallace Jackson
Editorial Board: Steve Anglin, Pramila Balan, Louise Corrigan, James T. DeWolf,
Jonathan Gennick, Robert Hutchinson, Celestin Suresh John, James Markham,
Susan McDermott, Matthew Moodie, Ben Renow-Clarke, Gwenan Spearing
Coordinating Editor: Mark Powers
Copy Editor: Mary Behr
Compositor: SPi Global
Indexer: SPi Global
Artist: SPi Global
Distributed to the book trade worldwide by Springer Science+Business Media New York,
233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505,
e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a
California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc
(SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail [email protected], or visit www.apress.com.
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional
use. eBook versions and licenses are also available for most titles. For more information, reference our
Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales.
Any source code or other supplementary materials referenced by the author in this text is available
to readers at www.apress.com/9781484219157. For detailed information about how to locate your
book’s source code, go to www.apress.com/source-code/. Readers can also access source code at
SpringerLink in the Supplementary Material section for each chapter.
Printed on acid-free paper
To Dave, the late Father Lucian, Jane, and Rob.
Contents at a
Glance
About the Author ............................................................................ xiii
About the Technical Reviewer ..........................................................xv
Acknowledgments ..........................................................................xvii
Introduction .....................................................................................xix
v
vi Contents at a Glance
Summary ............................................................................................... 28
vii
viii Contents
Summary ............................................................................................... 95
■Chapter 5: Selecting Nodes with XPath ........................................ 97
What Is XPath? ...................................................................................... 97
XPath Language Primer ......................................................................... 97
Location Path Expressions...................................................................................... 98
General Expressions ............................................................................................. 101
Contents ix
■Chapter 9: Parsing and Creating JSON Objects with Gson ......... 179
What Is Gson? ..................................................................................... 179
Obtaining and Using Gson .................................................................................... 180
Chapter 8: Parsing and Creating JSON Objects with mJson ............... 269
Chapter 9: Parsing and Creating JSON Objects with Gson .................. 272
Chapter 10: Extracting JSON Property Values with JsonPath.............. 276
xiii
About the Technical
Reviewer
Wallace Jackson has been writing for leading
multimedia publications about his work in new
media content development since the advent
of Multimedia Producer Magazine nearly two
decades ago. He has authored a half-dozen
Android book titles for Apress, including four
titles in the popular Pro Android series. Wallace
received his undergraduate degree in business
economics from the University of California
at Los Angeles and a graduate degree in MIS
design and implementation from the University
of Southern California. He is currently the CEO
of Mind Taffy Design, a new media content
production and digital campaign design and
development agency.
xv
Acknowledgments
Many people assisted me in the development of this book, and I thank them.
I especially thank Steve Anglin for asking me to write it and Mark Powers for
guiding me through the writing process.
xvii
Introduction
XML and (the more popular) JSON let you organize data in textual formats.
This book introduces you to these technologies along with Java APIs for
integrating them into your Java code. This book introduces you to XML and
JSON as of Java 8 update 60.
Chapter 1 introduces XML, where you learn about basic language features
(such as the XML declaration, elements and attributes, and namespaces).
You also learn about well-formed XML documents and how to validate them
via the Document Type Definition and XML Schema grammar languages.
Chapter 2 focuses on Java’s SAX API for parsing XML documents. You learn
how to obtain a SAX 2 parser; you then tour XMLReader methods along with
handler and entity resolver interfaces. Finally, you explore a demonstration
of this API and learn how to create a custom entity resolver.
Chapter 3 addresses Java’s DOM API for parsing and creating XML
documents. After discovering the various nodes that form a DOM document
tree, you explore the DOM API, where you learn how to obtain a DOM
parser/document builder and how to parse and create XML documents.
Chapter 4 places the spotlight on Java’s StAX API for parsing and creating
XML documents. You learn how to use StAX to parse XML documents with
stream-based and event-based readers, and how to create XML documents
with stream-based and event-based writers.
Moving on, Chapter 5 presents Java’s XPath API for simplifying access to
a DOM tree’s nodes. You receive a primer on the XPath language, learning
about location path expressions and general expressions. You also explore
advanced features starting with namespace contexts.
Chapter 6 completes my coverage of XML by targeting Java’s XSLT API. You
learn about transformer factories and transformers, and much more.
xix
xx Introduction
Note You can download this book’s source code by pointing your web browser
to www.apress.com/9781484219157 and clicking the Source Code tab
followed by the Download Now link.
Chapter 1
Introducing XML
Applications commonly use XML documents to store and exchange data.
XML defines rules for encoding documents in a format that is both
human-readable and machine-readable. This chapter introduces XML, tours
the XML language features, and discusses well-formed and valid documents.
What Is XML?
XML (eXtensible Markup Language) is a metalanguage (a language used to
describe other languages) for defining vocabularies (custom markup languages),
which is the key to XML’s importance and popularity. XML-based vocabularies
(such as XHTML) let you describe documents in a meaningful way.
XML vocabulary documents are like HTML (see http://en.wikipedia.org/
wiki/HTML) documents in that they are text-based and consist of markup
(encoded descriptions of a document’s logical structure) and content
(document text not interpreted as markup). Markup is evidenced via tags
(angle bracket-delimited syntactic constructs) and each tag has a name.
Furthermore, some tags have attributes (name-value pairs).
<recipe>
<title>
Grilled Cheese Sandwich
</title>
<ingredients>
<ingredient qty="2">
bread slice
</ingredient>
<ingredient>
cheese slice
</ingredient>
<ingredient qty="2">
margarine pat
</ingredient>
</ingredients>
<instructions>
Place frying pan on element and select medium heat. For each bread
slice, smear one pat of margarine on one side of bread slice. Place cheese
slice between bread slices with margarine-smeared sides away from the
cheese. Place sandwich in frying pan with one margarine-smeared side in
contact with pan. Fry for a couple of minutes and flip. Fry other side for a
minute and serve.
</instructions>
</recipe>
Listing 1-1 presents an XML document that describes a recipe for making
a grilled cheese sandwich. This document is reminiscent of an HTML
document in that it consists of tags, attributes, and content. However, that’s
CHAPTER 1: Introducing XML 3
where the similarity ends. Instead of presenting HTML tags such as <html>,
<head>, <img>, and <p>, this informal recipe language presents its own
<recipe>, <ingredients>, and other tags.
Note Although Listing 1-1’s <title> and </title> tags are also found in
HTML, they differ from their HTML counterparts. Web browsers typically display
the content between these tags in their title bars. In contrast, the content
between Listing 1-1’s <title> and </title> tags might be displayed as a
recipe header, spoken aloud, or presented in some other way, depending on the
application that parses this document.
XML Declaration
An XML document usually begins with the XML declaration, which is special
markup telling an XML parser that the document is XML. The absence of the
XML declaration in Listing 1-1 reveals that this special markup isn’t mandatory.
When the XML declaration is present, nothing can appear before it.
The XML declaration minimally looks like <?xml version="1.0"?> in which
the nonoptional version attribute identifies the version of the XML
specification to which the document conforms. The initial version of this
specification (1.0) was introduced in 1998 and is widely implemented.
Note The World Wide Web Consortium (W3C), which maintains XML, released
version 1.1 in 2004. This version mainly supports the use of line-ending
characters used on EBCDIC platforms (see http://en.wikipedia.org/
wiki/EBCDIC) and the use of scripts and characters that are absent from
Unicode 3.2 (see http://en.wikipedia.org/wiki/Unicode). Unlike
XML 1.0, XML 1.1 isn’t widely implemented and should be used only by those
needing its unique features.
4 CHAPTER 1: Introducing XML
XML supports Unicode, which means that XML documents consist entirely
of characters taken from the Unicode character set. The document’s
characters are encoded into bytes for storage or transmission, and the
encoding is specified via the XML declaration’s optional encoding attribute.
One common encoding is UTF-8 (see http://en.wikipedia.org/wiki/UTF-8),
which is a variable-length encoding of the Unicode character set. UTF-8 is a
strict superset of ASCII (see http://en.wikipedia.org/wiki/ASCII), which
means that pure ASCII text files are also UTF-8 documents.
Note In the absence of the XML declaration or when the XML declaration’s
encoding attribute isn’t present, an XML parser typically looks for a special
character sequence at the start of a document to determine the document’s
encoding. This character sequence is known as the byte-order-mark (BOM) and
is created by an editor program (such as Microsoft Windows Notepad) when it
saves the document according to UTF-8 or some other encoding. For example,
the hexadecimal sequence EF BB BF signifies UTF-8 as the encoding. Similarly,
FE FF signifies UTF-16 big endian (see https://en.wikipedia.org/
wiki/UTF-16), FF FE signifies UTF-16 little endian, 00 00 FE FF signifies
UTF-32 big endian (see https://en.wikipedia.org/wiki/UTF-32), and
FF FE 00 00 signifies UTF-32 little endian. UTF-8 is assumed when no
BOM is present.
If you’ll never use characters apart from the ASCII character set, you can
probably forget about the encoding attribute. However, when your native
language isn’t English or when you’re called to create XML documents that
include non-ASCII characters, you need to properly specify encoding.
For example, when your document contains ASCII plus characters from
a non-English Western European language (such as ç, the cedilla used in
French, Portuguese, and other languages), you might want to choose
ISO-8859-1 as the encoding attribute’s value—the document will probably
have a smaller size when encoded in this manner than when encoded with
UTF-8. Listing 1-2 shows you the resulting XML declaration.
The final attribute that can appear in the XML declaration is standalone.
This optional attribute, which is only relevant with DTDs (discussed later),
determines if there are external markup declarations that affect the
information passed from an XML processor (a parser) to the application.
Its value defaults to no, implying that there are, or may be, such declarations.
A yes value indicates that there are no such declarations. For more
information, check out “The standalone pseudo-attribute is only relevant if a
DTD is used” article at (www.xmlplease.com/xml/xmlquotations/standalone).
Figure 1-1. Listing 1-1’s tree structure is rooted in the recipe element
<?xml version="1.0"?>
<article title="The Rebirth of JavaFX" lang="en">
<abstract>
JavaFX 2 marks a significant milestone in the history of JavaFX. Now
that Sun Microsystems has passed the torch to Oracle, we have seen the
demise of JavaFX Script and the emergence of Java APIs (such as <code-
inline>javafx.application.Application</code-inline>) for interacting with
this technology. This article introduces you to this new flavor of JavaFX,
where you learn about JavaFX 2 architecture and key APIs.
</abstract>
<body>
</body>
</article>
This document’s root element is article, which contains abstract and body
child elements. The abstract element mixes content with a code-inline
element, which contains content. In contrast, the body element is empty.
Note As with Listings 1-1 and 1-2, Listing 1-3 also contains whitespace
(invisible characters such as spaces, tabs, carriage returns, and line feeds). The
XML specification permits whitespace to be added to a document. Whitespace
appearing within content (such as spaces between words) is considered part
of the content. In contrast, the parser typically ignores whitespace appearing
between an end tag and the next start tag. Such whitespace isn’t considered
part of the content.
An XML element’s start tag can contain one or more attributes. For example,
Listing 1-1’s <ingredient> tag has a qty (quantity) attribute and Listing 1-3’s
<article> tag has title and lang attributes. Attributes provide additional
details about elements. For example, qty identifies the amount of an
ingredient that can be added, title identifies an article’s title, and lang
identifies the language in which the article is written (en for English).
Attributes can be optional. For example, when qty isn’t specified, a default
value of 1 is assumed.
CHAPTER 1: Introducing XML 7
Note Element and attribute names may contain any alphanumeric character
from English or another language, and may also include the underscore (_),
hyphen (-), period (.), and colon (:) punctuation characters. The colon should
only be used with namespaces (discussed later in this chapter), and names
cannot contain whitespace.
<?xml version="1.0"?>
<svg-examples>
<example>
The following Scalable Vector Graphics document describes a blue-
filled and black-stroked rectangle.
<![CDATA[<svg width="100%" height="100%" version="1.1"
xmlns="http://www.w3.org/2000/svg">
<rect width="300" height="100"
style="fill:rgb(0,0,255);stroke-width:1; stroke:rgb(0,0,0)"/>
</svg>]]>
</example>
</svg-examples>
Namespaces
It’s common to create XML documents that combine features from different
XML languages. Namespaces are used to prevent name conflicts when
elements and other XML language features appear. Without namespaces, an
XML parser couldn’t distinguish between same-named elements or other
language features that mean different things, such as two same-named
title elements from two different languages.
Random documents with unrelated
content Scribd suggests to you:
Crowns
1. Crown of England. 2. Russian Crown. 3.
French Crown. 4. Austrian Crown. 5. Imperial
Crown (Charlemagne’s).
[389]
Conquering the Alps.
Immense labor and great ingenuity were required to haul the monster Italian guns
up the steep mountain sides to their positions.
[390]
The Most Formidable of the French Army’s Trench Artillery
80-m.m. mountain gun loaded with air-mine weighing 130 pounds. These mines
can be thrown for a considerable distance and create havoc in the enemy’s
trenches if the aim is true.
[391]
The Bennett-Mercier Machine Gun
This new automatic machine gun has been adopted by the United States Army,
Navy and Marine Corps. It is handled by two men, one to aim and fire it, the other
to feed the cartridges which are held in brass clips of 30 each. The complete gun
weighs only about 35 pounds, fires 400 shots per minute, using regular 30-caliber
Springfield rifle cartridges, with a maximum range of 3 miles and an effective
range of about 2,000 yards. The weapon is air cooled and can be fired steadily for
about 10 minutes without undue heating.
[392]
Three-inch Field Gun, Long Recoil Carriage and Limber
Weight of gun, carriage and limber complete, including 36
rounds of ammunition, 4,200 pounds; ground clearance,
22.5 inches. Seats are provided on axle of carriage for two
gunners in transportation, one of whom operates the road
brake.
Courtesy of the Bethlehem Steel Co.
Rapid-Fire Gun
Six-inch rapid-fire gun equipped with patented two-handed
elevating gear, consisting of two hand wheels on opposite
ends of the same shaft, the handles being 180 degrees
apart. The pointer uses both hands in elevating and
depressing the gun. The electric firing trigger A is worked by
the index finger of the right hand without releasing the
handle. There is a second firing handle B attached to the
slide, for firing either electrically or percussively.
Courtesy of the Bethlehem Steel Co.
[396]
Coast Defense Gun
A modern 14-inch coast defense gun at Sandy Hook. The gun is mounted on a
disappearing carriage, which lowers it out of sight behind the breastworks after
firing. This is one of the most powerful guns in the world, firing a projectile which
would pierce the armor of a battleship more than five miles away.
[397]
Copyright by Underwood & Underwood, N. Y.
A Battery of 12-inch Coast Defense Mortars
These powerful weapons fire a projectile which weighs from 700 to 1,046 pounds,
depending on the range desired, and which is capable of piercing the deck armor
of any battleship. They have a range of 20,000 yards with the 700-pound
projectile. The gun is 162⁄3 feet long and is fired only at elevations between 45°
and 65°.
[398]
Large image (1143 x 1499 px, 703 kB).
Giant Guns—Their Muzzle-Energy, Projectiles, and Penetrating
Powers
The British 13.5, which was known as the 12-inch-A until the
“Lion” was launched, has a length of 45 calibers, and a muzzle-
energy ten per cent greater than that of the 50-caliber 12-inch
of 1909 and 1910. It may be noted that the caliber is the
diameter of the bore of a gun. The statement that a gun has a
length of 45 calibers, for example, implies that the gun is forty-
five times the bore’s diameter. Thus a 12-inch gun of 45
calibers is 45 feet long.
These pieces are prepared with the utmost care to prevent any
defective material entering into the make-up of the gun. After the
parts are put together a thorough forging follows, either by use of
hammer or press, the latter being now used in preference. The usual
practice in forging is to continue it until the ingot is decreased to one-
half its original thickness and is within two inches of the desired
diameter of the finished work. It is then annealed with great care to
relieve the strains set up in the metal by the forging and next goes to
the machine shop to be rough bored and turned. The final boring
takes place after a second annealing. The above is only a rapid
sketch of the total process, in which elaborate care is taken to
prevent imperfection of any kind.
Military cannon are divided into three classes, based upon the length
of caliber, and technically known as guns, mortars and howitzers. In
guns the length is relatively great, in mortars relatively small,
compared to their calibers. Howitzers form a class between guns and
mortars in length. The field guns of the American army are the 3.6-
inch breech-loading mortars, and the 3.6-inch heavy and 3.2-inch
light guns. The siege guns in the service are the 5-inch siege guns,
the 7-inch howitzer, and the 7-inch mortar. The coast defense artillery
consists of the 8-, 10-, 12- and 16-inch guns and the 12-inch
mortars. In the recent European war very heavy cannon were used
for field service, pieces of the size usually placed in forts being drawn
to the field by powerful tractors, set on concrete platforms and used
in attacks on fortified cities. It was through the use of such ordnance
that the German army so easily reduced the strongly fortified Belgian
cities.
[402]
[404]
Range Finder and Chart Attachment[34]. (See page 410.)
[405]
Firing Gear for Guns.[35] (See page 410.)
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com