2017-02-17 97 views
0

我需要一個簡單的示例如何使用Java DOM解析器和Apache JxPath解析XML文件。我意識到使用DOM解析器技術,但現在我試圖將JxPath整合到我的源代碼中。如何使用JxPath和DOM解析器解析XML文件

我已經在網絡中搜索,但我找不到工作示例。

爲了測試我有這樣的XML:

<?xml version="1.0" encoding="UTF-8"?> 
<catalog> 
    <cd gender="male"> 
    <title>Empire Burlesque</title> 
    <artist>Bob Dylan</artist> 
    <country>USA</country> 
    <company>Columbia</company> 
    <price>10.90</price> 
    <year>1985</year> 
    </cd> 
    <cd gender="male"> 
    <title>Hide your heart</title> 
    <artist>Bonnie Tyler</artist> 
    <country>UK</country> 
    <company>CBS Records</company> 
    <price>9.90</price> 
    <year>1988</year> 
    </cd> 
</catalog> 

Java代碼:

File file = new File("Files/catalog.xml"); 
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); 
try 
{ 
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); 
    Document doc = docBuilder.parse(file); 

    NodeList nList = doc.getElementsByTagName("cd"); 
    for(int j = 0; j<nList.getLength(); j++){ 

     Element element = (Element)nList.item(j); 
     System.out.println(element.getElementsByTagName("artist").item(0).getTextContent() + "\n"); 

    } 

} 
catch (ParserConfigurationException | SAXException | IOException e) 
{ 
    e.printStackTrace(); 
} 

我已閱讀類集裝箱,DocumentContainer和JXPathContext,但 我將不勝感激一些幫助或特定工作示例的Web源代碼。

回答

2

下面是與您的工作類似的示例。

File file = new File("Files/catalog.xml"); 
DocumentContainer dc = new DocumentContainer(file.toURI().toURL()); 
JXPathContext ctx = JXPathContext.newContext(dc); 
Iterator iter = ctx.iterate("//cd/artist"); 
//In this case, following API will return DOM object 
//Iterator iter = ctx.selectNodes("//cd/artist").iterator(); 
while (iter.hasNext()) { 
    System.out.println(iter.next());//object type is String 
} 
1

您確定JXPath是您需要的嗎? JXPath是一個Apache庫,用於在不一定需要XML的東西上使用XPath表達式,如Java對象樹或地圖。如果你已經從你的XML中創建了一個DOM,你可以使用它的默認Java XPath實現。請看這裏:https://docs.oracle.com/javase/7/docs/api/javax/xml/xpath/XPathFactory.html。事實上,會的JXPath對着幹,你在這裏,因爲DOM有一個像ElementText與像元素名的元數據對象的樹。因此,不要像//cd/artist這樣的表達式得到類似​​的東西。

現在,如果您出於某種原因必須能夠在不知道事先知道的情況下,對可能是Java對象樹或DOM的某些東西使用XPath表達式,那麼JXPath將是一個很好的使用 - 案例在beckyang在他們的答案中描述的方法:使用的JXPath DocumentContainer訪問文檔。

0

是的,我從幾個小時閱讀和的JXPath我現在明白這個API的邏輯的答案

許多感謝。這裏是一些代碼形式我

public class Main { 
    private final static Logger log = Logger.getLogger(Main.class); 

    public static void main(String[] args) { 

     PropertyConfigurator.configure("log4j.properties"); 

     File file = new File("students.xml"); 
     DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); 
     try 
     { 
      DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); 
      Document doc = docBuilder.parse(file); 

      NodeList nList = doc.getElementsByTagName("cd"); 
      for(int j = 0; j<nList.getLength(); j++){ 

       Element element = (Element)nList.item(j); 

       JXPathContext context = JXPathContext.newContext(element); 
       log.debug(context.getValue("company/address/city[2]")); 

      } 

     } 
     catch (ParserConfigurationException | SAXException | IOException e) 
     { 
      e.printStackTrace(); 
     } 

    }