XML文件解析之JDOM解析
1.JDOM介绍
JDOM的官方网站是http://www.jdom.org/,JDOM解析用到的jar包可以在http://www.jdom.org/dist/binary/中下载,最新的JDOM2的版本是2.0.5,JDOM1的版本是1.1.3,根据官网中的介绍可以知道。JDOM是一个在基于内存的XML模型,它用于读写创建修改XML文档。JDOM和DOM相似因为他们都提供了内存XML文档模型,但是DOM被设计用于很多种语言(C,C++,ECMSctipr,Java,JScript,Lingo,PHP,PLSQL和Python),但是JDOM只是被设计用来处理Java。因为这个原因JDOM不遵循w3c标准规范。JDOM不是一个XML解析器但是它可以使用SAX,STAX或者DOM解析器用来构建一个JDOM文档。
2 JDOM2相关介绍:
2.1.1主要的包包括
org.jdom2 This package contains the core classes that represent the XML components. Document - Represents the complete XML document. It provides access to the root element and also the docType.
DocType - Represents an XML DOCTYPE.
Element - Represents an XML element. Has methods to manipulate the child elements, its textual content, attributes and namespaces.
Attribute - This represents an XML attribute. There are methods to get and set the value of the attribute and also the namespace. The Attribute has a parent and an AttributeType.
Namespace - Represents an XML Namespace.
CDATA - This represents an XML CDATA section.
Text - This represents an XML Text.
Comment - Represents an XML comment. Users can get and set comment text.
EntityRef - Represents an XML EnityRef.
ProcessingInstruction - Represents an XML Processing Instruction
Content - This is an abstract class which is extended by those classes that can be a child of a org.jdom2.Parent class (Element, CDATA, Comment, DocType, EntityRef, ProcessingInstruction, Text)
org.jdom2.filter These package has filters that allow filtering based on type, name, value or other parameters. It also allows ANDing, ORing and Negating filters. Filters are used in the public <E extends Content> List<E> getContent(final Filter<E> filter) and public <F extends Content> IteratorIterable<F> getDescendants(final Filter<F> filter) methods of the Element class. Filters are also using in the XPath package of JDom2. org.jdom2.input This package has the core classes responsible for building the JDOM2 document from DOM, SAX or StAX. The important classes are SAXBuilder - Builds a JDOM document from SAX parser.
DOMBuilder - Builds a JDOM document from pre-existing org.w3c.dom.DOM document.
StaxEventBuilder - Builds a JDOM document from StAX XMLEventReader
StaxStreamBuilder - Builds a JDOM document from StAX XMLStreamReader
org.jdom2.output These package has classes to ouput the JDOM2 document to various destinations. The main classes are : XMLOutputter - Output a JDOM2 document as a stream of bytes.
DOMOutputter - Convert a JDOM2 document into a DOM document. It can also convert various JDOM2 components to its equivalent DOM components
SAXOutputter - Convert a JDOM2 document into a stream of SAX events
StAXEventOutputter - output a JDOM2 document to an XMLEventWriter.
StAXStreamOutputter - Output a JDOM2 document to an XMLStreamWriter.
Other Packages org.jdom2.transform - Helps with XML transformation using JAXP TrAX classes.
org.jdom2.xpath - Support for XPath in JDOM2. By default, it uses the Jaxen xpath library
2.1.2具体的实现
http://www.cnblogs.com/hoojo/archive/2011/08/11/2134638.html 这篇文章介绍的很详细,这里不再赘述。感谢@hoojo
3、JDOM1的介绍
废话少说直接写实现:
3.1处理的XML文档内容
<?xml version="1.0" encoding="UTF-8"?>
<world>
<comuntry id="1">
<name>China</name>
<capital>Beijing</capital>
<population>1234</population>
<area>960</area>
</comuntry>
<comuntry id="2">
<name id="">America</name>
<capital>Washington</capital>
<population>234</population>
<area>900</area>
</comuntry>
<comuntry id="3">
<name >Japan</name>
<capital>Tokyo</capital>
<population>234</population>
<area>60</area>
</comuntry>
<comuntry id="4">
<name >Russia</name>
<capital>Moscow</capital>
<population>34</population>
<area>1960</area>
</comuntry>
</world>
3.2具体的实现代码包括读写过程
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List; import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter; public class JDOMParse { public static void main(String[] args) {
// TODO Auto-generated method stub
parseXML(new File("world.xml"));
createXML(); } public static void parseXML(File xmlPath){ SAXBuilder sb = new SAXBuilder();
try {
Document doc = sb.build(new FileInputStream(xmlPath));
Element root = doc.getRootElement();//获得XML的根元素
System.out.println("根节点:"+root.getName());
System.out.println("-----------------------------");
List list = root.getChildren();
for(int i = 0; i < list.size(); i++){
Element element = (Element)list.get(i);
System.out.println("子节点名称:"+element.getName());
List att_list = element.getAttributes();
for(int j = 0; j < att_list.size(); j++){
Attribute att = (Attribute)att_list.get(j);
System.out.println(att.getName()+":"+att.getValue());
}
List tempist = element.getChildren();
for(int t = 0; t < tempist.size(); t++){
Element temp = (Element)tempist.get(t);
System.out.println(temp.getName()+":"+temp.getValue());
} }
System.out.println("------------------结束--------------------------");
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } public static void createXML(){
Element root = new Element("root");//创建根元素
Document doc=new Document(root);
Element world = new Element("world");//根元素的子元素
root.addContent(world);//添加到根
for(int i=0;i<4;i++){
Element comuntry = new Element("comuntry");//根元素的子元素
Attribute att = new Attribute("id",i+"");
comuntry.setAttribute(att);
world.addContent(comuntry);
Element name = new Element("name");//根元素的子元素
name.setText("中国");
Element capital = new Element("capital");//根元素的子元素
capital.setText("Beijing");
Element population = new Element("population");//根元素的子元素
name.setText("34");
Element area = new Element("area");//根元素的子元素
area.setText("1960"); comuntry.addContent(name).addContent(capital).addContent(population).addContent(area);
}
XMLOutputter out = new XMLOutputter();
out.setFormat(Format.getCompactFormat().setEncoding("GB2312"));
System.out.println(out.outputString(doc));
}
}
3.3.结果
根节点:world
-----------------------------
子节点名称:comuntry
id:1
name:China
capital:Beijing
population:1234
area:960
子节点名称:comuntry
id:2
name:America
capital:Washington
population:234
area:900
子节点名称:comuntry
id:3
name:Japan
capital:Tokyo
population:234
area:60
子节点名称:comuntry
id:4
name:Russia
capital:Moscow
population:34
area:1960
------------------结束--------------------------
<?xml version="1.0" encoding="GB2312"?>
<root><world><comuntry id="0"><name>34</name><capital>Beijing</capital><population /><area>1960</area></comuntry><comuntry id="1"><name>34</name><capital>Beijing</capital><population /><area>1960</area></comuntry><comuntry id="2"><name>34</name><capital>Beijing</capital><population /><area>1960</area></comuntry><comuntry id="3"><name>34</name><capital>Beijing</capital><population /><area>1960</area></comuntry></world></root>
3.4上面的例子是一个很粗糙的举例,需要完善和改进的地方还有很多,比如更加通用化使用递归等等。
4.上面都只是一个简单的总结和记录,很多的不完善的地方,希望大家多多批评指正。
至此XML文件解析的四种方式都已经结束完毕了。
XML文件解析之JDOM解析的更多相关文章
- JAVA解析XML文件(DOM,SAX,JDOM,DOM4j附代码实现)
		
1.解析XML主要有四种方式 1.DOM方式解析XML(与平台无关,JAVA提供,一次性加载XML文件内容,形成树结构,不适用于大文件) 2.SAX方式解析XML(基于事件驱动,逐条解析,适用于只处理 ...
 - XML文件的创建和解析笔记
		
解析XML的四种方法 XML现在已经成为一种通用的数据交换格式,它的平台无关性,语言无关性,系统无关性,给数据集成与交互带来了极大的方便.对于XML本身的语法知识与技术细节,需要阅读相关的技术文献,这 ...
 - XML概念定义以及如何定义xml文件编写约束条件java解析xml   DTD XML Schema JAXP java xml解析 dom4j  解析 xpath dom sax
		
本文主要涉及:xml概念描述,xml的约束文件,dtd,xsd文件的定义使用,如何在xml中引用xsd文件,如何使用java解析xml,解析xml方式dom sax,dom4j解析xml文件 XML来 ...
 - xml文件的生成与解析
		
生成方法一:同事StringBuffer类对xml文件格式解析写入 package com.steel_rocky.xml; import android.app.Activity; import a ...
 - 使用XML序列化器生成XML文件和利用pull解析XML文件
		
首先,指定XML格式,我指定的XML格式如下: <?xml version='1.0' encoding='utf-8' standalone='yes' ?> <message&g ...
 - 解析XML文件之使用SAM解析器
		
XML是一种常见的传输数据方式,所以在开发中,我们会遇到对XML文件进行解析的时候,本篇主要介绍使用SAM解析器,对XML文件进行解析. SAX解析器的长处是显而易见的.那就是SAX并不须要将全部的文 ...
 - XML文件详解以及解析
		
转自:https://blog.csdn.net/com_ma/article/details/73277535 一.xml基础详解: 1.概述: xml:即可扩展标记语言,xml是互联网数据传输的重 ...
 - Python解析xml文件遇到的编码解析的问题
		
使用python对xml文件进行解析的时候,假设xml文件的头文件是utf-8格式的编码,那么解析是ok的,但假设是其它格式将会出现例如以下异常: xml.parsers.expat.ExpatErr ...
 - 解析XML文件之使用DOM解析器
		
在前面的文章中.介绍了使用SAX解析器对XML文件进行解析.SAX解析器的长处就是占用内存小.这篇文章主要介绍使用DOM解析器对XML文件进行解析. DOM解析器的长处可能是理解起来比較的直观,当然, ...
 
随机推荐
- Release报错Debug无错
			
代码在Release模式下会crash,Debug模式下可以运行,最后定位到原因 for (size_t j = 0; j < ids.size()-1; ++j) { } 发现问题是Relea ...
 - mysql常用命令、非交互式mysql命令看29条
			
CentOS下mysql数据库常用命令总结1.更改root密码 mysqladmin -uroot password 'yourpassword' 2.远程登陆mysql服务器 mysql -uroo ...
 - robot:生成随机的8为纯数字
			
1.引进random库 2.注意最后面的random为需要引入的包
 - c# 如何给 dataGridView里添加一个自增长列(列名为序号)
			
System.Data.DataTable table = new DataTable(); System.Data.DataColumn column = new Da ...
 - 《剑指offer》数学题及其它 (牛客11.05)
			
比较多的思维题,涉及位运算.快速幂.二进制.约瑟夫问题.队列.贪心.dp等等. 难度 题目 知识点 ☆ 12.数值的整数次方 细节,快速幂 ☆☆ 47.求1+2+3+···+n 思维发散 ☆☆ 48. ...
 - 机器学习算法K-NN的一个使用实例:预测一个人是否患有糖尿病 (KNN-Predict  whether a person will have diabetes or not  )
			
学习中...不断更新. 在糖尿病人的数据库中有几列是不能为0的 比如葡萄糖 胰岛素 身体指数和皮肤厚度.所以在数据预处理阶段需要对这些列的数据进行替换. remeber we did 12 minus ...
 - laravel的定时任务
			
首先在laravel项目命令创建: php artisan make:command TestCommand 会在App\Console\Commands文件下看到TestCommand.php文件, ...
 - 华为eNSP上的NAT地址转换配置
			
NAT是将IP数据报文报头中的IP地址转换为另一个IP地址的过程,主要用于实现内部网络(私有IP地址)访问外部网络(公有IP地址)的功能. 1.实验拓扑 地址表: 1.完成各个接口基本配置之后使用pi ...
 - hdoj3336(kmp算法next数组的应用)
			
题目链接:https://vjudge.net/problem/HDU-3336 题意:给定长为n(<=2e5)的字符串s,求s的每个前缀在s中出现的次数之和. 思路: 用dp[i]表示以s[i ...
 - WUSTOJ 1246: 字符串排序(Java)
			
1246: 字符串排序 题目 输入n(n<100)个字符串,每个字符串长度不超过1000,将他们按字典顺序输出.更过内容点击标题. 分析 Java中的ArrayList()可以比较方便的 ...