我正在研究struts2。我打电话给一个简单的网络服务,它给我所有国家的列表。我得到的结果是字符串形式,但实际上是一个xml输出。那么我怎样才能得到结果和简单的字符串。 当我在控制台中打印它时,得到如下结果:
<NewDataSet>
<Table>
<Name>Zambia</Name>
</Table>
<Table>
<Name>Zimbabwe</Name>
</Table>
</NewDataSet>
这被封装为字符串。 谁能帮助我让所有国家都处于正常的字符串?
答案 0 :(得分:1)
在@nmenego在评论中提供的链接中,以下答案是最好的开始&amp;因此,再次写在这里。因为其他人需要外部图书馆,尽管他们有其优点。
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);
另一种方法可能是将XML转换为POJO
这是一个很好的例子 - http://www.mkyong.com/java/jaxb-hello-world-example/