首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何提取XML的某些部分,而不是具有某些不相似性的部分

如何提取XML的某些部分,而不是具有某些不相似性的部分
EN

Stack Overflow用户
提问于 2019-06-14 01:41:47
回答 1查看 32关注 0票数 0

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

我刚用getElementById把所有的模块都拉出来了。我在想,如果不是这样的话..。

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>

我希望输出只是价格更高为10的块。我试图将字符串(价格为getElementById)解析为整数并声明它,然后将其放入,如果像这样( if (a>10)),代码中没有错误,但输出是错误的。它说Integer是不好的。

EN

回答 1

Stack Overflow用户

发布于 2019-06-14 03:13:13

嗯,如果这是家庭作业,它可能会对解决方案有一些限制。但是,查询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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56585934

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档