Tausif Shakeel
Tausif Shakeel

Reputation: 83

How to convert XML containing code and different namespaces to objects using Java?

<?xml version="1.0" encoding="UTF-8"?>
<Response xmlns="http://www.demo.com/response/ResponseDetails"    
xmlns:ft="http://www.demo.com/response/footer" 
xmlns:hd="http://www.demo.com/response/header"   
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       
xsi:schemaLocation="http://www.demo.com/response/ResponseDetails    
Response.xsd">
<hd:header>
  <hd:STATUS>
    <hd:MESSAGE>
      <hd:MESSAGE_CODE>0000</hd:MESSAGE_CODE>
      <hd:MESSAGE_DESC>SUCCESS</hd:MESSAGE_DESC>
      <hd:MESSAGE_TYPE>SU</hd:MESSAGE_TYPE>
    </hd:MESSAGE>
  </hd:STATUS>
</hd:header>
<ResponseDetails>
  <TAG1>tag 1 value</TAG1>
  <TAG2>tag2 value</TAG2>
  <TAG3>tag3 value</TAG3>
  <TAG4>tag4 value</TAG4>
</ResponseDetails>
<ft:footer>
  <ft:CODEDESCRIPTION />
</ft:footer>
</Response>

This is sample response I get from a web service call and I am unable to convert this string response to object. The above XML is the exact which I receive. How can I convert the above XML into objects using either JAXB or xstream?

Upvotes: 0

Views: 157

Answers (1)

kjhughes
kjhughes

Reputation: 111620

JAXB can convert XSDs to Java classes. There are many resources to help you get started, including Oracle's official Introduction to JAXB.

That your XML contains code such as Prolog matters not. Treat it as text.

Different namespaces are also supported. You might want to read through some of Blaise Doughan's incredibly helpful answers here on Stack Overflow such as this one.

If this answer is too broad, you might want to narrow the scope of your question.

Upvotes: 1

Related Questions