What if a web service returns its result in form of XML? like this may be -
It's easy!!
This is the code snippet which is going to do the magic!!
note:
It's easy!!
This is the code snippet which is going to do the magic!!
Config config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(UriBuilder.fromUri("http://localhost:8080/Test")
.build());
InputSource inputSource = new
InputSource(new StringReader(service.path(
"resources/test/Tom").get(String.class)));
DocumentBuilderFactory
factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(inputSource);
NodeList nameList = doc.getElementsByTagName("name");
NodeList surnameList = doc.getElementsByTagName("surname");
NodeList addressList = doc.getElementsByTagName("address");
System.out.println("No. of result returned : " + nameList.getLength());
List<User> userList = new ArrayList<User>();
for (int count = 0; count < nameList.getLength(); count++) {
User user = new User(nameList.item(count).getTextContent(),
surnameList.item(count).getTextContent(),
addressList.item(count).getTextContent());
userList.add(user);
}
System.out.println(userList);
note:
a. Obviously, User is the bean class as mentioned in the previous post.
b. doc.getElementsByTagName("name") means that we are reading the data from the nodes where the node name is "name"
Related post: A Web Service that returns XML
Feel free to share!
"The true sign of intelligence is not knowledge but imagination" - Albert Einstein
b. doc.getElementsByTagName("name") means that we are reading the data from the nodes where the node name is "name"
Related post: A Web Service that returns XML
Feel free to share!
"The true sign of intelligence is not knowledge but imagination" - Albert Einstein
No comments :
Post a Comment