java 操作xml

package dataModule;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
//下面主要是org.xml.sax包的类
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Xml {
String property="";
public String getProperty() {
return property;
}
public void setProperty(String property) {
this.property = property;
}
public ArrayList getXml(String path){
ArrayList al = new ArrayList();

//(1)得到DOM解析器的工厂实例
DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();
//得到javax.xml.parsers.DocumentBuilderFactory;类的实例就是我们要的解析器工厂
try {
//(2)从DOM工厂获得DOM解析器
DocumentBuilder dombuilder=domfac.newDocumentBuilder();
//通过javax.xml.parsers.DocumentBuilderFactory实例的静态方法newDocumentBuilder()得到DOM解析器
//(3)把要解析的XML文档转化为输入流,以便DOM解析器解析它
InputStream is=new FileInputStream(path);
//(4)解析XML文档的输入流,得到一个Document
Document doc=dombuilder.parse(is);
//由XML文档的输入流得到一个org.w3c.dom.Document对象,以后的处理都是对Document对象进行的
//(5)得到XML文档的根节点
Element root=doc.getDocumentElement();
//在DOM中只有根节点是一个org.w3c.dom.Element对象。
//(6)得到节点的子节点
NodeList estate=root.getChildNodes();

if(estate!=null){
for(int i=0;i<estate.getLength();i++){
Node city=estate.item(i);
if(city.getNodeType()==Node.ELEMENT_NODE){
//(7)取得节点的属性值
String cityname=city.getAttributes().getNamedItem("cityname").getNodeValue();

//注意,节点的属性也是它的子节点。它的节点类型也是Node.ELEMENT_NODE
//(8)轮循子节点

for(Node node=city.getFirstChild();node!=null;node=node.getNextSibling()){
Xml xml =new Xml();
if(node.getNodeType()==Node.ELEMENT_NODE){
if(node.getNodeName().equals("property1")){

property=node.getFirstChild().getNodeValue();
//

}
if(node.getNodeName().equals("property2")){
property=node.getFirstChild().getNodeValue();


}
if(node.getNodeName().equals("property3")){
property=node.getFirstChild().getNodeValue();


}
if(node.getNodeName().equals("property4")){
property=node.getFirstChild().getNodeValue();


}
if(node.getNodeName().equals("property5")){
property=node.getFirstChild().getNodeValue();


}
xml.setProperty(property);
al.add(xml);

}
}
}
}//(6)这是用一个org.w3c.dom.NodeList接口来存放它所有子节点的,还有一种轮循子节点的方法,后面有介绍
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return al;

}



}


xml文件
<?xml version="1.0" encoding="GB2312" standalone="no"?>
<estate>
<city cityname="chongqing">
<property1>CQ15-P000123291</property1>
<property2>CQ62-P000095732</property2>
<property3>CQ41-P000101178</property3>
<property4>CQ58-P000077181</property4>

</city>
</estate>

调用
Xml xml = new Xml();
ArrayList al = xml.getXml(path);
int alRows = al.size();
for(int i=0;i<alRows;i++){
Xml xml1 = (Xml)al.get(i);
.........
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值