2016-05-30 2 views
1

JavaでDOMを使用して解析するXMLページがあります。 XPathを使用してクエリを実行すると、たとえばprice <10またはprice >20のような結果が得られます。しかし、私は日付で比較しようとすると何の結果も得られません。 NetBeansは成功していると言いますが、結果は得られません。日付をJavaのXpathと比較する

このコードは、XMLページからです:

<!?xml version="1.0" encoding="UTF-8"?><catalog ><book id="bk101"><author>Gambardella, Matthew</author><title>XML Developer's Guide</title><genre>Computer</genre> 
<price>44.95</price> 
<publish_date>2000-10-01</publish_date> 
<description>An in-depth look at creating applications 
with XML.</description> 
</book> 
<catalog> 

これは私のJavaコードです:私がする必要がどのような

public class Main { 
    public static void main(String[] args) throws XPathExpressionException, FileNotFoundException { 

     XPathFactory factory = XPathFactory.newInstance(); 
     XPath path = factory.newXPath(); 
     XPathExpression xPathExpression=path.compile("//book[price >10]/* "); 
     //| //book[price>10]/* 

     File xmlDocument =new File("books.xml"); 
     InputSource inputSource = new InputSource(new FileInputStream(xmlDocument)); 

     Object result = xPathExpression.evaluate(inputSource,XPathConstants.NODESET); 

     NodeList nodeList = (NodeList)result; 

     for (int i = 0; i < nodeList.getLength(); i++) { 
      System.out.print(nodeList.item(i).getNodeName()+" "); 
      System.out.print(nodeList.item(i).getFirstChild().getNodeValue()); 
      System.out.print("\n"); 
     } 
    } 
} 

は、事前定義された日付にpublish_dateを比較することです。 「//ブック[公開日> 2000-01-01]/*この

答えて

2

のXpath(少なくとも1.0)のような 何かが日付を比較することはできませんが、機能を翻訳使用して、正しい順序で整数に日付を変えることができます。

//book[translate(publish_date,'-','') > 20000101] 
+0

これは私の問題を解決しました。ありがとうございました、遅く返事を申し訳ありません。 – ImRaphael

+0

幸いです。 – splash58