解析XML文件的方法

1 SAX解析 1

1-1 外部配置 1

1-2 具体的实现 1

1-3 效果图 3

2 DOM解析 (Document object Model)w3c 3

2-1 用到的函数解析 3

Document: 3

Element: 7

2-2 实例: 8

2-2-1 parse1 8

2-2-1 parse2 9

2-2-3 getNodes获取节点 10

2-2-4 测试 10

3 JDOM 解析 导入jdom包 11

4 DOM4J 解析导入的 dom4j 架包 11

4-1parse1 11

4-2parse2 12

4-3 测试 13

 

1 SAX解析

解析出的和原来的配置文件几乎一样

1-1 外部配置

 (1)需要继承DefaultHandler

 (2)重写五个方法

Characters: 标签中的文本值

endDocument: 结束文档

endElement: 结束标签

 startDocument: 文档开始解析

startElement: 开始标签 属性包含在开始标签里面

1-2 具体的实现

public class ParseBySAX extends DefaultHandler{

//标签中的文本值

@Override

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

throws SAXException {

System.out.print(new String(ch,start,length));

}

//结束文档

@Override

public void endDocument() throws SAXException {

System.out.print("文档结束解析");

}

//结束标签

@Override

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

throws SAXException {

System.out.print("<"+qName+">");

}

@Override

public void startDocument() throws SAXException {

System.out.println("文档开始解析");

}

//开始标签 属性包含在开始标签里面

@Override

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

Attributes attributes) throws SAXException {

String s = "<"+qName;

for(int i=0;i<attributes.getLength();i++){

String attrName = attributes.getQName(i);

String attrValue = attributes.getValue(i);

s+=" "+attrName+"=\""+attrValue+"\"";

}

s+=">";

System.out.print(s);

}

public static void main(String[] args) throws Exception, SAXException {

//创建解析工厂

SAXParserFactory factory = SAXParserFactory.newInstance();

//获取解析器

SAXParser parse = factory.newSAXParser();

//开始解析

parse.parse(new File("src/book.xml"), new ParseBySAX());

}

}

1-3 效果图

文档开始解析

<books>

<book no="01011">

<name>还珠格格<name>

<author>琼瑶<author>

<publish id="1">湖南电视台<publish>

<price>3<price>

<book>

<book no="01022">

<name>神雕侠侣<name>

<author>包中<author>

<publish>湖南电视台<publish>

<price>22<price>

<book>

<book no="01033">

<name>一代枭雄<name>

<author>孙红雷<author>

<publish>山地<publish>

<price>123<price>

<book>

<books>文档结束解析

2 DOM解析 (Document object Model)w3c

2-1 用到的函数解析

Document:

getDocumentElement():该属性允许直接访问文档的文档元素的子节点

getElementsByTagName(name):按文档顺序返回包含在文档中且具有给定标记名称的所有 Element 的 NodeList。

Element:

 

2-2 实例:

2-2-1 parse1

public void parse1(Document document){

//获取文档的根元素

Element root =  document.getDocumentElement();

System.out.println("获取文件的根元素"+root.getNodeName());

//获取根元素的所有子元素

NodeList nodes =  root.getChildNodes();

System.out.println("获取根元素的所有子元素的个数"+nodes.getLength());

for(int i=0;i<nodes.getLength();i++){

Node node = nodes.item(i);

//System.out.println(node.getNodeType());

//判断当前节点是否是一个完整的标签元素

if(node.getNodeType()==Node.ELEMENT_NODE){

//获取当前节点所有属性的集合

NamedNodeMap map =  node.getAttributes();

Node n = map.getNamedItem("no");

System.out.println("n0: "+n.getNodeValue());

//获取book节点下的所有子节点

NodeList list =  node.getChildNodes();

for(int j=0;j<list.getLength();j++){

Node n2 = list.item(j);

if(n2.getNodeType()==Node.ELEMENT_NODE){

//获得节点的内容

String text = n2.getTextContent();

System.out.println(n2.getNodeName()+": "+text);

}

}

System.out.println();

}

}

}

