簡體   English   中英

如何在 Java 中使用 xPath 正確解析此 XML 文件?

[英]How can I properly parse this XML file with xPath in Java?

這是我需要解析的 XML 文件:

<?xml version="1.0"?>
<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>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2002-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>
   <book id="bk103">
      <author>Corets, Eva</author>
      <title>Maeve Ascendant</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-11-17</publish_date>
      <description>After the collapse of a nanotechnology 
      society in England, the young survivors lay the 
      foundation for a new society.</description>
   </book>
   <book id="bk104">
      <author>Corets, Eva</author>
      <title>Oberon's Legacy</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2001-03-10</publish_date>
      <description>In post-apocalypse England, the mysterious 
      agent known only as Oberon helps to create a new life 
      for the inhabitants of London. Sequel to Maeve 
      Ascendant.</description>
   </book>
   <book id="bk105">
      <author>Corets, Eva</author>
      <title>The Sundered Grail</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2001-09-10</publish_date>
      <description>The two daughters of Maeve, half-sisters, 
      battle one another for control of England. Sequel to 
      Oberon's Legacy.</description>
   </book>
   <book id="bk106">
      <author>Randall, Cynthia</author>
      <title>Lover Birds</title>
      <genre>Romance</genre>
      <price>4.95</price>
      <publish_date>2003-09-02</publish_date>
      <description>When Carla meets Paul at an ornithology 
      conference, tempers fly as feathers get ruffled.</description>
   </book>
   <book id="bk107">
      <author>Thurman, Paula</author>
      <title>Splish Splash</title>
      <genre>Romance</genre>
      <price>4.95</price>
      <publish_date>2004-11-02</publish_date>
      <description>A deep sea diver finds true love twenty 
      thousand leagues beneath the sea.</description>
   </book>
   <book id="bk108">
      <author>Knorr, Stefan</author>
      <title>Creepy Crawlies</title>
      <genre>Horror</genre>
      <price>4.95</price>
      <publish_date>2005-12-06</publish_date>
      <description>An anthology of horror stories about roaches,
      centipedes, scorpions  and other insects.</description>
   </book>
   <book id="bk109">
      <author>Kress, Peter</author>
      <title>Paradox Lost</title>
      <genre>Science Fiction</genre>
      <price>6.95</price>
      <publish_date>2006-11-02</publish_date>
      <description>After an inadvertant trip through a Heisenberg
      Uncertainty Device, James Salway discovers the problems 
      of being quantum.</description>
   </book>
   <book id="bk110">
      <author>O'Brien, Tim</author>
      <title>Microsoft .NET: The Programming Bible</title>
      <genre>Computer</genre>
      <price>36.95</price>
      <publish_date>2006-12-09</publish_date>
      <description>Microsoft's .NET initiative is explored in 
      detail in this deep programmer's reference.</description>
   </book>
   <book id="bk111">
      <author>O'Brien, Tim</author>
      <title>MSXML3: A Comprehensive Guide</title>
      <genre>Computer</genre>
      <price>36.95</price>
      <publish_date>2007-12-01</publish_date>
      <description>The Microsoft MSXML3 parser is covered in 
      detail, with attention to XML DOM interfaces, XSLT processing, 
      SAX and more.</description>
   </book>
   <book id="bk112">
      <author>Galos, Mike</author>
      <title>Visual Studio 7: A Comprehensive Guide</title>
      <genre>Computer</genre>
      <price>49.95</price>
      <publish_date>2008-04-16</publish_date>
      <description>Microsoft Visual Studio 7 is explored in depth,
      looking at how Visual Basic, Visual C++, C#, and ASP+ are 
      integrated into a comprehensive development 
      environment.</description>
   </book>
</catalog>

我想顯示出版日期在 2005 年之后的每本書及其信息。價格大於 10。這是我的 Java 代碼:

package xml;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class Main {

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

        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();

        XPathExpression xPathExpression = xPath.compile("catalog/book[publish_date>2005]/price | catalog/book[price>10]/price");

        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.println("Info: " + nodeList.item(i).getFirstChild().getNodeValue());
        }

    }

}

我應該如何使用此查詢正確執行此操作?

添加 Lorem ipsum 以便可以發布問題:Lorem Ipsum 只是印刷和排版行業的虛擬文本。 自 1500 年代以來,Lorem Ipsum 一直是行業的標准虛擬文本,當時一位不知名的印刷商采用了一種類型的廚房並將其加擾以制作一本類型樣本書。 它不僅經歷了五個世紀,而且經歷了電子排版的飛躍,基本保持不變。 它在 1960 年代隨着包含 Lorem Ipsum 段落的 Letraset 表的發布而流行起來,最近還隨着 Aldus PageMaker 等桌面出版軟件(包括 Lorem Ipsum 的版本)而普及。

你幾乎是對的。 問題在於,根據規范,數字比較( <> )需要將每個操作數隱式轉換為數字。 如果節點的文本內容完全由 ASCII 數字組成,並且帶有可選的前導減號、可選的句點和可選的周圍空格,則它只是一個有效數字。

2002-12-16這樣的日期顯然不符合條件。 但是,您可以使用substring-before將其轉換為可以隱式轉換為數字的字符串:

XPathExpression xPathExpression = xPath.compile(
    "catalog/book[substring-before(publish_date,'-')>2005 and price>10]/price");

利用 XML 日期格式並在那里進行字符串比較,並結合您的條件

/catalog/book[(publish_date > '2005') and (number(price) > 10)]

因此

XPathExpression xPathExpression = xPath.compile("/catalog/book[(publish_date > '2005') and (number(price) > 10)]");
NodeList bookNodes = (NodeList)xPathExpression.evaluate(inputSource, XPathConstants.NODESET);
for (int i = 0; i < bookNodes.getLength(); i++) {
    Element bookElement = bookNodes.item(i);
    System.out.println("Author: " + bookElement.getElementsByTagName("author").item(0).getNodeValue());
}

您需要添加剩余的必要標簽。 此外,如果您預訂的元素可能並非全部包含所有預期的節點,則需要檢查 getElementsByTagName() 返回的集合

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM