Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax Free Open Book

Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax

Previous Section Next Section

Chapter 16. XPath

Much of the code in this book has involved navigating the tree structure of an XML document to find particular nodes. For example, the XML-RPC servlet in Example 10.13 read a client request looking for int elements. Such code can become quite involved and fragile if you aren't very careful. As the code walks down the tree hierarchy, loading one child after the other, a single misplaced or misnamed element may cause the program to fail. If an element isn't where it's expected to be, then the chain of method calls that gives directions to the desired elements will be broken. What's needed is a way to specify which nodes a program needs without explicitly specifying how the program navigates to those nodes.

XPath is a fourth-generation declarative language for locating nodes in XML documents. An XPath location path specifies which nodes from the document you want. It says nothing about what algorithm is used to find these nodes. You simply pass an XPath statement to a method, and the XPath engine is responsible for figuring out how to find all of the nodes that satisfy that expression. This is much more robust than writing the detailed search and navigation code yourself using DOM, SAX, or JDOM. XPath searches often succeed even when the document format is not quite what you expected. For example, a comment in the middle of a paragraph of text may break DOM code that expects to see contiguous text. XPath wouldn't be phased by this. Many XPath expressions are resistant even to much more significant alterations, such as changing the names or namespaces of ancestor elements, reordering the children of an element, or even adding or subtracting entire levels from the tree hierarchy.

In the large, using XPath in a Java program is like using SQL in a Java program. To extract information from a database, you write a SQL statement indicating what information you want, and you ask JDBC to fetch it for you. You neither know nor care how JDBC communicates with the database. Similarly with XML, you write an XPath expression to indicate what information you want from an XML document and ask the XPath engine to fetch it, without concerning yourself with the exact algorithms XPath uses to search the XML document.

    Previous Section Next Section


         Main Menu
    Main Page
    Table of content
    Copyright
    Praise for Elliotte Rusty Harold's 'Processing XML with Java™'
    List of Examples
    List of Figures
    Preface
    Part I: XML
    Part II: SAX
    Part III: DOM
    Part IV: JDOM
    Part V: XPath/XSLT
    Chapter 16. XPath
    Queries
    The XPath Data Model
    Location Paths
    Expressions
    XPath Engines
    DOM Level 3 XPath
    Jaxen
    Summary
    Chapter 17. XSLT
    Part VI: Appendixes


    More Books
    PHP Hacks
    Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
    The Koran (Holy Qur'an)
    Macromedia Flash 8 Bible
    Search Engine Optimization for Dummies
    YouTube Traffic
    PHP 5 for Dummies
    Harry Potter and The Chamber of Secrets
    Harry Potter and the Sorcerer's Stone
    The Pilgrim's Progress
    Wireless Hacks
    Flash Hacks. 100 Industrial-Strength Tips & Tools
    PayPal Hacks. 100 Industrial-Strength Tips and Tools
    Amazon Hacks
    Pdf Hacks
    The Da Vinci Code
    Google Hacks
    The Holy Bible
    Windows XP For Dummies
    Harry Potter and the Half-Blood Prince
    Seo Book
    Upgrading and Repairing Networks
    Macromedia Dreamweaver 8 UNLEASHED
    Windows XP Annoyances
    Windows XP Hacks
    Microsoft Windows XP Power Toolkit
    Teach Yourself MS Office In 24Hours
    iPod & iTunes Missing Manual
    PC Hacks 100 Industrial-Strength Tips and Tools
    PC Overclocking, Optimization, and Tuning - 2th Edition
    PC Hardware In A Nutshell 3rd Edition
    PC Hardware in a Nutshell, 2nd Edition
    Upgrading and Repairing PCs
    Google for Dummies
    MySQL Cookbook
    Teach Yourself Macromedia Flash 8 In 24 Hours
    PHP CookBook
    Sams Teach Yourself JavaScript in 24 Hours
    PHP5 Manual
    Free Games Paper Airplanes
    500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele