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

The NodeList Interface

DOM stores the lists of children of each node in NodeList objects. Example 9.13 illustrates this very basic indexed list. Indexes start from 0 and continue to the length of the list minus 1, just like Java arrays.

Example 9.13 The NodeList Interface
package org.w3c.dom;

public interface NodeList {

  public Node item(int index);
  public int  getLength();

}

Instances of this interface are returned by the getChildNodes() method in the Node interface, as well as by various methods in its subinterfaces that we'll encounter in Chapter 10.

The actual data structure that backs this interface can be a linked list, an array, or something else. Details vary from implementation to implementation. Whatever the concrete data structure is, you can use node lists to simplify operations that iterate over children. For example, the followNode() method in Example 9.11 could be rewritten using NodeList instead of getNextSibling() like this:

public void followNode(Node node) throws IOException {

  printer.writeNode(node);
  // Process the children
  NodeList children = node.getChildNodes();
  for (int i = 0; i < children.getLength(); i++) {
    Node child = children.item(i);
    followNode(child); // recursion
  }

}

This still walks the tree such as in several of the earlier programs; however, the algorithm is somewhat more obvious because it uses more list iteration and less recursion. (Recursion is still necessary to descend the tree but not to move from one sibling to the next.)

Whether or not this variant is more efficient than the original version that only uses the Node interface depends on the concrete implementation. It may indeed be faster if the implementation classes store children in arrays. It may not be faster if the implementation classes use linked lists. Either way the difference is unlikely to be significant. Using the approach that feels more natural to you is a lot more important than the marginal speed you might gain by picking one over the other.

Node lists are live. That is, if you add or delete a node from the list, the change is reflected in the document and vice versa. This can make it a little tricky to keep track of where you are in the list, as the length can keep changing, and nodes can move from one place in the list to another.

Node lists (and pretty much everything else in DOM) are not thread safe. If one thread is writing to or modifying a NodeList while another thread is reading from it, data corruption is almost guaranteed. Because node lists are live, code can be unsafe even when no other thread has a reference to that particular node list, as long as some other thread has a reference to the Document from which the NodeList was built.

    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
    Chapter 9. The Document Object Model
    The Evolution of DOM
    DOM Modules
    Application-Specific DOMs
    Trees
    DOM Parsers for Java
    Parsing Documents with a DOM Parser
    The Node Interface
    The NodeList Interface
    JAXP Serialization
    DOMException
    Choosing between SAX and DOM
    Summary
    Chapter 10. Creating XML Documents with DOM
    Chapter 11. The DOM Core
    Chapter 12. The DOM Traversal Module
    Chapter 13. Output from DOM
    Part IV: JDOM
    Part V: XPath/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