THE EXPERT’S VOICE® IN JAVA Java XML JSON and — 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 o [email protected] , or visit w ww.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 r [email protected] , or visit w ww.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 w ww.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 ■ Chapter 1: Introducing XML ............................................................1 ■ Chapter 2: Parsing XML Documents with SAX ..............................29 ■ Chapter 3: Parsing and Creating XML Documents with DOM .......57 ■ Chapter 4: Parsing and Creating XML Documents with StAX .......75 ■ Chapter 5: Selecting Nodes with XPath ........................................97 ■ Chapter 6: Transforming XML Documents with XSLT .................119 ■ Chapter 7: Introducing JSON ......................................................133 ■ Chapter 8: Parsing and Creating JSON Objects with mJson ..........149 ■ Chapter 9: Parsing and Creating JSON Objects with Gson .........179 v vi Contents at a Glance ■ Chapter 10: Extracting JSON Values with JsonPath ...................223 ■ Appendix A: Answers to Exercises ............................................241 Index ..............................................................................................279 Contents About the Author ............................................................................xiii About the Technical Reviewer ..........................................................xv Acknowledgments ..........................................................................xvii Introduction .....................................................................................xix ■ Chapter 1: Introducing XML ............................................................1 What Is XML? ..........................................................................................1 Language Features Tour ..........................................................................3 XML Declaration .......................................................................................................3 Elements and Attributes ...........................................................................................5 Character References and CDATA Sections ..............................................................7 Namespaces .............................................................................................................8 Comments and Processing Instructions .................................................................13 Well-Formed Documents .......................................................................14 Valid Documents ...................................................................................15 Document Type Defi nition .......................................................................................15 XML Schema ...........................................................................................................21 Summary ...............................................................................................28 vii viii Contents ■ Chapter 2: Parsing XML Documents with SAX ..............................29 What Is SAX? .........................................................................................29 Exploring the SAX API ............................................................................30 Obtaining a SAX 2 Parser ........................................................................................30 Touring XMLReader Methods ..................................................................................31 Touring the Handler and Resolver Interfaces..........................................................35 Demonstrating the SAX API ...................................................................40 Creating a Custom Entity Resolver ........................................................49 Summary ...............................................................................................54 ■ Chapter 3: Parsing and Creating XML Documents with DOM .......57 What Is DOM? ........................................................................................57 A Tree of Nodes .....................................................................................58 Exploring the DOM API ...........................................................................61 Obtaining a DOM Parser/Document Builder............................................................61 Parsing and Creating XML Documents ...................................................................63 Demonstrating the DOM API ..................................................................67 Summary ...............................................................................................74 ■ Chapter 4: Parsing and Creating XML Documents with StAX .......75 What Is StAX? ........................................................................................75 Exploring StAX .......................................................................................76 Parsing XML Documents.........................................................................................77 Creating XML Documents .......................................................................................85 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 XPath and DOM ...................................................................................103 Advanced XPath ..................................................................................110 Namespace Contexts ............................................................................................110 Extension Functions and Function Resolvers .......................................................111 Variables and Variable Resolvers ..........................................................................115 Summary .............................................................................................118 ■ Chapter 6: Transforming XML Documents with XSLT .................119 What Is XSLT? ......................................................................................119 Exploring the XSLT API .........................................................................120 Demonstrating the XSLT API ................................................................123 Summary .............................................................................................132 ■ Chapter 7: Introducing JSON ......................................................133 What Is JSON? .....................................................................................133 JSON Syntax Tour ................................................................................134 Demonstrating JSON with JavaScript .................................................137 Validating JSON Objects ......................................................................140 Summary .............................................................................................147 ■ Chapter 8: Parsing and Creating JSON Objects with mJson ..........149 What Is mJson? ...................................................................................149 Obtaining and Using mJson ..................................................................................150 Exploring the Json Class .....................................................................150 Creating Json Objects ...........................................................................................151 Learning About Json Objects ................................................................................155 Navigating Json Object Hierarchies ......................................................................163 Modifying Json Objects ........................................................................................165 Validation ..............................................................................................................170 Customization via Factories .................................................................................173 Summary .............................................................................................178
Description: