用sax解析xml

  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. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值