Java读取XML一般是两种方式,DOM解析和SAX解析。
DOM解析:一次性把xml文档加载成Document树,通过Document对象得到节点对象,通过节点对象访问xml文档内容(标签,属性,文本,注释),常用工具Dom4j。
SAX解析:对于XML文件节点内容加载一点,解析一点并处理 ,对内存要求比较不高,适合大容量文件读取。缺点:不能对XML文档进行修改。
1 Dom4j读取XML文件
1 引入包 import org.dom4j.*
XML文件内容
<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person id="1">
<name>刘备</name>
<age>30</age>
</person>
<person id="2">
<name>关羽</name>
<age>29</age>
</person>
<person id="3">
<name>张飞</name>
<age>28</age>
</person>
</persons>
Java代码
package cjr.xml;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;
public class ReadXmlDemo {
@Test
public void readXML() throws Exception{
List<Person> lsPersons = new ArrayList<Person>();
String filePath="./src/Person.xml";
Document doc = new SAXReader().read(filePath);
//1 获取根节点 Persons
Element rootEle = doc.getRootElement();
@SuppressWarnings("unchecked")
//2 获取所有元素节点,并解析装载
List<Element> elems = (List<Element>) rootEle.elements();
for(Element e : elems){
//读取属性
String id = e.attribute("id").getValue();
//读取文本
String name = e.element("name").getText();
//读取文本
int age =Integer.parseInt(e.element("age").getText());
Person person = new Person();
person.setId(id);
person.setName(name);
person.setAge(age);
lsPersons.add(person);
}
for(Person p : lsPersons){
System.out.println(p.toString());
}
}
}
实体对象Person
package cjr.xml;
public class Person {
private String id;
private String name;
private int age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "ID:" + this.id + ";Name:" + this.name + ";Age:" + this.age
+ ";";
}
public Person(String id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public Person(){}
}
运行结果:
ID:1;Name:刘备;Age:30;
ID:2;Name:关羽;Age:29;
ID:3;Name:张飞;Age:28;
2 Dom4j修改XML文件
核心java代码
package cjr.xml;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;
public class ReadXmlDemo {
/**
* @Description:copy一份XML,对属性,文本进行修改
* @author chenjiaren
* @date 2016-6-3
*/
@Test
public void editXML() throws Exception{
//读入文档
SAXReader reader = new SAXReader();//XML解析器
Document doc = reader.read("./src/Person.xml");
//修改相关信息
Element rootEle = doc.getRootElement();
Element ele1 = (Element) rootEle.elements().get(0);//获取第一个person标签
ele1.attribute("id").setValue("11");//修改第一个person的属性值
ele1.addAttribute("address", "hangzhou");//为第一个person对象添加address属性
ele1.element("name").setText("孔明");//修改第一个person下的name标签文本值
Element ele2 = (Element) rootEle.elements().get(1);//获取第二个person标签
ele2.attribute("id").detach();//删除第二个person的id属性
ele2.element("age").detach();//删除第二个person下的age标签
Element ele3 = (Element) rootEle.elements().get(2);//获取第二个person标签
ele3.detach();//删除第三个标签
Element newEle = rootEle.addElement("person");
newEle.addAttribute("id", "4");
newEle.addElement("name").setText("曹操");
newEle.addElement("age").setText("60");
//写出文档
FileOutputStream output = new FileOutputStream("./src/Person_copy.xml");
OutputFormat format = OutputFormat.createPrettyPrint();//格式化后显示
//format = OutputFormat.createCompactFormat();//紧凑格式,标签与标签之间没有空格换行
//format.setEncoding("GBK");//通过format设置XML的编码格式
XMLWriter writer = new XMLWriter(output,format);
writer.write(doc);
output.close();
System.out.println("修改成功!");
}
}
运行结果:另存后的Person_copy.xml文件内容
<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person id="11" address="hangzhou">
<name>孔明</name>
<age>30</age>
</person>
<person>
<name>关羽</name>
</person>
<person id="4">
<name>曹操</name>
<age>60</age>
</person>
</persons>
3 xPath快速定位节点
核心代码:
package cjr.xml;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.junit.Test;
public class XPathDemo {
@Test
public void readXMLByXPath() throws Exception{
Document doc = new SAXReader().read("./src/Person_copy.xml");
@SuppressWarnings("unchecked")
List<Node> lsNodes = doc.selectNodes("/persons/person[@id]");
for(Node n : lsNodes){
Element e = (Element)n;
System.out.println(e.getName());
}
}
}
运行结果:
person
person
4 SAX解析XML文档
1 导入包:import org.xml.sax.*
2 java代码
package cjr.xml;
import java.io.FileInputStream;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.junit.Test;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SAXReaderDemo {
@Test
public void readXML() throws Exception{
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
XMLHandler handler = new XMLHandler();
parser.parse(new FileInputStream("./src/Person.xml"), handler);
}
}
//定义自己的解析器
class XMLHandler extends DefaultHandler
{
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
String str = "<"+qName;
for(int i=0,len= attributes.getLength();i<len;i++){
str+=" "+attributes.getQName(i)+"=\""+attributes.getValue(i)+"\"";
}
str+=">";
System.out.print(str);
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
System.out.print("</"+ qName +">");
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
System.out.print(new String(ch, start, length));
}
}
<persons>
<person id="1">
<name>刘备</name>
<age>30</age>
</person>
<person id="2">
<name>关羽</name>
<age>29</age>
</person>
<person id="3">
<name>张飞</name>
<age>28</age>
</person>
</persons>
5 JS读取XML文件
js代码
loadXML = function(xmlFile){
var xmlDoc=null;
//判断浏览器的类型
//支持IE浏览器
var mb = myBrowser();
if(mb="Chrome"){
var xmlhttp = new window.XMLHttpRequest();
xmlhttp.open("GET",xmlFile,false);
xmlhttp.send(null);
xmlDoc = xmlhttp.responseXML;
return xmlDoc;
}
if(!window.DOMParser && window.ActiveXObject){
var xmlDomVersions = ['MSXML.2.DOMDocument.6.0','MSXML.2.DOMDocument.3.0','Microsoft.XMLDOM'];
for(var i=0;i<xmlDomVersions.length;i++){
try{
xmlDoc = new ActiveXObject(xmlDomVersions[i]);
break;
}catch(e){
}
}
}
//支持Mozilla浏览器
else if(document.implementation && document.implementation.createDocument){
try{
/* document.implementation.createDocument('','',null); 方法的三个参数说明
* 第一个参数是包含文档所使用的命名空间URI的字符串;
* 第二个参数是包含文档根元素名称的字符串;
* 第三个参数是要创建的文档类型(也称为doctype)
*/
xmlDoc = document.implementation.createDocument('','',null);
}catch(e){
}
}
else{
return null;
}
if(xmlDoc!=null){
xmlDoc.async = false;
xmlDoc.load(xmlFile);
}
return xmlDoc;
};
function doLoadXML(){
var str="XML内容:<br />";
var xmldoc=loadXML("test.xml");
var elements = xmldoc.getElementsByTagName("Company");
for (var i = 0; i < elements.length; i++) {
var name = elements[i].getElementsByTagName("cNname")[0].firstChild.nodeValue;
var ip = elements[i].getElementsByTagName("cIP")[0].firstChild.nodeValue;
str+="姓名:" + name + " ; IP: " + ip +"<br />";
}
document.getElementById("content").innerHTML=str;
}
XML文件内容
<?xml version="1.0" encoding="utf-8" ?>
<DongFang>
<Company>
<cNname>1</cNname>
<cIP>陈佳仁</cIP>
</Company>
<Company>
<cNname>2</cNname>
<cIP>2</cIP>
</Company>
<Company>
<cNname>3</cNname>
<cIP>的积分兑换</cIP>
</Company>
<Company>
<cNname>4</cNname>
<cIP>4</cIP>
</Company>
<Company>
<cNname>5</cNname>
<cIP>5</cIP>
</Company>
<Company>
<cNname>6</cNname>
<cIP>6</cIP>
</Company>
</DongFang>
5 Jquery读取XML文件
script代码
function doLoadXML1(){
var str="XML内容:<br />";
$.ajax({
//请求方式为get
type: "GET",
//xml文件位置
url: "test.xml",
//返回数据格式为xml
dataType: "xml",
//请求成功完成后要执行的方法
success: function (xml) {
var elements = xml.getElementsByTagName("Company");
for (var i = 0; i < elements.length; i++) {
var name = elements[i].getElementsByTagName("cNname")[0].firstChild.nodeValue;
var ip = elements[i].getElementsByTagName("cIP")[0].firstChild.nodeValue;
str+="姓名:" + name + " ; IP: " + ip +"<br />";
}
document.getElementById("content").innerHTML=str;
}
});
}
注: chrome浏览器会读取失败,因为chrome设置浏览器不能读取本地文件