2017-05-18 152 views
0

僅當描述爲T1並且使用xPath列表中的每個作業描述值(Mecánica,Guarnecidos,...)時,我需要獲取值28.42。在Java中使用xPath獲取節點

<priceByHourList> 
<priceByHour locked="false"> 
    <value> 
     <job> 
      <jobId>4</jobId> 
      <description> 
       <id>4</id> 
       <value>Mecánica</value> 
      </description> 
     </job> 
     <technician> 
      <technicianId>1</technicianId> 
      <description>T1</description> 
     </technician> 
     <value>28.42</value> 
    </value> 
</priceByHour> 
<priceByHour locked="false"> 
    <value> 
     <job> 
      <jobId>4</jobId> 
      <description> 
       <id>4</id> 
       <value>Mecánica</value> 
      </description> 
     </job> 
     <technician> 
      <technicianId>2</technicianId> 
      <description>T2</description> 
     </technician> 
     <value>28.42</value> 
    </value> 
</priceByHour> 
<priceByHour locked="false"> 
    <value> 
     <job> 
      <jobId>4</jobId> 
      <description> 
       <id>4</id> 
       <value>Mecánica</value> 
      </description> 
     </job> 
     <technician> 
      <technicianId>3</technicianId> 
      <description>T3</description> 
     </technician> 
     <value>28.42</value> 
    </value> 
</priceByHour> 
<priceByHour locked="false"> 
    <value> 
     <job> 
      <jobId>1</jobId> 
      <description> 
       <id>1</id> 
       <value>Electricidad</value> 
      </description> 
     </job> 
     <technician> 
      <technicianId>2</technicianId> 
      <description>T2</description> 
     </technician> 
     <value>28.42</value> 
    </value> 
</priceByHour> 
<priceByHour locked="false"> 
    <value> 
     <job> 
      <jobId>1</jobId> 
      <description> 
       <id>1</id> 
       <value>Electricidad</value> 
      </description> 
     </job> 
     <technician> 
      <technicianId>3</technicianId> 
      <description>T3</description> 
     </technician> 
     <value>28.42</value> 
    </value> 
</priceByHour> 
<priceByHour locked="false"> 
    <value> 
     <job> 
      <jobId>5</jobId> 
      <description> 
       <id>5</id> 
       <value>Guarnecidos</value> 
      </description> 
     </job> 
     <technician> 
      <technicianId>1</technicianId> 
      <description>T1</description> 
     </technician> 
     <value>28.42</value> 
    </value> 
</priceByHour> 

而我試圖讓列表中,但該節點是一樣的。

 for (int i = 0; i < nodes.getLength(); i++) { 
      Element pieza = (Element) nodes.item(i);    
      System.out.println(pieza.getNodeName());} 
+0

這是JSoup? –

+0

是否嘗試過'// value [.// description =「T1」]/value/text()'? – Andersson

回答

0

因爲你需要兩個細節:job/descriptionvalue你需要兩個XPath表達式。你可以這樣做如下:

public class PriceByHour 
{ 

    private static class PriceDetails 
    { 
     String jobDesc; 
     String value; 
    } 

    public static void main(String... args) 
    { 
     try 
     { 
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder db = dbf.newDocumentBuilder(); 

      Document doc = db.parse(new File(".//pricebyhour.xml")); 
      XPathFactory xPathFactory = XPathFactory.newInstance(); 
      XPath xpath = xPathFactory.newXPath(); 

      List<PriceDetails> priceDetailsList = new ArrayList<PriceDetails>(); 

      XPathExpression jobExpr = xpath.compile("//priceByHour/value[technician[./description='T1']]/job/description/value"); 
      XPathExpression valueExpr = xpath.compile("//priceByHour/value[technician[./description='T1']]/value"); 

      Object exprEval = jobExpr.evaluate(doc, XPathConstants.NODESET); 
      NodeList jobNodes = null; 
      NodeList valueNodes = null; 
      if (exprEval != null && exprEval instanceof NodeList) 
      { 
       jobNodes = (NodeList)exprEval; 
      } 

      exprEval = valueExpr.evaluate(doc, XPathConstants.NODESET); 
      if (exprEval != null && exprEval instanceof NodeList) 
      { 
       valueNodes = (NodeList)exprEval; 
      } 

      if (jobNodes != null && valueNodes != null && jobNodes.getLength() == valueNodes.getLength()) 
      { 
       for (int i = 0 ; i < jobNodes.getLength(); i++) 
       { 
        Node jobNode = jobNodes.item(i); 
        Node valueNode = valueNodes.item(i); 
        PriceDetails priceDetails = new PriceDetails(); 
        priceDetails.jobDesc = jobNode.getTextContent(); 
        priceDetails.value = valueNode.getTextContent(); 
        priceDetailsList.add(priceDetails); 

       } 
      } 

      for (PriceDetails priceDetails : priceDetailsList) 
      { 
       System.out.println("Job description is : " + priceDetails.jobDesc + ", " + "Value is : " + priceDetails.value); 
      } 

     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 

    } 
} 

將您的文件中的XML稱爲pricebyhour.xml並運行該代碼。

注意,第一個xpath取得作業描述,第二個xpath取得值。 作業說明和值的數量應該匹配,並且實際上可以構建一個類,其中包含兩個值作業說明和值,如類PriceDetails所示。

輸出是:

Job description is : Mecánica, Value is : 28.42 
Job description is : Guarnecidos, Value is : 28.42