I have java code and i'm trying to parse it and store it as XML file on my PC using JAXB but i have a marshaling exception:
Exception in thread "main" javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.internal.SAXException2: unable to marshal type "java.lang.String" as an element because it is missing an #XmlRootElement annotation]
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:311)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:236)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:103)
at xml.ConfList.addToList(ConfList.java:29)
at xml.Tester.work(Tester.java:34)
at xml.Tester.main(Tester.java:16)
Caused by: com.sun.istack.internal.SAXException2: unable to marshal type "java.lang.String" as an element because it is missing an #XmlRootElement annotation
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:237)
at com.sun.xml.internal.bind.v2.runtime.LeafBeanInfoImpl.serializeRoot(LeafBeanInfoImpl.java:126)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:483)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:308)
... 5 more
Java Result: 1
BUILD SUCCESSFUL (total time: 9 seconds)
The JAXB code i'm using :
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class ConfList {
private static final String fileName = "Source.xml";
List<String> xmlConfList;
private Object object;
public ConfList(Object object){
this.object = object;
}
public void addToList() throws IOException, JAXBException {
File file = new File(fileName);
JAXBContext jaxbContext = JAXBContext.newInstance(XmlConf.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(object, file); // i got the exception here
jaxbMarshaller.marshal(object, System.out);
}
}
The main class i use:
import java.io.IOException;
import java.util.ArrayList;
import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.xpath.XPathExpressionException;
import org.xml.sax.SAXException;
public class Tester{
public static void main (String [] args) throws XPathExpressionException, IOException, ParserConfigurationException, SAXException, TransformerException, JAXBException{
work();
}
public static void work () throws IOException, ParserConfigurationException, SAXException, XPathExpressionException, TransformerException, JAXBException{
String surl = "http://api.worldweatheronline.com/free/v1/weather.ashx?q=Cairo&format=xml&num_of_days=1&key=wd63kxr294rcgvbynhaf2z4r";
XmlSource xml = new XmlSource(surl);
xml.validate();
xml.load();
ArrayList<String> paths = xml.getAllPaths();
for(String path : paths){
System.out.println(path);
}
ConfList v = new ConfList(xml.getXml());
v.addToList();
}
}
The XmlConf Class :
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name = "XmlSource")
public class XmlConf {
private URL url;
private List<String> path = new ArrayList<String>();
private String urlp;
private Map<String, String> parameters;
private String host;
public URL getUrl() {
return url;
}
#XmlAttribute(name = "URL")
public void setUrl(URL url) {
this.url = url;
}
#XmlElement
public List<String> getPath() {
return path;
}
public void setPath(String path) {
this.path.add(path);
}
#XmlElement
public void setUrlPath(String urlp){
this.urlp = urlp;
}
public String getUrlPath(){
return urlp;
}
public void setParameters(Map<String, String> parameters){
this.parameters = parameters;
}
public Map<String, String> getParameters(){
return parameters;
}
public void setHostName(String host){
this.host = host;
}
public String getHostName(){
return host;
}
}
Note that all (XmlSource class) methods are run correctly.
Below is some sample code that unmarshals some XML into an instance of XmlConf and then marshals it back to XML.
Demo
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(XmlConf.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum16762200/input.xml");
XmlConf xmlConf = (XmlConf) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(xmlConf, System.out);
}
}
input.xml/Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<XmlSource URL="http://www.example.com">
<parameters>
<entry>
<key>A</key>
<value>a</value>
</entry>
<entry>
<key>B</key>
<value>b</value>
</entry>
</parameters>
<path>foo</path>
<urlPath>bar</urlPath>
</XmlSource>
Related
I am new to Restful W-S.I am trying to make basic web-service.I am unable to pass XML content (I am able to pass when I change it to plain text successfully)
package com.controller;
import javax.validation.constraints.NotBlank;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.pojo.Student;
#RestController
public class Controllers {
#RequestMapping(value="/hi",method=RequestMethod.POST,consumes=MediaType.TEXT_PLAIN_VALUE,produces=MediaType.APPLICATION_XML_VALUE)
public String hello(#RequestBody String std) {
System.out.println(std);
return "Response";
}
}
package com.pojo;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name="student")
#XmlAccessorType(XmlAccessType.FIELD)
public class Student {
#XmlElement(name="str")
private String str;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
}
Client-
package com.me.app;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.client.RestTemplate;
import com.pojo.Student;
#SpringBootApplication
#ComponentScan(basePackageClasses=com.controller.Controllers.class)
public class App {
public static void main(String args[])
{
SpringApplication.run(App.class, args);
getEmployees();
}
private static void getEmployees()
{
System.out.println("starting");
final String uri = "http://localhost:8005/hi";
Student std = new Student();
std.setStr("Something");
String s=jaxbObjectToXML(std);
System.out.println(s);
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.postForObject(uri, s, String.class);
System.out.println(result);
}
private static String jaxbObjectToXML(Student std) {
String xmlString = "";
try {
JAXBContext context = JAXBContext.newInstance(Student.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); // To format XML
StringWriter sw = new StringWriter();
m.marshal(std, sw);
xmlString = sw.toString();
} catch (JAXBException e) {
e.printStackTrace();
}
return xmlString;
}
}
Can anyone help me in passing content in XML.In the above scenario,I am able to pass text as content and its working fine.
Thanks in advance!
Try adding m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); Hope this helps.
I am trying to convert a Java program I had written to read a csv file into a Spring Boot Application but keep getting a NullPointerException. I just want to print out the contents of the csv file.
Here is the code:
Beans.xml
<?xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="Assembler" class="sample.spring.chapter01.Assembler">
<property name="infopieces" value="classpath:1erelistearticle2.csv"></property>
</bean>
<bean id="PieceDAO" class="sample.spring.chapter01.PieceDAO">
<property name="listp" ref="Piece"></property>
</bean>
<bean id = "Piece" class = "sample.spring.chapter01.Piece"/>
</beans>
Minapp.java
package sample.spring.chapter01;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;
public class MinApp {
private static ApplicationContext context;
private static Logger logger = Logger.getLogger(MinApp.class);
public static void main(String[] args) throws FileNotFoundException, IOException{
context=new ClassPathXmlApplicationContext("Beans.xml");
Resource resource =context.getResource("classpath:1erelistearticle2.csv");
Assembler obj=(Assembler) context.getBean("Assembler");
obj.display();
try {
ArrayList<Piece> listp=obj2.getList();
for (Piece p:listp) {
System.out.println(p);
}
} catch (IOException ef) {
}
}
Assembler.java
package sample.spring.chapter01;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import com.opencsv.CSVReader;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.springframework.core.io.Resource;
public class Assembler {
private PieceDAO listp;
//private CSVParser parser;
private Resource infopieces;
public void setlistp(PieceDAO lp) throws FileNotFoundException, IOException {
fill();
}
public void setinfopieces(Resource csvFile){
this.infopieces = csvFile;
}
public PieceDAO getlistp() {
return listp;
}
public CSVParser getinfocsv() throws FileNotFoundException, IOException{
// BufferedReader br = new BufferedReader(new FileReader("classpath:1erelistearticle.csv"));
//BufferedReader br = new BufferedReader(new FileReader("classpath:1erelistearticle.csv"));
InputStream is=infopieces.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
//ClassLoader classloader=Thread.currentThread().getContextClassLoader();
//InputStream is=classloader.getResourceAsStream("classpath:1erelistearticle.csv");
//CSVReader cr=new CSVReader(is);
CSVParser parser = CSVFormat.DEFAULT.withHeader().parse(br);
//CSVParser parser = CSVFormat.DEFAULT.withHeader().parse(is);
//for (CSVRecord record: parser) {
// System.out.println(record.get("référence ascensoriste"));
//}
return parser;
}
public void fill() throws FileNotFoundException, IOException{
CSVParser parser=getinfocsv();
for (CSVRecord record:parser) {
String ref=record.get("référence ascensoriste").trim();
String asc=record.get("ascensoriste").trim();
String desc=record.get("Description article").trim();
String prix=record.get("Pv");
String category=record.get("Familie").trim();
//System.out.println(category);
Piece lift_comp=new Piece();
lift_comp.setasc(asc);
lift_comp.setdesc(desc);
lift_comp.setref(ref);
lift_comp.setprice(prix);
lift_comp.settype(category);
lift_comp.setinfo();
listp.addPiece(lift_comp);
}
}
public void display() {
listp.output();
}
}
There are plenty of problems with the code. I guess the first one is
The call
obj.display();
which calls listp.output(); but the listp is never assigned. neither from config nor from code.
The problem has been solved. The classes and beans.xml are as below and read the csv file using the Spring framework.
Beans.xml
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="PieceDAO" class="sample.spring.chapter01.PieceDAO">
<property name="listp" ref="Piece"></property>
</bean>
<bean id = "Piece" class = "sample.spring.chapter01.Piece"/>
</beans>
Minapp.java
package sample.spring.chapter01;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;
public class MinApp {
private static ApplicationContext context;
private static Logger logger = Logger.getLogger(MinApp.class);
public static void main(String[] args) throws FileNotFoundException, IOException{
context=new ClassPathXmlApplicationContext("Beans.xml");
Resource resource =context.getResource("classpath:1erelistearticle2.csv");
PieceDAO obj=(PieceDAO) context.getBean("PieceDAO");
obj.fill(resource);
obj.display();
}
}
PieceDAO.java
package sample.spring.chapter01;
import java.util.List;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.springframework.core.io.Resource;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class PieceDAO {
private List<Piece> listp;
//private Resource resource;
public PieceDAO() {
}
public void setlistp(Piece p) throws FileNotFoundException, IOException{
listp=new ArrayList<Piece>();
//this.fill(resource);
}
public Piece getlistp(Piece p) {
for (Piece currp:listp) {
if (currp.equals(p)) {
return currp;
}
}
return null;
}
public void fill(Resource resource) throws FileNotFoundException, IOException{
InputStream is=resource.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
CSVParser parser = CSVFormat.DEFAULT.withHeader().parse(br);
for (CSVRecord record:parser) {
String ref=record.get("référence ascensoriste").trim();
String asc=record.get("ascensoriste").trim();
String desc=record.get("Description article").trim();
String prix=record.get("Pv");
String category=record.get("Familie").trim();
//System.out.println(category);
Piece lift_comp=new Piece();
lift_comp.setasc(asc);
lift_comp.setdesc(desc);
lift_comp.setref(ref);
lift_comp.setprice(prix);
lift_comp.settype(category);
lift_comp.setinfo();
listp.add(lift_comp);
}
}
public void display() {
for (Piece p:listp) {
System.out.println(p);
}
}
}
i fetched the defect url:
String defectUrl = conn.buildEntityCollectionUrl("defect");
defectUrl += "/98";
Map<String, String> requestHeaders = new HashMap<String, String>();
requestHeaders.put("Accept", "application/xml");
Response res = conn.httpGet(defectUrl, null, requestHeaders);
then converted into Entity:
String postedEntityReturnedXml = res.toString();
Entity entity = EntityMarshallingUtils.marshal(Entity.class,postedEntityReturnedXml);
than change the field value:
List<Field> fields = entity.getFields().getField();
for (Field f : fields) {
if (f.getName().equalsIgnoreCase("id"))
{
int i=f.hashCode();
System.out.println(i);
f.getValue().clear();
f.setName("");
}
}
now trying to convert it to xml formate again:
String xml = EntityMarshallingUtils.unmarshal(org.eclipse.jetty.server.Response.class,**fields**);
System.out.println(xml);
throwing error as object fields should not be there and not able to get what should i put in place of fields?
my EntityMarshallingUtils class is like:
package org.hp.qc.web.restapi.docexamples.docexamples.infrastructure;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.Marshaller;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
//import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class EntityMarshallingUtils {
private EntityMarshallingUtils() {}
#SuppressWarnings("unchecked")
public static <T> T marshal(Class<T> c, String xml) throws JAXBException {
T res;
if (c == xml.getClass()) {
res = (T) xml;
}
else {
JAXBContext ctx = JAXBContext.newInstance(c);
Unmarshaller marshaller = ctx.createUnmarshaller();
res = (T) marshaller.unmarshal(new StringReader(xml));
}
return res;
}
#SuppressWarnings("unchecked")
public static <T> String unmarshal(Class<T> c, Object o) throws Exception {
JAXBContext ctx = JAXBContext.newInstance(c);
Marshaller marshaller = ctx.createMarshaller();
StringWriter entityXml = new StringWriter();
marshaller.marshal(o, entityXml);
String entityString = entityXml.toString();
return entityString;
}
}
AnyOne who can help me on this?
I try to unmarshal a String using this code:
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAnyAttribute;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.namespace.QName;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.io.IOUtils;
import org.xml.sax.InputSource;
#XmlRootElement(name="Grid")
public class Marshal {
#XmlAttribute(name="Reload", required = false)
public int reload;
#XmlElementWrapper(name="Changes")
#XmlElement(name="I")
public List<XmlAttributeHolder> rowList = new ArrayList<XmlAttributeHolder>();
public static void main(String[] args) {
try {
JAXBContext jc = JAXBContext.newInstance(Marshal.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
// à€
String xmlString = "<Grid><IO/><Changes><I id=\"0\" Changed=\"1\" STT=\"à€\"/></Changes></Grid>";
InputStream inputStream = IOUtils.toInputStream(xmlString);
InputSource is = new InputSource(inputStream);
is.setEncoding("ISO-8859-1");
Marshal obj = (Marshal) unmarshaller.unmarshal(is);
System.out.println(xmlString);
for (int i=0;i<obj.rowList.size();i++) {
XmlAttributeHolder xah = obj.rowList.get(i);
System.out.println(xah.getAttrMap());
for (String formValue:xah.getAttrMap().values()) {
System.out.println(StringEscapeUtils.unescapeXml(formValue));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static class XmlAttributeHolder {
#XmlAnyAttribute
public Map<QName, String> attrMap = new HashMap<QName, String>();
public void addAttribute(String name, String value) {
attrMap.put(QName.valueOf(name), value);
}
public String getAttribute(String name) {
return attrMap.get(QName.valueOf(name));
}
public Map<QName, String> getAttrMap() {
return attrMap;
}
}
}
I try to run this code in Java 1.6 windows and gives the correct answer:
0
1
à€
When I try to run this code in IBM java 1.6 CentOS gives the wrong answer:
0
1
à €
Why the unmarshalling instruction doesn't convert correctly the à (even èéìòù...)?
If your input is actually a String I'd recommend passing that directly to the Unmarshaller, wrapped in a StringReader instead of trying to produce an InputStream from it. It's less error prone.
Try this (see code snippet below). Then you don't have to worry about whether your code specifies the correct encoding or does the character to byte conversion correctly for that encoding.
String xmlString = "<Grid><IO/><Changes><I id=\"0\" Changed=\"1\" STT=\"à€\"/></Changes></Grid>";
InputSource is = new InputSource(new StringReader(xmlString));
Marshal obj = (Marshal) unmarshaller.unmarshal(is);
I have an application importing and exporting data from an Oracle database to/from XML using JAXB. Now there are some BLOB fields in the DB containing uploaded files which I would like to have in the XML as a base64 encoded string. This works quite well out of the box with JAXB using #XmlSchemaType(name = "base64Binary") as done below:
#XmlType
public class DocumentTemplateFile {
// other fields ommited
#XmlElement(required = true)
#XmlSchemaType(name = "base64Binary")
private byte[] data;
// other code ommited
}
The problem with this solution is that the whole file content is stored in memory because of the byte array. Depending on the size of the file this could cause some issues.
I was therefore wondering if there's a way to create an XmlAdapter or similar that gets Streams from and to the file, so that I can stream it directly to / from the DB's BLOB without having the whole content in memory. I was thinking on something similar to this:
public class BlobXmlAdapter extends XmlAdapter<InputStream, OutputStream> {
#Override
public InputStream marshal(final OutputStream value) throws Exception {
return null;
}
#Override
public OutputStream unmarshal(final InputStream value) throws Exception {
return null;
}
}
This is obviously only an illustrative example so that you can have an idea of what I'm looking for. The end solution doesn't necessarily have to make use of XmlAdaters. All I need is a way to hook on the un/marshalling process and stream the data through a buffer / queue rather than storing everything in a byte array.
this solution uses following third party library. you should use following maven dependency:
<dependency>
<groupId>jlibs</groupId>
<artifactId>jlibs-xsd</artifactId>
<version>2.0</version>
</dependency>
<repository>
<id>jlibs-snapshots-repository</id>
<name>JLibs Snapshots Repository</name>
<url>https://raw.githubusercontent.com/santhosh-tekuri/maven-repository/master</url>
<layout>default</layout>
</repository>
We need to use following custom XmlAdapter:
import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.io.File;
/**
* #author Santhosh Kumar Tekuri
*/
public class Base64Adapter extends XmlAdapter<String, File>{
#Override
public File unmarshal(String v) throws Exception{
return new File(v);
}
#Override
public String marshal(File v) throws Exception{
throw new UnsupportedOperationException();
}
}
now change your pojo to use above adapter:
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.io.File;
#XmlRootElement
public class DocumentTemplateFile {
#XmlElement(required = true)
public String userName;
#XmlElement(required = true)
#XmlSchemaType(name = "base64Binary")
#XmlJavaTypeAdapter(Base64Adapter.class)
public File data;
}
now the following helper class should be used to read xml file:
import jlibs.xml.Namespaces;
import jlibs.xml.xsd.DOMLSInputList;
import jlibs.xml.xsd.XSParser;
import jlibs.xml.xsd.XSUtil;
import org.apache.xerces.xs.XSElementDeclaration;
import org.apache.xerces.xs.XSModel;
import org.apache.xerces.xs.XSSimpleTypeDefinition;
import org.apache.xerces.xs.XSTypeDefinition;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLFilterImpl;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.namespace.QName;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.Result;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* #author Santhosh Kumar Tekuri
*/
public class JAXBBlobUtil{
public static XSModel generateSchemas(Class clazz) throws Exception{
final Map<String, ByteArrayOutputStream> schemas = new HashMap<String, ByteArrayOutputStream>();
JAXBContext.newInstance(clazz).generateSchema(new SchemaOutputResolver(){
#Override
public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException{
ByteArrayOutputStream bout = new ByteArrayOutputStream();
schemas.put(suggestedFileName, bout);
StreamResult result = new StreamResult(bout);
result.setSystemId(suggestedFileName);
return result;
}
});
DOMLSInputList lsInputList = new DOMLSInputList();
for(Map.Entry<String, ByteArrayOutputStream> entry : schemas.entrySet()){
ByteArrayInputStream bin = new ByteArrayInputStream(entry.getValue().toByteArray());
lsInputList.addStream(bin, entry.getKey(), null);
}
return new XSParser().parse(lsInputList);
}
private static Object unmarshal(Class clazz, InputSource is) throws Exception{
XSModel xsModel = generateSchemas(clazz);
JAXBContext context = JAXBContext.newInstance(clazz);
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
XMLReader xmlReader = factory.newSAXParser().getXMLReader();
xmlReader = new Base64Filter(xmlReader, xsModel);
return context.createUnmarshaller().unmarshal(new SAXSource(xmlReader, is));
}
private static class Base64Filter extends XMLFilterImpl{
private XSModel schema;
private List<QName> xpath = new ArrayList();
private FileWriter fileWriter;
public Base64Filter(XMLReader parent, XSModel schema){
super(parent);
this.schema = schema;
}
#Override
public void startDocument() throws SAXException{
xpath.clear();
super.startDocument();
}
#Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException{
super.startElement(uri, localName, qName, atts);
xpath.add(new QName(uri, localName));
XSElementDeclaration elem = XSUtil.findElementDeclaration(schema, this.xpath);
if(elem!=null){
XSTypeDefinition type = elem.getTypeDefinition();
if(type.getTypeCategory()==XSTypeDefinition.SIMPLE_TYPE){
XSSimpleTypeDefinition simpleType = (XSSimpleTypeDefinition)type;
while(!Namespaces.URI_XSD.equals(simpleType.getNamespace()))
simpleType = (XSSimpleTypeDefinition)simpleType.getBaseType();
if("base64Binary".equals(simpleType.getName())){
try{
File file = File.createTempFile("data", "binary");
file.deleteOnExit();
fileWriter = new FileWriter(file);
String absolutePath = file.getAbsolutePath();
super.characters(absolutePath.toCharArray(), 0, absolutePath.length());
}catch(IOException ex){
throw new SAXException(ex);
}
}
}
}
}
#Override
public void characters(char[] ch, int start, int length) throws SAXException{
try{
if(fileWriter==null)
super.characters(ch, start, length);
else
fileWriter.write(ch, start, length);
}catch(IOException ex){
throw new SAXException(ex);
}
}
#Override
public void endElement(String uri, String localName, String qName) throws SAXException{
xpath.remove(xpath.size() - 1);
try{
if(fileWriter!=null)
fileWriter.close();
fileWriter = null;
}catch(IOException ex){
throw new SAXException(ex);
}
super.endElement(uri, localName, qName);
}
};
}
Now read xml file as below:
public static void main(String[] args) throws Exception{
DocumentTemplateFile obj = (DocumentTemplateFile)unmarshal(DocumentTemplateFile.class, new InputSource("sample.xml"));
// obj.data refers to File which contains base64 encoded data
}
create custom XmlAdapter as below:
public class Base64FileAdapter extends XmlAdapter<String, File>{
#Override
public String marshal(File file) throws Exception {
// todo: read file and convert to base64 and return
}
#Override
public File unmarshal(String data) throws Exception {
File file = File.createTempFile("dataFile", "binary");
file.deleteOnExit();
//todo: base64 decode string data and write bytes to file
return file;
}
}
now inside you bean, use it:
#XmlElement(required = true)
#XmlJavaTypeAdapter(Base64FileAdapter.class)
private File dataFile;
now the entire binary content is stored in file. you can read/write from this file. and this file is deleted on jvm exit.