java解析xml文件
java如何解析层节点的xml文件,最好用dom的方法解析,例子如下<?xmlversion="1.0"encoding="utf-8"?><glassversion="...
java如何解析层节点的xml文件,最好用dom的方法解析,例子如下
<?xml version="1.0" encoding="utf-8"?>
<glass version="***">
<work>
<job>
<id>job0001</id>
<workId>work0001</workId>
<name>GS01 </name>
<type>abedo</type>
<tag>mod09GA</tag>
<rule>
<start>2000-1-1</start>
<timeUnit>year</timeUnit>
<timeLength>1</timeLength>
<stop>2005-12-32</stop>
</rule>
<rule>
<start>2006-1-1</start>
<timeUnit>month</timeUnit>
<timeLength>6</timeLength>
<stop>2009-12-31</stop>
</rule>
</job>
<job>
<id>job0002</id>
<workId>work0001</workId>
<name>GS01 </name>
<type>abedo</type>
<tag>mod09GA</tag>
<rule>
<start>2000-1-1</start>
<timeUnit>year</timeUnit>
<timeLength>1</timeLength>
<stop>2005-12-32</stop>
</rule>
<rule>
<start>2006-1-1</start>
<timeUnit>month</timeUnit>
<timeLength>6</timeLength>
<stop>2009-12-31</stop>
</rule>
</job>
</work>
</glass>
问题里面不就是例子吗,晕 展开
<?xml version="1.0" encoding="utf-8"?>
<glass version="***">
<work>
<job>
<id>job0001</id>
<workId>work0001</workId>
<name>GS01 </name>
<type>abedo</type>
<tag>mod09GA</tag>
<rule>
<start>2000-1-1</start>
<timeUnit>year</timeUnit>
<timeLength>1</timeLength>
<stop>2005-12-32</stop>
</rule>
<rule>
<start>2006-1-1</start>
<timeUnit>month</timeUnit>
<timeLength>6</timeLength>
<stop>2009-12-31</stop>
</rule>
</job>
<job>
<id>job0002</id>
<workId>work0001</workId>
<name>GS01 </name>
<type>abedo</type>
<tag>mod09GA</tag>
<rule>
<start>2000-1-1</start>
<timeUnit>year</timeUnit>
<timeLength>1</timeLength>
<stop>2005-12-32</stop>
</rule>
<rule>
<start>2006-1-1</start>
<timeUnit>month</timeUnit>
<timeLength>6</timeLength>
<stop>2009-12-31</stop>
</rule>
</job>
</work>
</glass>
问题里面不就是例子吗,晕 展开
展开全部
SaxReader reader = new SaxReader();
Document doc = reader.read(new File("class.xml"));
Element element = doc.getRootElement();//获取根元素
迭代器:取根元素下的子元素名称
Iterator<Element> iter = element.elementIterator();
while(iter.hashNext){
Element el = (Element)iter.next();
el.getName();//获取元素名称
el.getText();//获取元素值
}
//获取属性名称、值
Iterator<Attribute> iter1 = element.attributeIterator();
while(iter1.hashNext()){
Attribute el = (Attribute)iter1.next();
el.getName();el.getValue();//获取属性名称和值
}
如果取所有,则用递归取:
public void getInfo(Element root){
Iterator<Element> iter = root.getElementIterator();
while(iter.hashNext()){
Element element = (Element)iter.next();
getInfo(element);//自己调用自己 递归方法
}
}
Document doc = reader.read(new File("class.xml"));
Element element = doc.getRootElement();//获取根元素
迭代器:取根元素下的子元素名称
Iterator<Element> iter = element.elementIterator();
while(iter.hashNext){
Element el = (Element)iter.next();
el.getName();//获取元素名称
el.getText();//获取元素值
}
//获取属性名称、值
Iterator<Attribute> iter1 = element.attributeIterator();
while(iter1.hashNext()){
Attribute el = (Attribute)iter1.next();
el.getName();el.getValue();//获取属性名称和值
}
如果取所有,则用递归取:
public void getInfo(Element root){
Iterator<Element> iter = root.getElementIterator();
while(iter.hashNext()){
Element element = (Element)iter.next();
getInfo(element);//自己调用自己 递归方法
}
}
展开全部
给个例子:
xml文件:
<?xml version="1.0" encoding="gb2312"?>
<LOGINDATE>
<loginDate>2010-06-07 21:37:33</loginDate>
<orgId>4</orgId>
<orgName>萧山店</orgName>
</LOGINDATE>
java代码:
public static Map ReadXml(){
File file = new File(path);
Map map=null;
try {
if (file.exists()) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
org.w3c.dom.Document doc=db.parse(new File(path));
NodeList nodeList = doc.getElementsByTagName("LOGINDATE");//跟节点
Node fatherNode = nodeList.item(0);
NodeList childNodes = fatherNode.getChildNodes();
map=new HashMap();
for (int j = 0; j < childNodes.getLength(); j++) {
Node childNode = childNodes.item(j);
// 如果这个节点属于Element ,再进行取值
if (childNode instanceof org.w3c.dom.Element) {
if(!childNode.getFirstChild().getNodeValue().equals(null)){
map.put(childNode.getNodeName(), childNode.getFirstChild().getNodeValue());
System.out.println(childNode.getNodeName()
+ ":" + childNode.getFirstChild().getNodeValue());
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
xml文件:
<?xml version="1.0" encoding="gb2312"?>
<LOGINDATE>
<loginDate>2010-06-07 21:37:33</loginDate>
<orgId>4</orgId>
<orgName>萧山店</orgName>
</LOGINDATE>
java代码:
public static Map ReadXml(){
File file = new File(path);
Map map=null;
try {
if (file.exists()) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
org.w3c.dom.Document doc=db.parse(new File(path));
NodeList nodeList = doc.getElementsByTagName("LOGINDATE");//跟节点
Node fatherNode = nodeList.item(0);
NodeList childNodes = fatherNode.getChildNodes();
map=new HashMap();
for (int j = 0; j < childNodes.getLength(); j++) {
Node childNode = childNodes.item(j);
// 如果这个节点属于Element ,再进行取值
if (childNode instanceof org.w3c.dom.Element) {
if(!childNode.getFirstChild().getNodeValue().equals(null)){
map.put(childNode.getNodeName(), childNode.getFirstChild().getNodeValue());
System.out.println(childNode.getNodeName()
+ ":" + childNode.getFirstChild().getNodeValue());
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询