2-2-1 parse2

public void parse2(Document document){

NodeList list = document.getElementsByTagName("book");

for(int i=0;i<list.getLength();i++){

Node node = list.item(i);

String no = node.getAttributes().getNamedItem("no").getNodeValue();

System.out.println("no: "+no);

NodeList nodes = node.getChildNodes();

for(int j=0;j<nodes.getLength();j++){

if(nodes.item(j).getNodeType()==Node.ELEMENT_NODE){

System.out.println(nodes.item(j).getNodeName()+" : "+nodes.item(j).getTextContent());

}

}

System.out.println();

}

}

2-2-3 getNodes获取节点

 public void getNodes(Node n){

if(n.hasChildNodes()){

NodeList n1 = n.getChildNodes();

for(int i=0;i<n1.getLength();i++){

Node node = n1.item(i);

if(node.getNodeType()==Node.ELEMENT_NODE){

System.out.println(node.getNodeName()+" : "+node.getTextContent());

}

getNodes(node);

}

}

}

2-2-4 测试

public static void main(String[] args) {

try {

//获得解析器工厂 用来生产解析器

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

//获得解析器

DocumentBuilder builder = factory.newDocumentBuilder();

//解析指定源获得解析文档

Document document = builder.parse(new File("src/book.xml"));

System.out.println(document);

//new ParseByDom().parse1(document);

//new ParseByDom().parse2(document);

Node n =  document.getDocumentElement();

new ParseByDom().getNodes(n);

catch (Exception e) {

e.printStackTrace();

}

}

3 JDOM 解析 导入jdom包

//获得解析器

SAXBuilder bulider = new SAXBuilder();

//解析文件获取文档对象

Document document = bulider.build(new File("src/book.xml"));

//获取文档根元素

Element root = document.getRootElement();

//获取指定所有的book节点

List list = root.getChildren("book");

for(Object obj:list){

Element ele = (Element)obj;

//获取当前节点中指定属性的值

String no = ele.getAttributeValue("no");

String name = ele.getChild("name").getText();

String author = ele.getChild("author").getText();

String publish = ele.getChild("name").getText();

String price = ele.getChild("price").getText();

System.out.println("no : "+no);

System.out.println("name : "+name);

System.out.println("author : "+author);

System.out.println("publish : "+publish);

System.out.println("price : "+price);

System.out.println();

}

4 DOM4J 解析导入的 dom4j 架包

4-1parse1

public void parse1(Document document){

Element root = document.getRootElement();

//获取当前根元素下所有的book子元素

Iterator<Element> it = root.elementIterator("book");

while(it.hasNext()){

Element e = it.next();

//获取属性

String no = e.attributeValue("no");

//获取子节点

String name = e.element("name").getTextTrim();

String author = e.element("author").getTextTrim();

String publish = e.element("publish").getTextTrim();

String price = e.element("price").getTextTrim();

System.out.println("no : "+no);

System.out.println("name : "+name);

System.out.println("author : "+author);

System.out.println("publish : "+publish);

System.out.println("price : "+price);

System.out.println();

}

}

4-2parse2

public void parse2(Document document){

//找寻大节点 里面有小的节点

List<Node> list = document.selectNodes("books/book");

for(Node n:list){

String no = n.valueOf("@no");

//获得单个的

String name = n.selectSingleNode("name").getName().trim();

String author= n.selectSingleNode("author").getName().trim();

String publish = n.selectSingleNode("publish").getName().trim();

String price = n.selectSingleNode("price").getName().trim();

System.out.println("no : "+no);

System.out.println("name : "+name);

System.out.println("author : "+author);

System.out.println("publish : "+publish);

System.out.println("price : "+price);

System.out.println();

}

}

4-3 测试

public static void main(String[] args) throws Exception {

//获取解析器

SAXReader reader = new SAXReader();

//开始解析

Document document = reader.read(new File("src/book.xml"));

//new ParseByDOM4J().parse1(document);

new ParseByDOM4J().parse1(document);

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值