Web services 将Web服务字符串xml转换为普通字符串

Web services 将Web服务字符串xml转换为普通字符串,web-services,struts2,Web Services,Struts2,我正在研究struts2。我正在调用一个简单的web服务,它为我提供了所有国家的列表。我得到的结果是字符串形式的,但实际上是xml输出。那么,我如何在简单的字符串中得到结果和。 当我在控制台中打印它时,我得到如下结果: 赞比亚 津巴布韦 这被封装为字符串。 有人能帮我把所有国家都变成普通字符串吗?在@nmenego在评论中提供的链接中,下面的答案是最好的开始答案&因此,再次写在这里。因为其他人需要外部库,尽管他们有自己的优点 String xml = "<resp><sta

我正在研究struts2。我正在调用一个简单的web服务,它为我提供了所有国家的列表。我得到的结果是字符串形式的,但实际上是xml输出。那么,我如何在简单的字符串中得到结果和。 当我在控制台中打印它时,我得到如下结果:


赞比亚
津巴布韦

这被封装为字符串。
有人能帮我把所有国家都变成普通字符串吗?

在@nmenego在评论中提供的链接中,下面的答案是最好的开始答案&因此,再次写在这里。因为其他人需要外部库,尽管他们有自己的优点

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);
stringxml=“goodhi”;
XPathFactory XPathFactory=XPathFactory.newInstance();
XPath=xpathFactory.newXPath();
InputSource source=新的InputSource(新的StringReader(
xml);
String status=xpath.evaluate(“/resp/status”,source);
System.out.println(“satus=“+状态”);

另一种方法是将XML转换为POJO


这里有一个很好的例子-

你试过了吗?太好了。谢谢你,尼戈