当前位置: 首页 > news >正文

Java中解析XML文件

1 在Java中解析XML文件共有四种方式

  • A、DOM方式解析XML数据

树结构,有助于更好地理解、掌握,代码易于编写,在解析过程中树结构是保存在内存中,方便修改

  • B、SAX方式解析

采用事件驱动模式,对内存消耗比较小,适用于仅处理xml中的数据时使用

  • C、JDOM方式解析

大量采用了 Collections 类

  • D、DOM4J方式解析

JDOM的一种智能分支,合并了许多超出基本XML文档表示的功能;
性能优越,灵活性好,功能强大,极端易用。

2 要处理的XML文件

<?xml version="1.0" encoding="UTF-8"?>
<books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><book id="1001"><name>JAVA 高级编程</name><author>张三</author><price>85.72</price></book><book id="1002"><name>C++和C#</name><author>李失失</author><price>125.73</price></book>
</books>

3 DOM方式解析XML数据的步骤

a. 创建一个DocumentBuilderFactory对象
b. 创建一个DocumentBuilder对象
c. 通过DocumentBuilder的parse()方法,得到Document对象
d. 通过getElementsByTagName()方法,获取节点的列表
e. 使用for循环遍历节点
f. 得到所有节点的属性和属性值
g. 得到所有节点的节点名和节点值

