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

JAXP Serialization

Although DOM is a read-write API in memory, it's sorely lacking when it comes to moving its in-memory data structure back out onto a disk, a network socket, or some other stream. Eventually, this omission will be rectified in DOM3. In the meantime, you have the choice of using either implementation-specific serialization classes or JAXP. The implementation-specific serialization classes generally provide more customization and features, but JAXP is sufficient for basic uses.

JAXP doesn't include a serialization package, but you can hack basic output through the javax.xml.transform package by conveniently "forgetting" to install a transform. :-) The pattern is the same as parsing a document with JAXP. The basic steps are as follows:

  1. Use the static TransformerFactory.newInstance() factory method to return a javax.xml.transform.TransformerFactory object.

  2. Use the newTransformer() method of this TransformerFactory object to return an implementation-specific instance of the abstract javax.xml.transform.Transformer class.

  3. Construct a new javax.xml.transform.dom.DOMSource object from your DOM Document object.

  4. Construct a new javax.xml.transform.stream.StreamResult object connected to the OutputStream you want to write the document onto.

  5. Pass both the source and the result objects to the transform() method of the Transformer object created in step 2.

We can use this procedure to write a simple driver program for Example 9.12. Example 9.14 first uses JAXP to build a DOM Document object from a URL, then passes this object to the Restructurer.processNode() method, and finally serializes the whole document onto System.out.

Example 9.14 Using JAXP to Read and Write an XML Document
import javax.xml.parsers.*; // JAXP
import javax.xml.transform.*; // JAXP
import javax.xml.transform.dom.DOMSource; // JAXP
import javax.xml.transform.stream.StreamResult; // JAXP
import org.xml.sax.SAXException;
import org.w3c.dom.Document;
import java.io.IOException;

public class RestructureDriver {

  public static void main(String[] args) {

    if (args.length <= 0) {
      System.out.println("Usage: java RestructureDriver URL");
      return;
    }
    String url = args[0];
    try {
      // Find a parser
      DocumentBuilderFactory factory
       = DocumentBuilderFactory.newInstance();
      factory.setNamespaceAware(true);
      DocumentBuilder parser = factory.newDocumentBuilder();

      // Read the document
      Document document = parser.parse(url);

      // Modify the document
      Restructurer.processNode(document);

      // Write it out again
      TransformerFactory xformFactory
       = TransformerFactory.newInstance();
      Transformer idTransform = xformFactory.newTransformer();
      Source input = new DOMSource(document);
      Result output = new StreamResult(System.out);
      idTransform.transform(input, output);

    }
    catch (SAXException e) {
      System.out.println(url + " is not well-formed.");
    }
    catch (IOException e) {
      System.out.println(
       "Due to an IOException, the parser could not read " + url
      );
    }
    catch (FactoryConfigurationError e) {
      System.out.println("Could not locate a factory class");
    }
    catch (ParserConfigurationException e) {
      System.out.println("Could not locate a JAXP parser");
    }
    catch (TransformerConfigurationException e) {
      System.out.println("This DOM does not support transforms.");
    }
    catch (TransformerException e) {
      System.out.println("Transform failed.");
    }

  }

}

You'll learn how to actually use these classes for their intended purposes of XSLT transformation in Chapter 17.

    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