2013-02-20 84 views
3

有什麼更好的方式來解析這樣的xml:XPATH更好的方式來解析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 /代碼和ActivityTerritory /代碼

回答

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)); 
    } 
} 
}