程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> [JAVA100例]069、使用SAX解析XML

[JAVA100例]069、使用SAX解析XML

編輯:關於JAVA

ReadXmlDemo.java
import java.io.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
public class ReadXmlDemo extends DefaultHandler {
public static void main(String[] arguments) {
    if (arguments.length > 0){
   ReadXmlDemo read = new ReadXmlDemo(arguments[0]);
} else {
   System.out.println("Usage: java ReadXmlDemo filename");
}
  }
ReadXmlDemo(String xmlFile) {
    File input = new File(xmlFile);
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
try {
   SAXParser sax = factory.newSAXParser();
   sax.parse(input, new XmlHandler() );
} catch (ParserConfigurationException pce) {
   System.out.println("Could not create that parser.");
   System.out.println(pce.getMessage());
} catch (SAXException saxe) {
   System.out.println("Problem with the SAX parser.");
   System.out.println(saxe.getMessage());
} catch (IOException ioe) {
   System.out.println("Error reading file.");
   System.out.println(ioe.getMessage());
}
  }
}
class XmlHandler extends DefaultHandler {
  static int READING_NAME = 1;
  static int READING_SEX = 2;
  static int READING_BIRTH = 3;
  static int READING_NOTHING = 0;
  int currentActivity = READING_NOTHING;
  ReadXml xml = new ReadXml();
XmlHandler() {
    super();
  }
public void startElement(String uri, String localName, String qName, Attributes attributes) {
 
    if (qName.equals("title"))
      currentActivity = READING_NAME;
else if (qName.equals("file"))
   currentActivity = READING_SEX;
//else if (qName.equals("Birth"))
   //currentActivity = READING_BIRTH;
  }
public void characters(char[] ch, int start, int length) {
    String value = new String(ch, start, length);
if (currentActivity == READING_NAME)
   xml.name = value;
if (currentActivity == READING_SEX)
   xml.sex = value;
//if (currentActivity == READING_BIRTH)
   //xml.birth = value;
  }
public void endElement(String uri, String localName, String qName) {
    if (qName.equals("page")) {
   System.out.println("\nName: "+xml.name);
   System.out.println("Sex: "+xml.sex);
   //System.out.println("Birth: "+xml.birth);
   xml = new ReadXml();
}
  }
}
class ReadXml {
  String name;
  String sex;
  String birth;
}
SaxParsePage.java
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.io.*;
/**
* <p>Title: 使用SAX解析XML</p>
* <p>Description: 使用SAX接口解析book.xml文件</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Filename: SaxParsePage.java</p>
* @version 1.0
*/
public class SaxParsePage extends DefaultHandler {
/**
*<br>方法說明:主方法,聲明解析工廠和加載XML文件
*<br>輸入參數:
*<br>返回類型:
*/
 public static void main(String[] argv) {
  try {
   //建立SAX解析工廠
   SAXParserFactory spfactory = SAXParserFactory.newInstance();
   //生成SAX解析對象
   SAXParser parser = spfactory.newSAXParser();
   //指定XML文件,進行XML解析
   parser.parse(new File("book.xml"), new SaxParsePage());
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
/**
*<br>方法說明:文件打開時調用
*<br>輸入參數:
*<br>返回類型:
*/
 public void startDocument() {
  System.out.println("***開始解析***");
 }
/**
*<br>方法說明:當遇到開始標記時調用
*<br>輸入參數:
*<br>返回類型:
*/
 public void startElement(String uri,
              String localName,
              String qName,
              Attributes attributes) throws SAXException {
System.out.println("節點開始:" + qName);
  for(int i=0;i<attributes.getLength();i++){
   System.out.println("節點屬性名稱:" + attributes.getQName(i));
   System.out.println("節點屬性值:"+attributes.getValue(i));
  }
 }
/**
*<br>方法說明:當分析器遇到無法識別為標記或者指令類型字符時調用
*<br>輸入參數:
*<br>返回類型:
*/
 public void characters(char[] ch,
             int offset,
             int length) throws SAXException{
System.out.println("節點數據:" + new String(ch, offset, length));
 }
/**
*<br>方法說明:當遇到節點結束時調用
*<br>輸入參數:
*<br>返回類型:
*/
 public void endElement(String uri,
             String localName,
             String qName) {
System.out.println("節點結束:" + qName);
 }
/**
*<br>方法說明:當到文檔的末尾調用
*<br>輸入參數:
*<br>返回類型:
*/
 public void endDocument() {
  System.out.println("****文件解析完畢****");
 }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved