Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过XML记录进行复杂的搜索查询_Java_Xml_Xpath - Fatal编程技术网

Java 通过XML记录进行复杂的搜索查询

Java 通过XML记录进行复杂的搜索查询,java,xml,xpath,Java,Xml,Xpath,我有一个包含一个XML字符串字段的对象列表。我必须对该字段执行类似SQL的查询,并获得满足这些值的子列表。我正在尝试使用XPath 首先,我无法找出XPath字符串来实现这一点。第二,可能有更好的方法。我试着搜索,但答案并不能真正解决这个问题 详细信息 我有一个书单: List <Books> allBooks; 以下是详细信息xml字符串的示例: <book> <name>Harry Potter and the sorcerer's stone&l

我有一个包含一个XML字符串字段的对象列表。我必须对该字段执行类似SQL的查询,并获得满足这些值的子列表。我正在尝试使用XPath

首先,我无法找出XPath字符串来实现这一点。第二,可能有更好的方法。我试着搜索,但答案并不能真正解决这个问题

详细信息 我有一个书单:

List <Books> allBooks;
以下是详细信息xml字符串的示例:

<book>
   <name>Harry Potter and the sorcerer's stone</name>
   <author>J K Rowling</author>
   <genre>fantasy</genre>
   <keyword>wizard</keyword>
   <keyword>british</keyword>
   <keyword>hogwarts</keyword>
   <price>25</price>
</book>
我曾考虑将此数据放入数据库中以运行实际的查询,但由于该列表只包含几百条记录,因此连接、加载数据等开销不值得

有人知道如何通过XPath实现这一点吗?有更好的方法吗?

我们需要图书记录

//book
作者“J K罗琳”

类型是“幻想”

关键词是“巫师”或“霍格沃茨”


假设
书籍
是根目录

/Books/Book[(author = "J K Rowling") and (genre = "fantasy") and (keyword = "wizard" or keyword = "hogwarts")]

首先需要构建XPath查询。我建议参考之前的答案(hoaz有一个很好的列表)。然后您需要编写代码来编译查询并对其求值。例如:

  public List<Book> findBookInformation(List<Books> books)  
      throws ParserConfigurationException, SAXException, 
         IOException, XPathExpressionException {

    List<Book> foundBooks = new ArrayList<Book>(); // books matching criteria

    for (Book book : books) {
      DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
      domFactory.setNamespaceAware(true); // never forget this!
      DocumentBuilder builder = domFactory.newDocumentBuilder();
      Document doc = builder.parse(new InputSource(new StringReader(book.details))); // parse details XML into a Doc object

      XPathFactory factory = XPathFactory.newInstance();
      XPath xpath = factory.newXPath();
      //using one of the query examples
      XPathExpression expr = xpath.compile("/book[author = \"J K Rowling\" and genre = \"fantasy\" and (keyword = \"wizard\" or keyword = \"hogwarts\")]");

      Object result = expr.evaluate(doc, XPathConstants.NODESET); 
      NodeList nodes = (NodeList) result;
      if (null != nodes && nodes.getLength() > 0) {
        foundBooks.add(book); // add to your return list
      }
    }
    return foundBooks;
  }
公共列表查找图书信息(列表图书)
抛出ParserConfiguration异常、SAXException、,
IOException,XPathExpressionException{
List foundBooks=new ArrayList();//图书匹配条件
用于(书籍:书籍){
DocumentBuilderFactory domFactory=DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);//永远不要忘记这一点!
DocumentBuilder=domFactory.newDocumentBuilder();
Document doc=builder.parse(新的InputSource(新的StringReader(book.details));//将详细信息XML解析为doc对象
XPathFactory=XPathFactory.newInstance();
XPath=factory.newXPath();
//使用其中一个查询示例
XPathExpression expr=xpath.compile(“/book[author=\'jk Rowling\”和genre=\'fantasy\”和(关键字=\'wizard\'或关键字=\'hogwarts\”)”);
Object result=expr.evaluate(doc,XPathConstants.NODESET);
节点列表节点=(节点列表)结果;
if(null!=节点&&nodes.getLength()>0){
foundBooks.add(book);//添加到您的退货列表中
}
}
还书;
}

您可以扩展这样一个方法来接受查询参数来动态构建XPath查询,但这应该给您一个基本的想法。

也许我做错了什么,但这个查询似乎不起作用:-(.下面是我的Java代码中的相关行:XPathExpression expr=XPath.compile(“//book[author=\”J K Rowling\“和类型=\“幻想”和(关键字=\“向导”或关键字=\“霍格沃茨\”)/text()”;您希望找到什么
text()
呢?也许您需要
/name/text()
?我不认为
text()
能满足您的需要。这些表达式返回的是图书对象(以XML形式,请注意);您需要运行另一个XPath查询来提取其他元素(或使用其他DOM遍历方法)。@hoaz:My bad:-)。是的,它与名称配合得很好。谢谢。+1对字符串进行了逐步解释。@Bionic_希腊文:xpath编译字符串正是我要找的&正如你提到的,hoaz的一个字符串工作得很好。不过,感谢&+1,感谢下一步根据是否找到节点提取子列表!
//book[author = "J K Rowling"]
//book[author = "J K Rowling" and genre = "fantasy"]
//book[author = "J K Rowling" and genre = "fantasy" and (keyword = "wizard" or keyword = "hogwarts")]
/Books/Book[(author = "J K Rowling") and (genre = "fantasy") and (keyword = "wizard" or keyword = "hogwarts")]
  public List<Book> findBookInformation(List<Books> books)  
      throws ParserConfigurationException, SAXException, 
         IOException, XPathExpressionException {

    List<Book> foundBooks = new ArrayList<Book>(); // books matching criteria

    for (Book book : books) {
      DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
      domFactory.setNamespaceAware(true); // never forget this!
      DocumentBuilder builder = domFactory.newDocumentBuilder();
      Document doc = builder.parse(new InputSource(new StringReader(book.details))); // parse details XML into a Doc object

      XPathFactory factory = XPathFactory.newInstance();
      XPath xpath = factory.newXPath();
      //using one of the query examples
      XPathExpression expr = xpath.compile("/book[author = \"J K Rowling\" and genre = \"fantasy\" and (keyword = \"wizard\" or keyword = \"hogwarts\")]");

      Object result = expr.evaluate(doc, XPathConstants.NODESET); 
      NodeList nodes = (NodeList) result;
      if (null != nodes && nodes.getLength() > 0) {
        foundBooks.add(book); // add to your return list
      }
    }
    return foundBooks;
  }