WEB开发网
开发学院软件开发Java JAXP 全面介绍,第 1 部分:XML 处理工具包使解析... 阅读

JAXP 全面介绍,第 1 部分:XML 处理工具包使解析和验证变得更容易

 2010-04-06 00:00:00 来源:WEB开发网   
核心提示: 清单 1. 使用 SAXParserFactoryimportjava.io.OutputStreamWriter;importjava.io.Writer;//JAXPimportjavax.xml.parsers.FactoryConfigurationError;importjavax.x

清单 1. 使用 SAXParserFactory

import java.io.OutputStreamWriter; 
import java.io.Writer; 
// JAXP 
import javax.xml.parsers.FactoryConfigurationError; 
import javax.xml.parsers.ParserConfigurationException; 
import javax.xml.parsers.SAXParserFactory; 
import javax.xml.parsers.SAXParser; 
// SAX 
import org.xml.sax.Attributes; 
import org.xml.sax.SAXException; 
import org.xml.sax.helpers.DefaultHandler; 
public class TestSAXParsing { 
  public static void main(String[] args) { 
    try { 
      if (args.length != 1) { 
        System.err.println ("Usage: java TestSAXParsing [filename]"); 
        System.exit (1); 
      } 
      // Get SAX Parser Factory 
      SAXParserFactory factory = SAXParserFactory.newInstance(); 
      // Turn on validation, and turn off namespaces 
      factory.setValidating(true); 
      factory.setNamespaceAware(false); 
      SAXParser parser = factory.newSAXParser(); 
      parser.parse(new File(args[0]), new MyHandler()); 
    } catch (ParserConfigurationException e) { 
      System.out.println("The underlying parser does not support " + 
                " the requested features."); 
    } catch (FactoryConfigurationError e) { 
      System.out.println("Error occurred obtaining SAX Parser Factory."); 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
  } 
} 
class MyHandler extends DefaultHandler { 
  // SAX callback implementations from ContentHandler, ErrorHandler, etc. 
} 

上一页  1 2 3 4 5 6 7 8 9 10  下一页

Tags:JAXP 全面 介绍

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接