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

Receiving Processing Instructions

Each processing instruction that the parser reads is passed to the processingInstruction() method in one invocation. This includes processing instructions that occur before and after the root element. The target is given as the first argument and the data as the second argument.

public void processingInstruction (String target, String data)
 throws SAXException

Caution

Technically the XML declaration is not a processing instruction, even though it looks like one; and it is not passed to the processingInstruction() method. SAX 2.1 will add some features and properties for retrieving the values of the version, standalone, and encoding attributes from the XML declaration. However, until then SAX parsers do not tell client programs what was in the XML declaration, or even whether the document contained an XML declaration in the first place.


Although many processing instructions such as xml-stylesheet use a pseudo-attribute format, this depends on the semantics defined for the particular target. The processingInstruction() method does not distinguish between processing instructions like xml-stylesheet that use pseudo-attributes and processing instructions like php that don't. The data of both kinds is passed to the processingInstruction() method as a single String. If you want to split that string into attributes and values, you have to write the code to do that yourself. SAX doesn't do it for you.

Tip

The JDOM ProcessingInstruction class is able to break up processing instructions that use pseudo-attributes into name-value pairs. Even if you're parsing primarily with SAX, you can still use the JDOM ProcessingInstruction class to extract these pairs for processing instructions that are known to contain pseudo-attributes. For example, this SAX processingInstruction() method uses JDOM to find the value of the href attribute of an xml-stylesheet processing instruction:

public void processingInstruction(String target, 
 String data) {

  if (target.equals("xml-stylesheet")) {
    ProcessingInstruction xml-stylesheet
     = new ProcessingInstruction(target, data);
    String href = xml-stylesheet.getValue("href");
  }

}

Of course for this to work, you'll need to have the JDOM class library installed in your class path, and to have imported at least org.jdom.ProcessingInstruction.


Example 6.11 demonstrates with a straightforward program to list the targets and data of all the processing instructions found in a document. Of course the real purpose of processing instructions is to pass their data to a separate program somehow identified by the target.

Example 6.11 A ContentHandler That Prints Processing Instruction Targets and Data on System.out
import org.xml.sax.*;
import org.xml.sax.helpers.XMLReaderFactory;

public class ProcessingInstructionLister extends DefaultHandler {


  public void processingInstruction(String target, String data) {

    System.out.println("Processing Instruction:");
    System.out.println("  target: " + target);
    System.out.println("  data:   " + data);
    System.out.println();

  }


  public static void main(String[] args) {

    try {
      XMLReader parser = XMLReaderFactory.createXMLReader(
        "org.apache.xerces.parsers.SAXParser"
      );
      ContentHandler handler = new ProcessingInstructionLister();
      parser.setContentHandler(handler);
      for (int i = 0; i < args.length; i++) {
        parser.parse(args[i]);
      }
    }
    catch (Exception e) {
      System.err.println(e);
    }
  }

}

Following is the output from running this program over styled_order.xml from Chapter 1. This document contains a single xml-stylesheet processing instruction. Note especially that the XML declaration is not considered to be a processing instruction and is not reported.

C:\XMLJAVA>java ProcessingInstructionLister styled_order.xml 
Processing Instruction:
  target: xml-stylesheet
  data:   type="text/css" href="order.css"
    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
    Chapter 6. SAX
    What Is SAX?
    Parsing
    Callback Interfaces
    Receiving Documents
    Receiving Elements
    Handling Attributes
    Receiving Characters
    Receiving Processing Instructions
    Receiving Namespace Mappings
    'Ignorable White Space'
    Receiving Skipped Entities
    Receiving Locators
    What the ContentHandler Doesn't Tell You
    Summary
    Chapter 7. The XMLReader Interface
    Chapter 8. SAX Filters
    Part III: 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