zoukankan      html  css  js  c++  java
  • SAX解析XML

    一、使用SAX解析xml文件

    新建一个类,继承之DefaultHandler:

    示例代码如下:

    package org.demo.saxxml;

    import org.xml.sax.Attributes;

    import org.xml.sax.SAXException;

    import org.xml.sax.helpers.DefaultHandler;

    public class MyContentsHandler extends DefaultHandler {

           String Name,Sex;

           String tagName;

           String xmlContent="";

           @Override

    //解析每个标签的时候调用的方法

           public void characters(char[] ch, int start, int length)

                         throws SAXException {

                  if(tagName.equals("Name")){

                         Name=new String(ch,start,length);

                         System.out.println(Name);

                  }else if(tagName.equals("Sex")){

                         Sex=new String(ch,start,length);

                  }

                  super.characters(ch, start, length);

           }

    //解析xml文档结束时

           @Override

           public void endDocument() throws SAXException {

                  // TODO Auto-generated method stub

                  System.out.println("End...");

                 

                  super.endDocument();

           }

    //解析xml标签结束时

           @Override

           public void endElement(String uri, String localName, String qName)

                         throws SAXException {

                  // TODO Auto-generated method stub

                  if(localName.equals("Name")){

                         xmlContent+=Name+"|";

                         System.out.println(xmlContent);

                  }else if(localName.equals("Sex")){

                         xmlContent+=Sex+"<br>";

                  }

                  super.endElement(uri, localName, qName);

           }

    //解析xml文档开始时

           @Override

           public void startDocument() throws SAXException {

                  // TODO Auto-generated method stub

                  super.startDocument();

           }

    //解析xml标签开始时

           @Override

           public void startElement(String uri, String localName, String qName,

                         Attributes attributes) throws SAXException {

                  // TODO Auto-generated method stub

                  tagName=localName;

                  super.startElement(uri, localName, qName, attributes);

           }

           public String getContent(){

                  return xmlContent;

           }

          

    }

  • 相关阅读:
    Oracle EXPDP/IMPDP示例
    Oracle修改日志归档模式、归档路径以及空间大小的相关测试
    CentOS6.8 安装 Oracle11.2.0.4
    安装ORACLE时 各Linux版本下载地址
    Oracle数据库导入报ORA-39083处理
    oracle跨平台数据迁移 expdp/impdp 字符集问题 导致ORA-02374 ORA-12899 ORA-02372
    ASM路径问题导致数据库不能正常启动 -- 报:ORA-03113: end-of-file on communication channel
    oralce11g RAC 启动后 CRS-0184: Cannot communicate with the CRS daemon.
    Oracle 11g安装时针对不同操作系统所需的依赖包查询地址
    关于oracle RAC心跳线采用直连 还是交换机连接的建议
  • 原文地址:https://www.cnblogs.com/itfenqing/p/4429524.html
Copyright © 2011-2022 走看看