XPATH更好的解析xml的方法

时间:2013-02-20 04:57:53

标签: xpath xml-parsing

解析这样的xml的更好方法是什么:

<FindLicensesResponse xmlns="http://abc.com">
<FindLicensesResult>
    <Licensies>
        <ActivityLicense>
            <id>1</id>
            <DateIssue>2011-12-29T00:00:00</DateIssue>
            <ActivityType xmlns:s01="http://www.w3.org/2001/XMLSchema-instance" s01:type="ActivityType">
                <code>somecode1</code>
            </ActivityType>
            <ActivityTerritory xmlns:s02="http://www.w3.org/2001/XMLSchema-instance" s02:type="Territory">
                <code>somecode2</code>
            </ActivityTerritory>
            <ActivityLicenseAttachments />
        </ActivityLicense>
        <ActivityLicense>
            <id>2</id>
            <DateIssue>2011-12-21T00:00:00</DateIssue>
            <ActivityType xmlns:s01="http://www.w3.org/2001/XMLSchema-instance" s01:type="ActivityType">
                <code>somecode3</code>
            </ActivityType>
            <ActivityTerritory xmlns:s02="http://www.w3.org/2001/XMLSchema-instance" s02:type="Territory">
                <code>somecode4</code>
            </ActivityTerritory>
            <ActivityLicenseAttachments />
        </ActivityLicense>
    </Licensies>
</FindLicensesResult>

我需要从每个ActivityLicense获取值:id,DateIssue和内部ActivityType:代码和内部ActivityTerritory:代码。

现在我这样做:

CachedXPathAPI xpathAPI = new CachedXPathAPI();
Element nsctx = result.getSOAPPart().createElementNS(null, "nsctx");
nsctx.setAttributeNS("http://www.w3.org/2000/xmlns/","xmlns:el","http://abc.com");
NodeList activityLicenses = xpathAPI.selectNodeList(result.getSOAPPart(),"//el:ActivityLicense", nsctx);

for (int i = 0; i < activityLicenses.getLength(); i++) {
    Node id = xpathAPI.selectSingleNode(activityLicenses.item(i), "//el:id", nsctx);
    Node dateIssue = xpathAPI.selectSingleNode(activityLicenses.item(i), "//el:DateIssue",nsctx);

    System.out.println("id: " + id.getTextContent());
    System.out.println("dateIssue: " + dateIssue.getTextContent());
}

但我无法从ActivityType / code和ActivityTerritory / code

获取值

1 个答案:

答案 0 :(得分:1)

查看此解决方案

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

public class StringTest {

public static void main(String[] args) throws Exception {
    String xml = "";
    java.util.Scanner sc = new java.util.Scanner(new File("xml.xml"));
    while(sc.hasNextLine()){
        xml+=sc.nextLine();
    }
    javax.xml.parsers.DocumentBuilderFactory dbFactory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
    javax.xml.parsers.DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    InputStream is = new ByteArrayInputStream(xml.getBytes());
    org.w3c.dom.Document doc = dBuilder.parse(is);
    doc.getDocumentElement().normalize();
    XPath xpath = XPathFactory.newInstance().newXPath();

    org.w3c.dom.NodeList nodeList = doc.getElementsByTagName("ActivityLicense");
    for(int i=0;i<nodeList.getLength();i++){
        org.w3c.dom.Node node = nodeList.item(i);
        System.out.println(xpath.evaluate("ActivityTerritory/code/text()", node, XPathConstants.STRING));
    }
}
}