XML的2种读取方式

DOM

将XML文档换成一个对象模型的集合(DOM树),应用程序通过对模型的操作完成对XML数据的操作

File xmlfile=new File("D:/MyEclipse 10/test/WebRoot/NewFile.xml");
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=null;
try{
    builder=factory.newDocumentBuilder();
}catch(ParserConfigurationException e){
    e.printStackTrace();
}
org.w3c.dom.Document doc=null;
if(builder!=null){
    try{
        doc=builder.parse(xmlfile);
    }catch(SAXException e){
        e.printStackTrace();
    }catch(IOException e){
        e.printStackTrace();
    }
}
if(doc!=null){
    NodeList nl=doc.getElementsByTagName("name");
    System.out.println(nl.item(0).getFirstChild().getNodeValue());
}

SAX

按顺序访问

package tets;

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.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class ReadXMLBySAX {
    public static void main(String[] args) {
        File xmlfile = new File("D:/MyEclipse 10/test/WebRoot/NewFile.xml");
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser paser;
        try {
            paser = factory.newSAXParser();
            paser.parse(xmlfile, new SAX());
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class SAX extends DefaultHandler {

    @Override
    public void startDocument() throws SAXException {
        System.out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    }

    @Override
    public void endDocument() throws SAXException {
        System.out.println("\n 文档读取结束");
    }

    @Override
    public void startElement(String uri, String localName, String name,
            Attributes attributes) throws SAXException {
        System.out.print("<" + name);
        if (attributes != null) {
            for (int i = 0; i < attributes.getLength(); i++) {
                System.out.print(" " + attributes.getQName(i) + "=\""
                        + attributes.getValue(i) + "\"");
            }
        }
        System.out.print(">");
    }

    @Override
    public void endElement(String uri, String localName, String name)
            throws SAXException {
        System.out.print("</" + name + ">");
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        System.out.print(new String(ch, start, length));
    }
}

hibernate和spring框架都是用DOM4J读取xml配置文件的,另外还有一个Java组件 JDOM=DOM的修改文件+SAX的读取快速

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值