现在的位置: 首页 > 综合 > 正文

用sax解析xml

2012年07月10日 ⁄ 综合 ⁄ 共 3452字 ⁄ 字号 评论关闭
  1. package net.risesoft.common.util;
  2. import org.xml.sax.*;
  3. import javax.xml.parsers.*;
  4. import org.xml.sax.helpers.DefaultHandler;
  5. import java.io.*;
  6. public class SaxParse {
  7.     public static void main(String argv[]) {
  8.         SaxParse saxParserApp = new SaxParse();
  9.         //saxParserApp.parseDocument();
  10.     }
  11.     public boolean parseDocument(InputStream is) {
  12.         boolean flag=false;
  13.         try {
  14.             SAXParserFactory factory = SAXParserFactory.newInstance();
  15.             SAXParser saxParser = factory.newSAXParser();
  16.             DefaultHandler handler = new CustomSAXHandler();
  17.             saxParser.parse(is, handler);
  18.             CustomSAXHandler h = (CustomSAXHandler) handler;
  19.             flag=h.getResult();
  20.         } catch (SAXException e) {
  21.         } catch (ParserConfigurationException e) {
  22.         } catch (IOException e) {
  23.         }
  24.         return flag;
  25.     }
  26.     private class CustomSAXHandler extends DefaultHandler {
  27.         private boolean flag;
  28.         private boolean result;
  29.         private StringBuffer content=new StringBuffer();
  30.         public boolean getResult(){
  31.             return result;
  32.         }
  33.         private String filename = null;
  34.         private byte[] fileContent = null;
  35.         public CustomSAXHandler() {
  36.         }
  37.         public void startDocument() throws SAXException {
  38.             System.out.println("Event Type: Start Document");
  39.         }
  40.         public void endDocument() throws SAXException {
  41.             result=true;
  42.             System.out.println("Event Type: End Document");
  43.         }
  44.         public void startElement(String uri, String localName, String qName,
  45.                 Attributes attributes) throws SAXException {
  46.             if ("IMGNAME".equalsIgnoreCase(qName)) {
  47.                 flag = true;
  48.             }
  49.         }
  50.         public void endElement(String uri, String localName, String qName)
  51.                 throws SAXException {
  52.             if ("IMGNAME".equalsIgnoreCase(qName))
  53.                 flag = false;
  54.             if ("IMGVALUE".equalsIgnoreCase(qName)) {
  55.                 try {
  56.                     String value=content.toString();
  57.                     fileContent = AdapterUtil.base64Decode(value);
  58.                     if (filename != null && fileContent != null) {
  59.                         File file = new File("e://" + filename);
  60.                         if (!file.exists())
  61.                             file.createNewFile();
  62.                         OutputStream fos = new FileOutputStream(file);
  63.                         fos.write(fileContent);
  64.                         fos.close();
  65.                     }
  66.                 } catch (Exception e) {
  67.                     e.printStackTrace();
  68.                 }
  69.                 filename = null;
  70.                 fileContent = null;
  71.                 content=new StringBuffer();
  72.             }
  73.         }
  74.         public void characters(char[] ch, int start, int length)
  75.                 throws SAXException {
  76.             String str = new String(ch, start, length).trim();
  77.             if (flag)
  78.                 filename = str;
  79.             else 
  80.                 content.append(str);
  81.         }
  82.         public void error(SAXParseException e) throws SAXException {
  83.             System.out.println("Error: " + e.getMessage());
  84.         }
  85.         public void fatalError(SAXParseException e) throws SAXException {
  86.             System.out.println("Fatal Error: " + e.getMessage());
  87.         }
  88.         public void warning(SAXParseException e) throws SAXException {
  89.             System.out.println("Warning: " + e.getMessage());
  90.         }
  91.     }
  92. }

抱歉!评论已关闭.