最近新需求要解析xml格式的日志文件,解析完之后数据库落地。

经过度娘搜索,写了demo,现记录如下:

 测试XML

<?xml version="1.0" encoding="UTF-8"?>
<stus core-uuid = "4bc7e7f6-14db-11ea-b8e5-9fa46b26ed54" switchname="iZbp1i0fg2swiwfhc4zzqnZ">
<stu id="10086">
<name>张三</name>
<age>18</age>
<address>深圳</address>
</stu>
<stu id="10086">
<name>李四</name>
<name>李五</name>
<name>李六</name>
<age>28</age>
<address>北京</address>
</stu>
</stus>

  

JAVA类

public class Test{

private static  String FilePath = "D:/NewFile.xml";

public static void analysisXml() {
//1. 创建sax读取对象
SAXReader saxReader = new SAXReader();
//2. 指定解析的xml源
Document document = null;
// 通过reader对象的read方法加载books.xml文件,获取docuemnt对象。
// Document document = reader.read(new File("src/res/books.xml"));
try {
// document = saxReader.read(new URL("https://www.w3school.com.cn/example/xmle/note.xml"));
// document = saxReader.read(new ByteArrayInputStream(FilePath.getBytes("UTF-8")));
document = saxReader.read(new File(FilePath));
} catch (Exception e1) {
log.error("解析调用结果异常", e1);
e1.printStackTrace();
}
//3. 得到根元素
Element root = document.getRootElement();
System.out.println("=====父节点=====");
System.out.println("rootgetName--"+root.getName());
System.out.println("rootattributeValue--"+root.attributeValue("core-uuid"));
System.out.println("rootattributeValue--"+root.attributeValue("switchname"));
System.out.println("rootgetPath--"+root.getPath());
System.out.println("rootgetNamespace--"+root.getNamespace());
System.out.println("roottoString--"+root.toString());
System.out.println("=====结束=====");
// 通过element对象的elementIterator方法获取迭代器
Iterator it = root.elementIterator();
// 遍历迭代器,获取根节点中的信息(书籍)
while (it.hasNext()) {
System.out.println("=====开始遍历=====");
Element element = (Element) it.next();
System.out.println("elementName::"+element.getName());
// 获取book的属性名以及 属性值
List<Attribute> attrs = element.attributes();
for (Attribute attr : attrs) {
System.out.println("属性名:" + attr.getName() + "--属性值:"
+ attr.getValue());
}
Iterator itt = element.elementIterator();
while (itt.hasNext()) {
Element bookChild = (Element) itt.next();
System.out.println("节点名:" + bookChild.getName() + "--节点值:" + bookChild.getStringValue());
}
System.out.println("=====结束遍历=====");
}
} public static void main(String[] args) {
analysisXml();
}
}

  

引用包

import java.io.File;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

  

输出结果

=====父节点=====
rootgetName--stus
rootattributeValue--4bc7e7f6-14db-11ea-b8e5-9fa46b26ed54
rootattributeValue--iZbp1i0fg2swiwfhc4zzqnZ
rootgetPath--/stus
rootgetNamespace--org.dom4j.Namespace@babe [Namespace: prefix mapped to URI ""]
roottoString--org.dom4j.tree.DefaultElement@1700915 [Element: <stus attributes: [org.dom4j.tree.DefaultAttribute@21de60b4 [Attribute: name core-uuid value "4bc7e7f6-14db-11ea-b8e5-9fa46b26ed54"], org.dom4j.tree.DefaultAttribute@c267ef4 [Attribute: name switchname value "iZbp1i0fg2swiwfhc4zzqnZ"]]/>]
=====结束=====
=====开始遍历=====
elementName::stu
属性名:id--属性值:10086
节点名:name--节点值:张三
节点名:age--节点值:18
节点名:address--节点值:深圳
=====结束遍历=====
=====开始遍历=====
elementName::stu
属性名:id--属性值:10086
节点名:name--节点值:李四
节点名:name--节点值:李五
节点名:name--节点值:李六
节点名:age--节点值:28
节点名:address--节点值:北京
=====结束遍历=====

  

参考地址:https://blog.csdn.net/myme95/article/details/94454324

参考地址:https://www.cnblogs.com/bingru/p/10438020.html

参考地址:https://www.cnblogs.com/zhuawang/p/3371291.html

参考地址:https://blog.csdn.net/testTop/article/details/93527796

最新文章

  1. CRL通用权限控制系统
  2. jQuery实现页面内锚点平滑跳转
  3. winform 多线程编程
  4. CefSharp 初用遇到的一些问题及解决方法
  5. php下xcache的安装
  6. Map集合概述
  7. 基于Retrotfit2.1+Material Design+ijkplayer开发的一个APP(新闻,gif 动图,视频播放)
  8. Xcode7推出的新优惠:免证书测试
  9. 定时器 corn 表达式
  10. 【阿里云产品公测】ACE、BAE及SAE云引擎对比评测
  11. LA 6187 - Never Wait for Weights 并查集的带权路径压缩
  12. HDOJ2008数值统计
  13. 【git】借助github学习成果
  14. 高可用集群(HA)之Keeplived原理+配置过程
  15. wpf做的3d滑动gallery
  16. 不错的网络协议栈测试工具 — Packetdrill
  17. Postman----request的body中实现数据驱动
  18. 有了GPRS为什么还要LoRa和NB-IoT?【转】
  19. 在Linux下如何使用openssl生成RSA公钥和私钥对
  20. 小程序 iphone X 吸底按钮适配

热门文章

  1. vim 复制
  2. ListView 九宫格布局实现
  3. dataframe中的数据类型及转化
  4. 从DT时代云栖大会聊聊恒生电子
  5. vue 常用插件,保存
  6. STL hash function的模板特化
  7. JAVA总结--设计模式
  8. Java-集合第六篇操作集合的工具类Collections
  9. 第二次java面试(用友山东济南分公司)
  10. 【洛谷p3956】棋盘