java - 如何提取 XML 的某些部分但有一些不相似之处

标签 java xml

所以我有这个带有 block 的 XML 文件,我需要提取其中一些价格高于 10、发布日期高于 2005 年的 block 。

我刚刚使用 getElementById 拉取了所有 block 。我想我可以在这里使用 if else...

public class Helper 
{

    public static void parseXML()
    {
        try {
            File inputFile = new File("XML.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(inputFile);
            doc.getDocumentElement().normalize();
            System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
            NodeList nList = doc.getElementsByTagName("book");
            System.out.println("----------------------------");

            for (int temp = 0; temp < nList.getLength(); temp++) {
                Node nNode = nList.item(temp);
                System.out.println("\nCurrent Element :" + nNode.getNodeName());  

                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;
                System.out.println("First Name : " 
                + eElement
                .getElementsByTagName("author")
                .item(0)
                .getTextContent());
                System.out.println("Title : " 
                + eElement
                .getElementsByTagName("title")
                .item(0)
                .getTextContent());
                System.out.println("Genre : " 
                + eElement
                .getElementsByTagName("genre")
                .item(0)
                .getTextContent());
                System.out.println("Price : " 
                + eElement
                .getElementsByTagName("price")
                .item(0)
                .getTextContent());
                System.out.println("Publish date : " 
                + eElement
                .getElementsByTagName("publish_date")
                .item(0)
                .getTextContent());
                System.out.println("Description : " 
                + eElement
                .getElementsByTagName("description")
                .item(0)
                .getTextContent());
                }
            }
}
            catch (SAXException ex) { } 
            catch (IOException ex) { } 
            catch (ParserConfigurationException ex) { }
    }
}
<?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>

我预计输出只是价格较高的区 block 10。 我尝试将字符串(价格的 getElementById)解析为整数并声明它,然后将其放入 if 像这样(if(a>10)),代码中没有错误,输出中却有错误。它说 Integer 不好。

最佳答案

好吧,如果这是家庭作业,它可能会对解决方案有一些限制。然而,查询xml文档的正确工具是XPath 。它是一种灵活的查询语言,解析器是 JDK 的一部分。

这是一个解决作业的示例

try {
    Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
            Files.newInputStream(Paths.get("C:/temp/xx.xml")));
    XPath xPath = XPathFactory.newInstance().newXPath();
    // find elements in path /catalog/book where price element > 10.00 and first 4 letters of element publish_date are greater than '2005'
    String expression = "/catalog/book[price > 10.00 and substring(publish_date,1,4) > '2005']";
    NodeList list = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);

    for (int i = 0 ; i < list.getLength() ; i++) {
        Node node = list.item(i);
        String id = node.getAttributes().getNamedItem("id").getTextContent();
        System.out.println(id);
    }
} catch (Exception e) {
    e.printStackTrace();
}

输出

bk110
bk111
bk112

关于java - 如何提取 XML 的某些部分但有一些不相似之处,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56585934/

相关文章:

mysql - 将 MySQL XML 输出字段名转换为可用的标签名称

python - 如何向在Python中使用lxml中的xpath找到的标签添加属性?

java - 如何修复此未绑定(bind)前缀错误?不知道我错过了什么

Java Mail writeTo 和读回给出 Illegal semicolon 错误

java - 如何在java中的arraylist中获取随时间升序的日期

android 在按下选择器 xml 时更改 ImageButton 的图像

java - 使用 java 创建 xml

java - C++(可能还有 Java)如何为同步锁定对象?

java - 使用重复平方计算幂的递归方法

java - Java 文件中 import 语句的含义