文档解析 xml和sax

xml

XML 可扩展标记语言 ,是一种简单的数据存储语言,使用一系列简单的标签描述数据,使人们或者程序可以理解 ; 可读性强 灵活性高.

对44.txt文件中的内容进行提取在这里提取Weather中所包含的内容

import java.io.File;
import java.io.IOException;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.w3c.dom.NamednodeMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Node;

public class FileXml {
    public static void main(String[] args) {
        File file =new File ("d:\\44.txt");
        DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder db=dbf.newDocumentBuilder();
            Document doc=db.parse(file);
            NodeList weathers=doc.getElementsByTagName("Weather");
            for (int i = 0; i < weathers.getLength(); i++) {
                Node weather =weathers.item(i);
/* NamednodeMap map=weather.getAttributes(); System.out.println(map.getNamedItem(" ").getNodeValue());*/
                for(Node node =weather.getFirstChild();node!=null;node=node.getNextSibling())
                    if(node.getNodeType()==node.ELEMENT_NODE){
                        System.out.println(node.getNodeName());
                        if(node.getFirstChild()!=null){
                            System.out.println(node.getFirstChild().getNodeValue());
                        }
                    }
            }
        } catch (ParserConfigurationException e) {
            // Todo Auto-generated catch block
            e.printstacktrace();
        } catch (SAXException e) {
            // Todo Auto-generated catch block
            e.printstacktrace();
        } catch (IOException e) {
            // Todo Auto-generated catch block
            e.printstacktrace();
        }
    }
}

运行结果:

SAX

自定义解析器来对文件进行提取

自己定义的一个文件解析器,重写了认的方法
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MySAXHandle extends DefaultHandler{

    @Override
    public void characters(char[] ch,int start,int length) throws SAXException {
        // Todo Auto-generated method stub
        super.characters(ch,start,length);
        System.out.println("标签内容 is "+new String(ch,length));
    }

    @Override
    public void endDocument() throws SAXException {
        // Todo Auto-generated method stub
        super.endDocument();
        System.out.println("文档结束");  
    }

    @Override
    public void endElement(String uri,String localName,String qName) throws SAXException {
        // Todo Auto-generated method stub
        super.endElement(uri,localName,qName);
        System.out.println("标签结束");
    }

    @Override
    public void startDocument() throws SAXException {
        // Todo Auto-generated method stub
        super.startDocument();
        System.out.println("文档开始 ");
    }

    @Override
    public void startElement(String uri,String qName,Attributes attributes) throws SAXException {
        // Todo Auto-generated method stub
        super.startElement(uri,qName,attributes);
        System.out.println("标签开始 name is "+qName); 
    }

}

文件进行解析提取

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

public class FileSax {
    public static void main(String[] args) {
        File file=new File("d:\\44.txt"); 
        SAXParserFactory factory=SAXParserFactory.newInstance();
        try {
            SAXParser parser =factory.newSAXParser();
            MySAXHandle handle=new MySAXHandle();
            parser.parse(file,handle);
        } catch (ParserConfigurationException e) {
            // Todo Auto-generated catch block
            e.printstacktrace();
        } catch (SAXException e) {
            // Todo Auto-generated catch block
            e.printstacktrace();
        } catch (IOException e) {
            // Todo Auto-generated catch block
            e.printstacktrace();
        }
    }
}

运行结果为:

  1. xml中 dom 树形结构一次性读到内存中,特点是简单快,但是只适用于小文件
  2. sax 事件驱动 如果文件很大,不会全部读到内存中

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念