0

I am working on struts2. I am calling a simple web service which gives me list of all countries. The result I am getting is in string formal but actually its a xml output. So how can I get the result and in simple string. when I print it in console I get result like this:

<NewDataSet> <Table> <Name>Zambia</Name> </Table> <Table> <Name>Zimbabwe</Name> </Table> </NewDataSet>

this is encapsulated as string. can anyone help me to get all countries in normal string?

  • Have you tried [this](http://stackoverflow.com/questions/807418/simplest-way-to-query-xml-in-java)? – nmenego Nov 29 '13 at 08:01

1 Answers1

1

In the links provided by @nmenego in the comment, below answer is the best one to start & hence, writing it here again. Because others' require external libraries, although they have their merits.

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

Reference

Another approach could be to convert XML to POJO

Here's a very good example - http://www.mkyong.com/java/jaxb-hello-world-example/

Community
  • 1
  • 1
coding_idiot
  • 13,526
  • 10
  • 65
  • 116