简体   繁体   English

如何使用Java查询Xml文件

[英]How to query Xml file using java

I am having Xml file which contains about 50 Tables <tables></tables> . 我有Xml文件,其中包含大约50个Tables <tables></tables> All tables are linked to each other. 所有表都相互链接。

<table name="Information">
        <row>
            <field name="aggregateMID">123</field>
            <field name="MID">345</field>
            <field name="sequenceNum">1</field>
        </row>
        <row>
            <field name="aggregateMID">123</field>
            <field name="MID">346</field>
            <field name="sequenceNum">2</field>
        </row>
</table>

Can it be possible to query xml according to linking and fetch all required information? 是否可以根据链接查询xml并获取所有必需的信息? Please suggest what methods and API's will help me to do this job. 请提出哪些方法和API可以帮助我完成这项工作。

You can parse the XML file using a Java DOM Parser.

you can try this.

try {
    Document document = builder.parse(new FileInputStream("c:\\table.xml"));
} catch (SAXException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

You can refer these links: http://viralpatel.net/blogs/java-xml-xpath-tutorial-parse-xml/ 您可以参考以下链接: http : //viralpatel.net/blogs/java-xml-xpath-tutorial-parse-xml/

http://www.journaldev.com/1194/java-xpath-tutorial-with-examples http://www.journaldev.com/1194/java-xpath-tutorial-with-examples

I assume you have good reason not to use XPATH/XSLT directly. 我认为您有充分的理由不直接使用XPATH / XSLT。

If you want sql like queries, may be you can take a look at XQUERY 如果您想要类似查询的sql,也许可以看看XQUERY

Also, please remember that Xquery doesn't come with JAVA by dafault. 另外,请记住,Dafault并未随附Xquery附带的Xquery。 You have to download a suitable implementation jar from internet 您必须从互联网下载合适的实施jar

答案可能是XQuery,但是直到看到问题我们才能确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM