Month: May 2015

Converting base64 Decoded XML into Java Object

This program explains how to convert a base64 decoded XML data with namespace into Java Object along with few exceptions explained.


import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "xmlPojo",namespace="http://test.service.com")
public class XmlPojo {

 private String name;

 /**
 * @return the name
 */
 public String getName() {
 return name;
 }

 /**
 * @param name the name to set
 */
 @XmlElement(name="name")
 public void setName(String name) {
 this.name = name;
 }
}

Now we have main class to test the application.

 

import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.Base64;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class TeatMain {

 public static void main(String[] args) {

 try {
 String encodedXml= "PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9InllcyI/Pg0KPG5zMjp4bWxQb2pvDQoJeG1sbnM6bnMyPSJodHRwOi8vdGVzdC5zZXJ2aWNlLmNvbS8iPg0KPG5hbWU+bXlOYW1lPC9uYW1lPg0KPC9uczI6eG1sUG9qbz4=";
 byte[] asBytes = Base64.getDecoder().decode(encodedXml);

 String decodedXml = new String(asBytes, "utf-8");
 JAXBContext jaxbContext = JAXBContext.newInstance(XmlPojo.class);
 Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

 XmlPojo rapidPerformance = (XmlPojo) unmarshaller.unmarshal(new StringReader(decodedXml));
 System.out
 .println(rapidPerformance.getName());
 } catch (UnsupportedEncodingException e) {
 } catch (JAXBException e) {
 }
 }
Output: myName
 
Sample xml file used in this program
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:xmlPojo
 xmlns:ns2="http://test.service.com/">
<name>myName</name>
</ns2:xmlPojo>

Key points to remember in this program,

  • when your xml has name space use namespace attribute in @XmlRootElement
  • while giving namespace in pojo make sure that it exactly matches in xml file ex: Here it ends with slash
  • If you forget the slash in the end of namespace we will get the below exception, I wrote this post because I spend half day find such issue.

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://test.service.com/", local:"xmlPojo"). Expected elements are <{http://test.service.com}xmlPojo>
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:642)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:254)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:249)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:116)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1049)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:478)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:459)
at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:148)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:379)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:605)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3138)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:880)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:649)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:211)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:184)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:214)
at com.test.TestMain(TestMain.java:37)