Preview Java XML Tutorial
Total Page:16
File Type:pdf, Size:1020Kb
XML 1 XML About the Tutorial XML (Extensible Markup Language) is a very popular simple text-based language that can be used as a mode of communication between different applications. It is considered as a standard means to transport and store data. JAVA provides excellent support and a rich set of libraries to parse, modify or inquire XML documents. This tutorial will teach you basic XML concepts and the usage of various types of Java based XML parsers in a simple and intuitive way. Audience This tutorial has been prepared for beginners to help them understand the basic-to-advanced concepts related to XML parsing using Java Programming language. After completing this tutorial, you will find yourself at a moderate level of expertise in XML parsing using Java from where you can take yourself to higher levels of expertise. Prerequisites Knowledge of computers is not a prerequisite to follow the contents of this tutorial. This tutorial assumes no background in computers or computer programming, though basic knowledge of computer terminologies will help in understanding the given concepts very easily. Disclaimer & Copyright Copyright 2016 by Tutorials Point (I) Pvt. Ltd. All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any contents or a part of contents of this e-book in any manner without written consent of the publisher. We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or in this tutorial, please notify us at [email protected] 2 XML Table of Contents About the Tutorial ........................................................................................................................................... 1 Audience .......................................................................................................................................................... 1 Prerequisites .................................................................................................................................................... 1 Disclaimer & Copyright .................................................................................................................................... 1 Table of Contents ............................................................................................................................................ 2 1. Java XML − Overview ................................................................................................................................ 5 What is XML? ................................................................................................................................................... 5 Example ........................................................................................................................................................... 5 Advantages ...................................................................................................................................................... 6 Disadvantages.................................................................................................................................................. 6 2. Java XML − Parsers .................................................................................................................................... 7 What is XML Parser? ........................................................................................................................................ 7 JAVA DOM PARSER ...................................................................................................................... 8 3. Java DOM Parser − Overview .................................................................................................................... 9 When to Use? .................................................................................................................................................. 9 What you get? ................................................................................................................................................. 9 Advantages ...................................................................................................................................................... 9 DOM Interfaces ............................................................................................................................................... 9 Common DOM Methods ............................................................................................................................... 10 4. Java DOM Parser − Parse XML Document ............................................................................................... 11 Steps to Using JDOM ..................................................................................................................................... 11 Demo Example............................................................................................................................................... 12 5. Java JDOM Parser − Query XML Document ............................................................................................. 16 Demo Example............................................................................................................................................... 16 6. Java JDOM Parser − Create XML Document ............................................................................................ 19 Demo Example............................................................................................................................................... 19 7. Java JDOM Parser − Modify XML Document ........................................................................................... 22 Demo Example............................................................................................................................................... 22 JAVA SAX PARSER ...................................................................................................................... 25 3 XML 8. Java SAX Parser − Overview .................................................................................................................... 26 When to Use? ................................................................................................................................................ 26 Disadvantages of SAX .................................................................................................................................... 26 ContentHandler Interface .............................................................................................................................. 27 Attributes Interface ....................................................................................................................................... 27 9. Java SAX Parser - Parse XML Document .................................................................................................. 28 Demo Example............................................................................................................................................... 28 10. Java SAX Parser − Query XML Document ................................................................................................ 34 Demo Example............................................................................................................................................... 34 11. Java SAX Parser − Create XML Document ................................................................................................ 40 12. Java SAX Parser − Modify XML Document ............................................................................................... 41 Demo Example............................................................................................................................................... 41 JDOM XML PARSER ................................................................................................................... 47 13. Java DOM Parser − Overview .................................................................................................................. 48 Environment Setup ........................................................................................................................................ 48 When to Use? ................................................................................................................................................ 48 What You Get? .............................................................................................................................................. 48 Advantages .................................................................................................................................................... 48 JDOM Classes ................................................................................................................................................. 48 Common JDOM Methods .............................................................................................................................. 49 14. Java DOM Parser − Parse XML Document ..............................................................................................