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

问题描述:

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

I have small Strings with XML, like:

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?

XPath ,没有外部依赖关系:

XPath using Java 1.5 and above, without external dependencies:

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);