读书人

用xpath获取xml中的子节点有关问题

发布时间: 2012-01-20 18:53:53 作者: rapoo

用xpath获取xml中的子节点问题
Xml代码
<?xml version="1.0" encoding="utf-8"?>
<ROOT>
<APP_SYSTEM_ID>1000001</APP_SYSTEM_ID >
<APP_SYSTEM>iteye</APP_SYSTEM >
<ORGAN operatparam="add">


<ORGAN_INFO>
<ORGAN_ID>101</ORGAN_ID >
<PARENT_ID>1</PARENT_ID >
<ORGANNAME>湖南</ORGANNAME >
<IS_LEAF>02</IS_LEAF >
<ACCOUNT_ID> </ACCOUNT_ID >
<PASSWORD> </PASSWORD >
</ORGAN_INFO >

<ORGAN_INFO>
<ORGAN_ID>1</ORGAN_ID >
<PARENT_ID>-1</PARENT_ID >
<ORGANNAME>中国</ORGANNAME >
<IS_LEAF>02</IS_LEAF >
<ACCOUNT_ID> </ACCOUNT_ID >
<PASSWORD> </PASSWORD >
</ORGAN_INFO >

<ORGAN_INFO>
<ORGAN_ID>10101</ORGAN_ID >
<PARENT_ID>101</PARENT_ID >
<ORGANNAME>长沙市</ORGANNAME >
<IS_LEAF>02</IS_LEAF >
<ACCOUNT_ID> </ACCOUNT_ID >
<PASSWORD> </PASSWORD >
</ORGAN_INFO >

<ORGAN_INFO>
<ORGAN_ID>1010101</ORGAN_ID >
<PARENT_ID>10101</PARENT_ID >
<ORGANNAME>烟草局</ORGANNAME >
<IS_LEAF>02</IS_LEAF >
<ACCOUNT_ID> </ACCOUNT_ID >
<PASSWORD> </PASSWORD >
</ORGAN_INFO >

<ORGAN_INFO>
<ORGAN_ID>1010102</ORGAN_ID >
<PARENT_ID>10101</PARENT_ID >
<ORGANNAME>农业局</ORGANNAME >
<IS_LEAF>02</IS_LEAF >
<ACCOUNT_ID> </ACCOUNT_ID >
<PASSWORD> </PASSWORD >
</ORGAN_INFO >

<ORGAN_INFO>
<ORGAN_ID>1010101001</ORGAN_ID >
<PARENT_ID>1010102</PARENT_ID >
<ORGANNAME>李四</ORGANNAME >
<IS_LEAF>01</IS_LEAF >
<ACCOUNT_ID>lisi</ACCOUNT_ID >
<PASSWORD>lisi</PASSWORD >
</ORGAN_INFO >

<ORGAN_INFO>
<ORGAN_ID>1010101001</ORGAN_ID >
<PARENT_ID>1010101</PARENT_ID >
<ORGANNAME>李四</ORGANNAME >
<IS_LEAF>01</IS_LEAF >
<ACCOUNT_ID>lisi</ACCOUNT_ID >
<PASSWORD>lisi</PASSWORD >
</ORGAN_INFO >

</ORGAN >


</ROOT >
上面是我的xml文件内容。

