xml文件解析问题
这种方法比较麻烦,一级一级的节点弄得我都晕了,最后还是达不到目的,请高手指点。详细见下面:
xml文件
<?xml version= "1.0 " encoding= "UTF-8 "?>
<conditions>
<singleCondition>
<table name= "family " value= "f ">
<condition> writeId </condition>
<condition> familyType </condition>
</table>
<table name= "familyhouse " value= "fh ">
<condition> houseType </condition>
</table>
</singleCondition>
<dualCondition>
<table name= "family " value= "f ">
<condition> familyMoney </condition>
</table>
<table name= "familyhouse " value= "fh ">
<condition> houseUseArea </condition>
</table>
</dualCondition>
</conditions>
希望结果:
map1(f,writeId),
map1(f,familyType),
map1(fh,houseType).
map2(f,familyMoney),
map2(fh,houseUseArea)
我的代码:
package com.app.project.util;
/**
* <p> Title: XML工具类 </p>
* <p> Description: </p>
* <p> Copyright: Copyright (c) 2007 </p>
* <p> Company: JFZX </p>
* <p> CreateDate: Feb 6, 2007 </p>
* @author ZhongZuo
*/
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
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 XMLUtils {
/**
* 解析表单提交查询条件分类
* 说明:调用ConditionList.xml
*
*/
public List getConditionList(){
List list = new ArrayList();
List singleConditionList = new ArrayList();
List dualConditionList = new ArrayList();
DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();
try {
DocumentBuilder dombuilder=domfac.newDocumentBuilder();
InputStream is=new FileInputStream( "C:\\ConditionList.xml ");
Document doc=dombuilder.parse(is);
Element root=doc.getDocumentElement();
NodeList conditions=root.getChildNodes();
if(conditions!=null){
for(int i=0;i <conditions.getLength();i++){
Node childNode=conditions.item(i);
if(childNode.getNodeType()==Node.ELEMENT_NODE){
if(childNode.getNodeName().equals( "singleCondition ")){
NodeList childNodeList = childNode.getChildNodes();
for(int j=0;j <childNodeList.getLength();j++){
Node childNode2 = childNodeList.item(i);
if(childNode2.getNodeType()==Node.ELEMENT_NODE){
String tableName = childNode2.getAttributes().getNamedItem( "name ").getNodeValue();
System.out.println( "#: "+tableName);
for(Node node=childNode2.getFirstChild();node!=null;node=node.getNextSibling()){
if(node.getNodeType()==Node.ELEMENT_NODE){
if(node.getNodeName().equals( "condition ")){
String condition=node.getFirstChild().getNodeValue();
System.out.println( "@ "+condition);
singleConditionList.add(condition);
}
}
}
}
}
}
if(childNode.getNodeName().equals( "dualCondition ")){
for(Node node=childNode.getFirstChild();node!=null;node=node.getNextSibling()){
if(node.getNodeType()==Node.ELEMENT_NODE){
if(node.getNodeName().equals( "condition ")){
String condition=node.getFirstChild().getNodeValue();
//System.out.println( "# "+condition);
dualConditionList.add(condition);
}
}
}
}
}
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
list.add(singleConditionList);
list.add(dualConditionList);
return list;
}
public static void main(String[] args) {
XMLUtils xmlUtils = new XMLUtils();
List list = xmlUtils.getConditionList();
//System.out.println(list);
}
}
[解决办法]
不知道有xpath这东西么
[解决办法]
楼上的说得好啊。
不用xpath,用xml干嘛呢,直接用长字符串,效果一样,呵呵