2013-12-13 2 views
0

이것이 많은 질문과 관련되어 있음을 알고 있지만 제 XML 레이아웃을 사용한 예제를 찾을 수 없습니다.자바에서 Xpath를 만들어 다른 속성 값을 기반으로 속성 값을 선택하십시오.

<product name="ANALYTICS" count="24" occurred_at_time="1386648300000"> 
    <user_type name="Unknown" count="24" occurred_at_time="1386648300000"> 
     <session_source name="Web" count="24" occurred_at_time="1386648300000"/> 
    </user_type> 
</product> 
<product name="CARSWELL" count="492" occurred_at_time="1386651900000"> 
    <user_type name="External" count="492" occurred_at_time="1386651900000"> 
     <session_source name="Web" count="492" occurred_at_time="1386651900000"/> 
    </user_type> 
    <user_type name="Internal" count="19" occurred_at_time="1386622200000"> 
     <session_source name="UNKNOWN" count="12" occurred_at_time="1386624300000" /> 
     <session_source name="Web" count="10" occurred_at_time="1386604200000" /> 
    </user_type> 
</product> 

xml 내에서 관심있는 제품을 지정하고 올바른 표현식을 만드는 데 어려움을 겪고있는 속성 파일을 반복합니다.

for (int j = 0; j < Products.length; j++) { 
    XPathFactory xPathfactory = XPathFactory.newInstance(); 
    XPath xpath = xPathfactory.newXPath(); 

    try { 
      XPathExpression expr = xpath.compile("product[@name=" + Products[j] + "]attribute::count"); 
      CompareNextWeb = expr.evaluate(doc); 
      System.out.println(CompareNextWeb); 

    } catch (XPathExpressionException ex) { 
      Logger.getLogger(NextReport.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 

그래서 나는 수를 선택할 수 있도록하려면, 미래, occured_at_time에서,이 표현과 함께 "[+ 제품 [J 제품 이름 = @"] + "] 속성 :: 횟수". 정말로 상황을 고려해 볼 때 이것이 가능한지 알아야합니다. 그렇지 않으면 다른 방식으로 접근해야합니다.

답변

1

xpath.compile("product[@name='" + Products[j] + "']/attribute::count"); 또는 xpath.compile("product[@name='" + Products[j] + "']/@count");을 원합니다.

+0

완벽한, 감사합니다! – Chad

관련 문제