在Java中查询XML的最简单方法 [英] Simplest way to query XML in Java

查看:536
本文介绍了在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);

这篇关于在Java中查询XML的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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