Java代码
public class XmlParseHelper {
private DocumentBuilder builder;
private Document document;
private Element root;

public XmlParseHelper(InputStream is) throws IOException {

this.document = null;
this.builder = null;

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(false);
try {
this.builder = factory.newDocumentBuilder();

this.builder.setEntityResolver(new EntityResolver() {


public InputSource resolveEntity(String publicId,
String systemId) throws SAXException, IOException {
return new InputSource(new StringReader(""));
}

});

this.document = this.builder.parse(is);
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
} catch (SAXException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public XmlParseHelper(String xml) throws UnsupportedEncodingException,
IOException {
this(new ByteArrayInputStream(xml.getBytes("gbk")));
}

private NodeList findNodes(String xql) throws XPathExpressionException {
XPathFactory xfactory = XPathFactory.newInstance();
XPath xpath = xfactory.newXPath();
XPathExpression expr = xpath.compile(xql);

Object result = expr.evaluate(this.document, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
return nodes;
}

private Node findNode(String xql) throws XPathExpressionException {
NodeList nodes = findNodes(xql);
if ((nodes == null) || (nodes.getLength() == 0))
return null;
return nodes.item(0);
}

private Node getChildNode(Node node, String xql)
throws XPathExpressionException {
XPathFactory xfactory = XPathFactory.newInstance();
XPath xpath = xfactory.newXPath();
XPathExpression expr = xpath.compile(xql);
Object obj = expr.evaluate(node, XPathConstants.NODE);
Node result = (Node) obj;
return result;

}

private String getNodeValue(String xql) throws XPathExpressionException {
Node node = findNode(xql);
return node.getTextContent();
}

public String getAppName() {
String appName = "";
try {
appName = getNodeValue("ROOT/APP_SYSTEM");
} catch (XPathExpressionException e) {
e.printStackTrace();
}
return appName;
}

public String getAppId() {
String appId = "";
try {
appId = getNodeValue("/ROOT/APP_SYSTEM_ID");
} catch (XPathExpressionException e) {
e.printStackTrace();
}
return appId;
}

public List<MultNodeInfo> getUpdateList() {
return getListByOperatparam("update");
}

public List<MultNodeInfo> getAddList() {


return getListByOperatparam("add");
}

public List<MultNodeInfo> getDeleteList() {
return getListByOperatparam("delete");
}

private List<MultNodeInfo> getListByOperatparam(String operatparam) {
NodeList nodeList = null;
String xql = "/ROOT/ORGAN[@operatparam=\"" + operatparam + "\"]/ORGAN_INFO";
try {
nodeList = findNodes(xql);

} catch (XPathExpressionException e) {
e.printStackTrace();
}
if (null == nodeList) {
return null;
}
List<MultNodeInfo> result = new ArrayList<MultNodeInfo>();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
String organId;
String parentOrganId;
String organName;
String isLeaf;
String accountId;
String password;
MultNodeInfo multNodeInfo = new MultNodeInfo();
try {
organId = getChildNode(node, "/ROOT/ORGAN/ORGAN_INFO/ORGAN_ID").getTextContent();
parentOrganId = getChildNode(node, "//PARENT_ID")
.getTextContent();
organName = getChildNode(node, "//ORGANNAME").getTextContent();
isLeaf = getChildNode(node, "//IS_LEAF").getTextContent();
accountId = getChildNode(node, "//ACCOUNT_ID").getTextContent();
password = getChildNode(node, "//PASSWORD").getTextContent();
multNodeInfo.setOrganId(organId);
multNodeInfo.setParentOrganId(parentOrganId);
multNodeInfo.setOrganName(organName);
multNodeInfo.setNodeType(isLeaf);
multNodeInfo.setAccountId(accountId);
multNodeInfo.setPassword(password);
result.add(multNodeInfo);
} catch (DOMException e) {
e.printStackTrace();
} catch (XPathExpressionException e) {
e.printStackTrace();
}
}

return result;
}
public static void main(String args[]) throws XPathExpressionException, TransformerException{
try {
InputStream is = new FileInputStream(new File("E:\\批量正常数据1.xml"));
XmlParseHelper xh= new XmlParseHelper(is);
String nodeXPathExpr = "/ROOT/ORGAN[@operatparam=\"" + "add" + "\"]/ORGAN_INFO/";
List<MultNodeInfo> nl = xh.getAddList();
for(MultNodeInfo node:nl){

System.out.println(node.getOrganId()+"------------");;
}

} catch (IOException e) {


// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

输出结果是:
Java代码
101-----------------
101-----------------
101-----------------
101-----------------
101-----------------
101-----------------
101-----------------
而我期望的结果是:

Java代码
101------------------
1-----------------
10101-----------------
1010101-----------------
1010102-----------------
1010101001-----------------
1010101001-----------------

如果想要得到我想要的结果,应该怎么做啊,请教各位了

[解决办法]
XMLSpy 这个工具 很好用啊 直接就可以获得Xpath
[解决办法]
怎么有两个<ORGAN_ID>1010101001</ORGAN_ID >

XML code
<ORGAN_INFO>   <ORGAN_ID>1010101001</ORGAN_ID >   <PARENT_ID>1010102</PARENT_ID >   <ORGANNAME>李四</ORGANNAME >   <IS_LEAF>01</IS_LEAF >   <ACCOUNT_ID>lisi</ACCOUNT_ID >   <PASSWORD>lisi</PASSWORD >   </ORGAN_INFO >      <ORGAN_INFO>   <ORGAN_ID>1010101001</ORGAN_ID >   <PARENT_ID>1010101</PARENT_ID >   <ORGANNAME>李四</ORGANNAME >   <IS_LEAF>01</IS_LEAF >   <ACCOUNT_ID>lisi</ACCOUNT_ID >   <PASSWORD>lisi</PASSWORD >   </ORGAN_INFO > 

读书人网 >J2EE开发

热点推荐