在 Java 中查询 XML 的最简单方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/807418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:42:11  来源:igfitidea点击:

Simplest way to query XML in Java

javaxml

提问by

I have small Strings with XML, like:

我有带有 XML 的小字符串,例如:

String myxml = "<resp><status>good</status><msg>hi</msg></resp>";

which I want to query to get their content.

我想查询以获取其内容。

What would be the simplest way to do this?

什么是最简单的方法来做到这一点?

回答by willcodejavaforfood

You could try JXPath

你可以试试JXPath

回答by Pierre

convert this string into a DOM object and visit the nodes:

将此字符串转换为 DOM 对象并访问节点:

Document dom= DocumentBuilderFactory().newDocumentBuilder().parse(new InputSource(new StringReader(myxml)));
Element root= dom.getDocumentElement();
for(Node n=root.getFirstChild();n!=null;n=n.getNextSibling())
 {
 System.err.prinlnt("Current node is:"+n);
 }

回答by McDowell

XPathusing Java 1.5 and above, without external dependencies:

XPath使用 Java 1.5 及更高版本,没有外部依赖:

String xml = "<resp><status>good</status><msg>hi</msg></resp>";

XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();

InputSource source = new InputSource(new StringReader(xml));
String status = xpath.evaluate("/resp/status", source);

System.out.println("satus=" + status);

回答by JavaRocky

After your done with simple ways to query XML in java. Look at XOM.

在你完成了在 java 中查询 XML 的简单方法之后。看看 XOM。

回答by Jonik

Using dom4j, similar to McDowell's solution:

使用dom4j,类似于McDowell 的解决方案

String myxml = "<resp><status>good</status><msg>hi</msg></resp>";

Document document = new SAXReader().read(new StringReader(myxml));
String status = document.valueOf("/resp/msg");

System.out.println("status = " + status);

XML handling is a bit simpler using dom4j. And several other comparable XML libraries exist. Alternatives to dom4j are discussed here.

使用 dom4j 处理 XML 会更简单一些。和其他一些可比性的XML库存在此处讨论了dom4j 的替代方案。

回答by Peter ?tibrany

Here is example of how to do that with XOM:

以下是如何使用XOM执行此操作的示例:

String myxml = "<resp><status>good</status><msg>hi</msg></resp>";

Document document = new Builder().build(myxml, "test.xml");
Nodes nodes = document.query("/resp/status");

System.out.println(nodes.get(0).getValue());

I like XOM more than dom4j for its simplicity and correctness. XOM won't let you create invalid XML even if you want to ;-) (e.g. with illegal characters in character data)

我喜欢 XOM 多于 dom4j,因为它的简单性和正确性。即使您想要,XOM 也不会让您创建无效的 XML ;-)(例如在字符数据中使用非法字符)

回答by OscarRyz

@The comments of this answer:

@这个答案的评论 :

You can create a method to make it look simpler

你可以创建一个方法让它看起来更简单

String xml = "<resp><status>good</status><msg>hi</msg></resp>";

System.out.printf("satus= %s\n", getValue("/resp/status", xml ) );

The implementation:

实施:

public String getValue( String path, String xml ) { 
    return XPathFactory
               .newInstance()
               .newXPath()
               .evaluate( path , new InputSource(
                                 new StringReader(xml)));

}

回答by vtd-xml-author

Here is a code snippet of querying your XML with VTD-XML

这是使用VTD-XML查询 XML 的代码片段

import com.ximpleware.*;
public class simpleQuery {

    public static void main(String[] s) throws Exception{
        String myXML="<resp><status>good</status><msg>hi</msg></resp>";
        VTDGen vg = new VTDGen();
        vg.setDoc(myXML.getBytes());
        vg.parse(false);
        VTDNav vn = vg.getNav();
        AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath("/resp/status");
        int i = ap.evalXPath();
        if (i!=-1)
            System.out.println(" result ==>"+vn.toString(i));
    }
}

回答by deamon

You can use Jerryto query XML similar to jQuery.

您可以使用Jerry来查询类似于 jQuery 的 XML。

jerry(myxml).$("status")