org.dom4j.io.SAXReader解析xml
参考网址
XPath 语法
http://www.w3school.com.cn/xpath/xpath_syntax.asp
XML文件
G:\eclipsewk\SDK201702\Test-Pack\package\work\before\AndroidManifest.xml
<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.android.test" android:versionCode="1"android:versionName="1.0" ><uses-sdk
android:minSdkVersion="8"android:targetSdkVersion="21" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.GET_TASKS" /><uses-permission android:name="android.permission.WAKE_LOCK" /><application><activity
android:name="com.android.test.MainActivity"android:label="@string/app_name" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><service android:name="com.android.test.CoreService"android:exported="true" ></service></application></manifest>
解析代码
Element org.dom4j.Document.getRootElement()
InputStream fis=new FileInputStream("G:\\eclipsewk\\SDK201702\\Test-Pack\\package\\work\\before\\AndroidManifest.xml");
Document document = new SAXReader().read(fis);// 得到Document对象
Element root = document.getRootElement();// 获得根节点
System.out.println("-----------------root----------------");
System.out.println(root.asXML());//打印出整个xml文件的内容
打印的结果是:
-----------------root----------------
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.test" android:versionCode="1" android:versionName="1.0"><uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21"/><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/><uses-permission android:name="android.permission.GET_TASKS"/><uses-permission android:name="android.permission.WAKE_LOCK"/><application><activity android:name="com.android.test.MainActivity" android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN"/><category android:name="android.intent.category.LAUNCHER"/></intent-filter></activity><service android:name="com.android.test.CoreService"android:exported="true" ></service></application></manifest>
Attribute org.dom4j.Element.attribute(String paramString)
String attribute=root.attribute("package").getText();//获取一个attribute的值
System.out.println("-----------------package----------------"+attribute);/***打印结果:
-----------------package----------------com.android.test
*/
String attribute=root.attribute("application").getText();//会报错java.lang.NullPointerException
System.out.println("-----------------application----------------"+attribute);/***打印结果:
java.lang.NullPointerExceptionat pack.SAXReaderXML.main(SAXReaderXML.java:...)
*/
Node org.dom4j.Node.selectSingleNode(String paramString)
Element element = (Element)root.selectSingleNode("//application");
System.out.println("----selectSingleNode application ----");
System.out.println("application "+element.asXML());/***打印结果:
----selectSingleNode application ----
application <application><activity xmlns:android="http://schemas.android.com/apk/res/android" android:name="com.android.test.MainActivity" android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN"/><category android:name="android.intent.category.LAUNCHER"/></intent-filter></activity><service android:name="com.android.test.CoreService"android:exported="true" ></service>
</application>*/
/**再来看一个例子
//表示从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置**/
Element element = (Element)root.selectSingleNode("//application//activity//intent-filter");
System.out.println(element.asXML());/***打印结果:
<intent-filter><action xmlns:android="http://schemas.android.com/apk/res/android" android:name="android.intent.action.MAIN"/><category xmlns:android="http://schemas.android.com/apk/res/android" android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
*/
List org.dom4j.Node.selectNodes(String paramString)
System.out.println("----selectNodes uses-permission ----");
List<?> nodesList = root.selectNodes("//uses-permission");
for (int i = 0; i < nodesList.size(); i++) {System.out.println("-----node------"+i);Element node = (Element)nodesList.get(i);System.out.println(node.asXML());
}/***打印结果:
----selectNodes uses-permission ----
-----node------0
<uses-permission xmlns:android="http://schemas.android.com/apk/res/android"
android:name="android.permission.ACCESS_NETWORK_STATE"/>
-----node------1
<uses-permission xmlns:android="http://schemas.android.com/apk/res/android" android:name="android.permission.GET_TASKS"/>
-----node------2
<uses-permission xmlns:android="http://schemas.android.com/apk/res/android" android:name="android.permission.WAKE_LOCK"/>
*/
Iterator org.dom4j.Element.elementIterator()
System.out.println("---------Iterator org.dom4j.Element.elementIterator()-----------");
Element element = (Element)root.selectSingleNode("//application");
Iterator iterator=element.elementIterator();//再从子节点在遍历其子节点
while (iterator.hasNext()) {Element next = (Element) iterator.next();System.out.println("---------next.asXML-----------");System.out.println(next.asXML());
}/***打印结果:
---------Iterator org.dom4j.Element.elementIterator()-----------
---------next.asXML-----------
<activity xmlns:android="http://schemas.android.com/apk/res/android" android:name="com.android.test.MainActivity" android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN"/><category android:name="android.intent.category.LAUNCHER"/></intent-filter>
</activity>
---------next.asXML-----------
<service xmlns:android="http://schemas.android.com/apk/res/android"android:name="com.android.test.CoreService" android:exported="true">
</service>
*/
全部代码
package pack;import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;public class SAXReaderXML {public static void main(String[] args) {try {InputStream fis=new FileInputStream("G:\\eclipsewk\\SDK201702\\Test-Pack\\package\\work\\before\\AndroidManifest.xml");//Document document = new SAXReader().read(fis);// 得到Document对象Element root = document.getRootElement();// 获得根节点
// System.out.println("-----------------root----------------");
// System.out.println(root.asXML());// String attribute=root.attribute("package").getText();
// System.out.println("-----------------package----------------"+attribute);// String attribute=root.attribute("application").getText();//java.lang.NullPointerException
// System.out.println("-----------------application----------------"+attribute);
// // Element element = (Element)root.selectSingleNode("//application");
// System.out.println("----selectSingleNode application ----");
// System.out.println("application "+element.asXML());// Element element = (Element)root.selectSingleNode("//application//activity//intent-filter");
// System.out.println(element.asXML());// System.out.println("----selectNodes uses-permission ----");
// List<?> nodesList = root.selectNodes("//uses-permission");
// for (int i = 0; i < nodesList.size(); i++) {// System.out.println("-----node------"+i);
// Element node = (Element)nodesList.get(i);
// System.out.println(node.asXML());
// }// System.out.println("---------Iterator org.dom4j.Element.elementIterator()-----------");
// Element element = (Element)root.selectSingleNode("//application");
// Iterator iterator=element.elementIterator();//再从子节点在遍历其子节点
// while (iterator.hasNext()) {// Element next = (Element) iterator.next();
// System.out.println("---------next.asXML-----------");
// System.out.println(next.asXML());
// }System.out.println("---------Iterator org.dom4j.Element.elementIterator()-----------");Element element = (Element)root.selectSingleNode("//application");Iterator iterator=element.elementIterator();//再从子节点在遍历其子节点while (iterator.hasNext()) {Element next = (Element) iterator.next();System.out.println("---------next.asXML-----------");System.out.println(next.asXML());System.out.println("---------next getName-----------");System.out.println(next.getName());System.out.println("---------next getText-----------");System.out.println(next.getText());}} catch (Exception e) {e.printStackTrace();}}
}
Node、Element、Attribute
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore><book><title lang="eng">Harry Potter</title><price>29.99</price></book>
</bookstore>/**
1、想要获取title 的名字怎么做?
2、想要获取lang="eng"这个名字和对应的值怎么做?
3、想要获取39.95这个值怎么做?
*/
package pack;import java.io.FileInputStream;
import java.io.InputStream;import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;public class SAXReaderXML2 {public static void main(String[] args) {try {InputStream fis=new FileInputStream("G:\\eclipsewk\\SDK201702\\Test-Pack\\package\\work\\before\\bookstore.xml");//Document document = new SAXReader().read(fis);// 得到Document对象Element root = document.getRootElement();// 获得根节点Element titleElement = (Element)root.selectSingleNode("//book//title");System.out.println("---------titleElement asXML-----------");System.out.println(titleElement.asXML());Attribute attribute=titleElement.attribute("lang");System.out.println("-----String org.dom4j.Node.getName()--"+attribute.getName());System.out.println("-----String org.dom4j.Node.getValue()--"+attribute.getValue());Element priceElement = (Element)root.selectSingleNode("//book//price");System.out.println("---------String org.dom4j.Node.asXML()-----------");System.out.println(priceElement.asXML());System.out.println("-----String org.dom4j.Node.getName()--"+priceElement.getName());System.out.println("-----String org.dom4j.Element.getText()--"+priceElement.getText());} catch (Exception e) {e.printStackTrace();}}
}
总结
1、Element和Node是什么关系?先看org.dom4j.Element的定义
public abstract interface Element extends Branch再看org.dom4j.Branch的定义
public abstract interface Branch extends Node2、Element和Attribute是什么关系?先看org.dom4j.Attribute的定义
public abstract interface Attribute extends Node从Element中获取Attribute怎么做
Attribute org.dom4j.Element.attribute(String paramString)3、XML文件和Element是什么关系?XML文件由多个Element构成。每一个Element下面可以有多个子Element
4、Element长什么样子<ParentElementgetName><ChildElementgetName AttributegetName="AttributegetValue">ChildElementgetText</ChildElementgetName></ParentElementgetName>看到这个你get到String org.dom4j.Node.getName()、String org.dom4j.Element.getText()、String org.dom4j.Node.getValue()三个函数的用法了吗?
org.dom4j.io.SAXReader解析xml相关推荐
- DocumentHelper和SAXReader解析XML字符串
最近在看湖北中烟OA门户的项目,中烟门户里面解析XML文档使用的是DocumentHelper,之前做的智能导航项目里面解析XML使用的是SAXReader,很好奇它们有什么区别,于是就查资料将两者的 ...
- 使用jdk DOM,SAX和第三方jar包DOM4J创建,解析xml文件
xml的创建,解析 1. 什么是xml文件 1.1 什么是xml文件 1.2 解析xml的方式,优缺点 2. 使用dom操作xml文件 2.1 使用dom创建xml文件 2.2 使用dom解析xml文 ...
- dom4j创建、解析xml文件(增删改查)
先对xml文件进行解析,xml文件如下图所示 <?xml version="1.0" encoding="UTF-8"?> <?eclipse ...
- Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: org/dom4j/io/SAXReader
Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: org/dom4j/io/SAXReader ...
- 简单使用SAXReader解析xml数据
之前的工作中,一直是使用json格式的数据进行数据传输.很少会接触到xml格式的数据.不过因为工作需求,在对接其他产品的接口时,偶尔会遇到需要使用xml格式数据的情况,所以,也得学学如何解析xml.不 ...
- 用SAXReader解析xml文档
使用SAXReader需要导入dom4j-full.jar包. dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的.dom4j是一个非常非常优秀的Java XML API, ...
- java.lang.NoClassDefFoundError: org/dom4j/io/SAXReader
已经引入了jar包, 还是报java.lang.NoClassDefFoundError: org/dom4j/io/SAXReader 点击PUT INTO OUTPUT ROOT , web-i ...
- java saxreader 字符串_DocumentHelper 和SAXReader 解析xml 字符串
DocumentHelper 解析xml字符串 String xml=com.jetsen.platform.util.FileUtil.getFileContent(request.getSessi ...
- SAXReader解析xml繁体字乱码问题
SAXReader reader = new SAXReader(); //解析时默认为空,需设置 reader.setEncoding("gbk"); 转载于:https://b ...
- dom4j生成、解析xml
Java代码 /** * 创建xml * @param obj 泛型对象 * @param entityPropertys 泛型对象的List集合 * @param Encode ...
最新文章
- 输入列号得到excel对应的字母列
- 数学建模matlab imread,《matlab数学建模方法与实践》第三章 数据的处理
- 轻松简单地开发Web Services 2
- django定义模型类-14
- 基于Prometheus和Grafana打造业务监控看板
- java是如何实现原语的_Java中的低GC:使用原语而不是包装器
- 最近和前字节跳动大佬聊了聊今年春招面试的变化
- opencv java水平投影_使用OpenCv中Mat进行水平投影与垂直投影并实现字符切分
- java if函数的使用方法_java – 如何使用新的computeIfAbsent函数?
- R语言使用merge函数合并数据,如何保持原始数据顺序
- Setting property 'source' to 'org.eclipse.jst.jee.
- 科大讯飞 语音识别 VB WIN10 X64 VS2017
- 《程序员的数学》读书计划
- 2021中青杯数学建模A题 (论文+代码) 超详细
- It is possible to bind and connect to localhost:8080 at the same time - application server will prob
- Win7计算机内存不足,请保存文件并关闭这些程序
- 【记录】kali制作绕过火绒检测的木马(仅通过MSF的方式)
- 威胁猎人产品总监彭巍:业务安全发展趋势及对安全研发的挑战
- IDEA的接口测试工具
- LAMP环境搭建与配置(一)
热门文章
- Windows10(MSN)天气数据爬取
- 文明4 java_文明4主题曲《Baba Yetu》(敬请关注中文歌词部分~)
- 【笔记环境】Typora+Joplin+PicGo+Gitee+Onedrive
- excel使用教程_有哪些超好用、高质量的Excel学习网站?
- shark恒破解笔记2-绕过自校验
- word章节模板构建:新建样式和多级列表。(附插入目录)
- @DependsOn
- 嵌入式大佬 | 嵌入式C语言知识点万字总结
- handler机制及使用场景
- qpsk matlab仿真,qpsk的matlab和simulink仿真.pdf