如何查询在Java中同时两个XML文件? [英] How to Query two XML files simultaneously in Java?

查看:177
本文介绍了如何查询在Java中同时两个XML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的情况,我有两个XML文件,我想同时查询这些文件。

In my scenario, I have two XML files and I wanna query these files simultaneously.

文件1,publishers.xml

File 1, publishers.xml

<publishers>
    <publisher pubid="1" name="ABC" />
    <publisher pubid="2" name="RST" />
    <publisher pubid="3" name="XYZ" />
</publishers>

文件2,将Books.xml

File 2, books.xml

<books>
    <book bkid="1" pubid="1" name="introduction to A" />
    <book bkid="2" pubid="3" name="introduction to B" />
    <book bkid="3" pubid="3" name="introduction to C" />
    <book bkid="4" pubid="2" name="introduction to D" />
</books>

问:


  • 有没有用Java / Android的任何可用的XML查询的事情(类似LINQ)?

  • 在JAVA ,我该怎么办呢? (我想从两个XML文件同时查询)

  • Is there any XML Query thing available in Java/Android (similar to LINQ)?
  • IN JAVA, how can I do this? (I want to query from two XML files simultaneously)


选择图书发件人的books.xml
JOIN publishers.xml的打开 publishers.pubid = books.pubid
BKID =2

SELECT book FROM books.xml JOIN publishers.xml ON publishers.pubid = books.pubid AND bkid = "2"

让我解释一下在普通的英语:),节点&LT;书&GT; 从Books.xml的选择在属性 BKID = 2 ,并在同一时间&LT;发布者GT; 从publishers.xml文件,其中 publishers.pubid = books.pubid

Let me explain in "plain" English :), the node <book> is selected from books.xml where the attribute bkid=2 and at the same time <publisher> from the publishers.xml file where publishers.pubid=books.pubid.

所需的输出(处理):

Book ID: 2
Book Name: introduction to B
Publisher: XYZ

谢谢!

推荐答案

这是共同的?不,没有,但有时是必要的。在此 StackOverflow的答案描述我会使用XQuery。当然,如果你能合并文件,将简化您的问题。

Is this common? no it is not, but sometimes necessary. I would use Xquery as described in this StackOverflow answer. Of course if you could merge the files it would simplify your problem.

- 编辑 -

由于XQuery似乎并没有对Android的非常坚实的支持,我想看看不同的选项:

Since Xquery doesn't seem to have very solid support on Android I would look at a different option:


  1. 合并文件放在一起:你可以一个新的父节点内拉动两个流了下来,他们的concat &LT;根和GT; XML文件Concat的此处&lt; /根&GT; 然后转换该字符串转换成一个DOM文档,并使用XPath来解析它。

  1. Merge the files together: You could pull both of the streams down, concat them inside of a new parent node <root> xml files concat here </root> then convert this string into a dom document and use xpath to parse it.

这篇关于如何查询在Java中同时两个XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