JAVA读取XML文件并解析获取元素、属性值、子元素信息
JAVA读取XML文件并解析获取元素、属性值、子元素信息
关键字
XML读取 InputStream DocumentBuilderFactory Element Node
前言
正文(直接上源码)
XML文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"> <bean name="HelloWorld" class="com.huishe.HelloWord">
<property name="textone" value="Hello World!"></property>
<property name="texttwo" value="Hello SUN!"></property>
</bean> </beans>
XMLParse解析源码
package com.huishe.testOfSpring; import java.io.FileInputStream;
import java.io.InputStream; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList; public class XMLParse { public static void main(String[] args) throws Exception {
//1-获取XML-IO流
InputStream xmlInputStream = getXmlInputStream("xml/tinyioc.xml");
//2-解析XML-IO流 ,获取Document 对象,以及Document对象 的根节点
Element rootElement = getRootElementFromIs(xmlInputStream);
//3~5-从根元素解析得到元素
parseElementFromRoot(rootElement); //控制台输出:
//name == HelloWorld
//className == com.huishe.HelloWord
//propertyEle: name == textone
//propertyEle: value == Hello World!
//propertyEle: name == texttwo
//propertyEle: value == Hello SUN!
} //1-获取XML-IO流
private static InputStream getXmlInputStream(String xmlPath){
InputStream inputStream = null;
try {
//1-把要解析的 XML 文档转化为输入流,以便 DOM 解析器解析它
inputStream= new FileInputStream(xmlPath);
} catch (Exception e) {
e.printStackTrace();
}
return inputStream;
}
//2-解析XML-IO流 ,获取Document 对象,以及Document对象 的根节点
private static Element getRootElementFromIs(InputStream inputStream) throws Exception {
if(inputStream == null){
return null;
}
/*
* javax.xml.parsers 包中的DocumentBuilderFactory用于创建DOM模式的解析器对象 ,
* DocumentBuilderFactory是一个抽象工厂类,它不能直接实例化,但该类提供了一个newInstance方法 ,
* 这个方法会根据本地平台默认安装的解析器,自动创建一个工厂的对象并返回。
*/
//2-调用 DocumentBuilderFactory.newInstance() 方法得到创建 DOM 解析器的工厂
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//3-调用工厂对象的 newDocumentBuilder方法得到 DOM 解析器对象。
DocumentBuilder docBuilder = factory.newDocumentBuilder();
//4-调用 DOM 解析器对象的 parse() 方法解析 XML 文档,得到代表整个文档的 Document 对象,进行可以利用DOM特性对整个XML文档进行操作了。
Document doc = docBuilder.parse(inputStream);
//5-得到 XML 文档的根节点
Element root =doc.getDocumentElement();
//6-关闭流
if(inputStream != null){
inputStream.close();
}
return root;
} //3-从根元素解析得到元素
private static void parseElementFromRoot(Element root) {
NodeList nl = root.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element) {
Element ele = (Element) node;
//4-从元素解析得到属性值
getDataFromElement(ele);
//5-从元素解析特定子元素并解析(以property为例)
getCertainElementFromParentElement(ele);
}
}
} //4-从元素解析得到属性值
private static void getDataFromElement(Element ele) {
String name = ele.getAttribute("name");//根据属性名称读取属性值
System.out.println("name == " + name);
String className = ele.getAttribute("class");
System.out.println("className == " + className);
}
//5-从元素解析特定子元素并解析(以property为例)
private static void getCertainElementFromParentElement(Element ele) {
NodeList propertyEleList = ele.getElementsByTagName("property");//根据标签名称获取标签元素列表
for (int i = 0; i < propertyEleList.getLength(); i++) {
Node node = propertyEleList.item(i);
if (node instanceof Element) {
Element propertyEle = (Element) node;
String name = propertyEle.getAttribute("name");
System.out.println("propertyEle: name == " + name);
String value = propertyEle.getAttribute("value");
System.out.println("propertyEle: value == " + value);
}
} } }
总结
读取XML配置涉及到IO、DocumentBuilderFactory、Node等概念,这里只使用,不具体分析
参考文献
1- https://blog.csdn.net/hua1017177499/article/details/78985166
JAVA读取XML文件并解析获取元素、属性值、子元素信息的更多相关文章
- java 读取XML文件作为配置文件
首先,贴上自己的实例: XML文件:NewFile.xml(该文件与src目录同级) <?xml version="1.0" encoding="UTF-8&quo ...
- java读取xml文件报“org.xml.sax.SAXParseException: Premature end of file” .
背景:java读取xml文件,xml文件内容只有“<?xml version="1.0" encoding="UTF-8"?>”一行 java读取该 ...
- java读取XML文件的四种方式
java读取XML文件的四种方式 Xml代码 <?xml version="1.0" encoding="GB2312"?> <RESULT& ...
- java读取 xml文件
java读取xml文件的四种方法 转自https://www.cnblogs.com/lingyao/p/5708929.html Xml代码 1 <?xml version="1. ...
- 通过Java读取xml文件内容
读取XML中的内容就需要对XML进行解析,目前对XML进行解析的方法分为四种: 下面解析的方法是DOM4J,需要下载jar包dom4j:https://dom4j.github.io/ package ...
- 用java操作XML文件(DOM解析方式)
XML 可扩展标记语言(Extensible Markup Language),是独立于软件和硬件的传输工具. XML的作用: (1)用作配置文件 (2)简化数据共享 (3)简化数据传输 XML DO ...
- JAVA读取XML文件数据
XML文档内容如下: <?xml version="1.0" encoding="UTF-8"?> <root> <field t ...
- java读取XML文件,及封装XML字符串
package com.yyl.text; import java.io.FileInputStream; import java.util.ArrayList; import org.junit.T ...
- 【Java】XML文件的解析
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...
随机推荐
- too many connections 解决方法
最近写javaee项目的时候,mysql报了too many connections的错误,百度的内容有一些有问题,所以我重新写一下我的解决方法. mysql -u root -p 回车输入密码进入m ...
- 使用ubuntu远程连接windows, Connect to a Windows PC from Ubuntu via Remote Desktop Connection
from: https://www.digitalcitizen.life/connecting-windows-remote-desktop-ubuntu NOTE: This tutorial w ...
- jquery 设计的思路-----初级
jquery.js 很经典,其中有一些设计思路简直非常经典 1.安全的创建一个构造函数并进行调用: <script> // 这是一种安全的构造函数的创建方法,在调用构造函数G的时候,不论使 ...
- 如何让cxgrid自动调整列宽
1.选中cxgridview,在属性中找OptionsView--->ColumAutoWidth,把这个属性设为True; 2.在FDMemtable的open之后加上如下代码即可 [delp ...
- python语言中的运算符
基本运算符 1.比较运算符 # >,< ,>= ,<=, ==(比较值) ,!=(不等号) 2.逻辑运算符 and or not(取反) 3.算术运算 / ...
- cin.tie与sync_with_stdio加速输入输出
在LeetCode上练习习题的时候每次AC之后都会去看别人的代码,几乎每次都能遇到cin.tie与sync_with_stdio.类似这样: static auto x = [](){ std::io ...
- 关于nginx多层uptstream转发获取客户端真实IP的问题
因为公司有个需求需要获取客户端的真实IP,前端是haproxy,后面是nginx,本来这个需求不难完成,但是难就难在是https请求也就是ssl 由于个人水平有限,在网上爬了很多资料,刚开始的ha是通 ...
- React之使用Context跨组件树传递数据
--------------------------------- 讲解一 原文:https://blog.csdn.net/xuxiaoping1989/article/details/78480 ...
- [ SHELL编程 ] shell编程中数值计算方法实例
SHELL编程中经常会涉及到数值计算,有时候对于这些计算命令使用场景容易忘记或者混淆,这里针对常用的命令做个总结.主要包括let.bc.expr.(())等. 1.let 使用格式:let 表达式,表 ...
- 八皇后问题-dfs
一.题意解析 国际象棋中的皇后,可以横向.纵向.斜向移动.如何在一个8X8的棋盘上放置8个皇后,使得任意两个皇后都不在同一条横线.竖线.斜线方向上?八皇后问题是一个古老的问题,于1848年由一位国际象 ...