import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;public class TestDom4Xml {public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {//创建一个DocumentBuilderFactory对象DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();//创建一个DocumentBuilder对象DocumentBuilder db = dbf.newDocumentBuilder();//通过DocumentBuilder的parse()方法,得到Document对象Document doc = db.parse("book.xml");//通过getElementsByTagName()方法,获取节点的列表NodeList nodelist = doc.getElementsByTagName("book");System.out.println(nodelist.getLength());//使用for循环遍历节点for(int i=0;i<nodelist.getLength();i++){Node node = nodelist.item(i);//得到所有节点属性和属性值的对象NamedNodeMap nnm = node.getAttributes();for(int j=0;j<nnm.getLength();j++){Node sub_node = nnm.item(j);//得到所有节点的属性和属性值System.out.println(sub_node.getNodeName() + " : " + sub_node.getNodeValue());}//得到所有节点的子节点NodeList childlist = node.getChildNodes();for(int j=0;j<childlist.getLength();j++){Node sub_node = childlist.item(j);//获取节点类型short type = sub_node.getNodeType();//判断节点类型是否不为#text,因会将前一标签的末尾>与下一标签的开头<之间的字符,标记为#text//因此这里需要进行判断if(type == Node.ELEMENT_NODE){//得到所有节点的节点名和节点值System.out.println(sub_node.getNodeName() + " : " + sub_node.getTextContent());}}}}
}

输出结果如下

2

id : 1001

name : JAVA 高级编程

author : 张三

price : 85.72

id : 1002

name : C++和C#

author : 李失失

price : 125.73

4 SAX方式解析XML文件

4.1 创建DeaultHandler子类,用来解析XML文档

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;public class BookDefaultHandler extends DefaultHandler {/*** 解析xml文档时调用*/public void startDocument() throws SAXException {System.out.println("开始解析XML文档");super.startDocument();}/*** 解析xml文档结束时调用*/public void endDocument() throws SAXException {super.endDocument();System.out.println("完成解析XML文档");}/*** 解析XML文档节点开始时使用*/public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {super.startElement(uri, localName, qName, attributes);//判断如果是 book节点,获取节点属性和属性值if(qName.equals("book")){//获取当前属性的数量int len = attributes.getLength();//循环获取每个属性for(int i=0;i<len;i++){//属性名称String name = attributes.getQName(i);//属性值String value = attributes.getValue(i);System.out.println("属性名称: " + name + "\t属性值: " + value);}}else if(!"books".equals(qName) && !"book".equals(qName)){System.out.print("节点的名称:" + qName + "\t");}}/*** 解析XML文档节点结束时使用*/public void endElement(String uri, String localName, String qName) throws SAXException {super.endElement(uri, localName, qName);}/*** 解析节点文本内容*/public void characters(char[] ch, int start, int length) throws SAXException {super.characters(ch, start, length);//将节点文本转为字符串String value = new String(ch, start, length);//排除空数据,并输出节点文本if(!"".equals(value.trim())){System.out.println(value);}}
}

4.2 SAX方式解析XML数据的步骤

a. 创建SAXParserFactory对象
b. 创建SAXParser对象(作为解析器)
c. 创建DefaultHandler子类对象
d. 调用parse方法

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 TestSax4Xml {public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {//a. 创建SAXParserFactory对象SAXParserFactory spf = SAXParserFactory.newInstance();//b. 创建SAXParser对象(作为解析器)SAXParser sp = spf.newSAXParser();//c. 创建DefaultHandler子类对象BookDefaultHandler bdh = new BookDefaultHandler();//d. 调用SAXParser对象的parse方法sp.parse("book.xml", bdh);}
}

4.3 输出结果

开始解析XML文档

属性名称: id 属性值: 1001

节点的名称:name JAVA 高级编程

节点的名称:author 张三

节点的名称:price 85.72

属性名称: id 属性值: 1002

节点的名称:name C++和C#

节点的名称:author 李失失

节点的名称:price 125.73

完成解析XML文档

5 JDOM方式解析XML数据

5.1 步骤

a. 创建SAXBuilder对象
b. 调用build方法,通过IO流得到Document对象
c. 获取根节点
d. 获取根节点下直接子节点的集合
e. 遍历集合

import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;public class TestJdom4Xml {public static void main(String[] args) throws JDOMException, IOException {//a. 创建SAXBuilder对象SAXBuilder sb = new SAXBuilder();//b. 调用build方法,通过OI流得到Document对象Document doc = sb.build(new FileInputStream("src/book.xml"));//c. 获取根节点Element root = doc.getRootElement();//d. 获取根节点下直接子节点的集合List<Element> books = root.getChildren();//e. 遍历集合,获取每一个子节点for(int i=0;i<books.size();i++){//获取集合中的元素Element book = books.get(i);//获取当前节点下的属性集合List<Attribute> atts = book.getAttributes();for(int j=0;j<atts.size();j++){//输出属性名称:属性值System.out.println(atts.get(j).getName() + "\t" + atts.get(j).getValue());}//获取当前节点下子节点List<Element> subEles = book.getChildren();//遍历子节点,获取名称和文本值for(Element e : subEles){System.out.println(e.getName() + "\t" + e.getValue());}}}
}

5.2 输出结果

id 1001

name JAVA 高级编程

author 张三

price 85.72

id 1002

name C++和C#

author 李失失

price 125.73

6 DOM4J解析XML

6.1 DOM4J解析XML步骤

a. 创建SAXReader对象
b. 调用read方法
c. 获取根节点
d. 通过迭代器遍历直接节点

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Iterator;import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;public class TestDom4jXml {public static void main(String[] args) throws FileNotFoundException, DocumentException {//a. 创建SAXReader对象SAXReader sr = new SAXReader();//b. 调用read方法Document doc = sr.read(new FileInputStream("src/book.xml"));//c. 获取根节点Element root = doc.getRootElement();//d. 通过迭代器遍历直接节点for(Iterator<Element> iter=root.elementIterator();iter.hasNext();){Element book = iter.next();//获取节点下所有属性Iterator<Attribute> arrts = book.attributeIterator();//遍历属性信息while(arrts.hasNext()){Attribute at = arrts.next();String name = at.getName();String value = at.getValue();System.out.println("节点属性:" + name + "\t" + value);}//遍历节点下子节点Iterator<Element> subele = book.elementIterator();//获取子节点下所有节点名称和文本值while(subele.hasNext()){Element node = subele.next();System.out.println(node.getName() + "\t" + node.getText());}}}
}

6.2 输出结果

节点属性:id 1001

name JAVA 高级编程

author 张三

price 85.72

节点属性:id 1002

name C++和C#

author 李失失

price 125.73

http://www.lryc.cn/news/32087.html

相关文章:

  • 二点回调测买 源码
  • 钉钉端H5开发调试怎么搞
  • Mysql Server原理简介
  • 23种设计模式-外观模式
  • 使用 Vulkan VkImage 作为 CUDA cuArray
  • 电商API接口-电商OMS不可或缺的一块 调用代码展示
  • Solaris ZFS文件系统rpool扩容
  • 模式识别 —— 第二章 参数估计
  • 判断4位回文数-课后程序(Python程序开发案例教程-黑马程序员编著-第3章-课后作业)
  • 【NLP】Word2Vec 介绍
  • 3月6日,30秒知全网,精选7个热点
  • Python笔记 -- 字典
  • 【独家】华为OD机试 - 滑动窗口(C 语言解题)
  • MySQL调优 - SQL查询深度分页问题
  • 0306spring--复习
  • 动手实现一遍Transformer
  • 【Flutter入门到进阶】Flutter基础篇---弹窗Dialog
  • 【操作系统】进程和线程的区别
  • Linux开发环境配置--正点原子阿尔法开发板
  • Android ThreadPoolExecutor的基本使用
  • 基于区域生长和形态学处理的图像融合方法——Matlab图像处理
  • 三个案例场景带你掌握Cisco交换机VLAN互通
  • 小白入门之持久连接与非持久连接的差别
  • TypeScript篇.01-简介,类,接口,基础类型
  • 分享几种WordPress怎么实现相关文章功能
  • PANGO的IOB的电平能力那些事
  • scrpy学习-02
  • MySQL运维篇之Mycat分片规则
  • vue router elementui template CDN模式实现多个页面跳转
  • ElasticSearch - ElasticSearch基本概念及集群内部原理