Hi I am trying to convert a JAVA object to xml using JAXB marshaller .I am using a custom exception handler because i want to get rid of jaxb converting some special characters such as & to html & . I am using java 8. i am not sure what is wrong in my code. the following is my code trying to convert an object to xml using jaxb classes.
JAXBContext jaxbContext = JAXBContext.newInstance(SmilDTO.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setProperty( MepCharacterEscapeHandler.class.getName(), new MepCharacterEscapeHandler() );
StringWriter stringWriter = new StringWriter();
marshaller.marshal(smilDTO, stringWriter);
String smilDefinition = stringWriter.toString();
log.debug("Created SMIL definition : {}", smilDefinition);
return Optional.of(smilDefinition);
} catch (JAXBException e) {
e.printStackTrace();
log.error("Cannot parse slides into SMIL definition! {}", e.getMessage());
return Optional.empty();
}
this is my escape character handler class. public class MepCharacterEscapeHandler implements com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler {
@Override
public void escape(char[] ch, int start, int length, boolean isAttVal, Writer out) throws IOException {
// avoid calling the Writerwrite method too much by assuming
// that the escaping occurs rarely.
// profiling revealed that this is faster than the naive code.
int limit = start+length;
for (int i = start; i < limit; i++) {
char c = ch[i];
if(c == '&' || c == '<' || c == '>' || c == '\r' || (c == '\"' && isAttVal) ) {
if(i!=start)
out.write(ch,start,i-start);
start = i+1;
switch (ch[i]) {
case '&':
out.write("&");
break;
case '<':
out.write("<");
break;
case '>':
out.write("<");
break;
case '\"':
out.write("\";");
break;
}
}
}
if( start!=limit )
out.write(ch,start,limit-start);
}
}
However i am getting an exception as below
javax.xml.bind.PropertyException: name: com.openmind.primecast.config.MepCharacterEscapeHandler value: com.openmind.primecast.config.MepCharacterEscapeHandler@4970b93d
at javax.xml.bind.helpers.AbstractMarshallerImpl.setProperty(AbstractMarshallerImpl.java:358)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.setProperty(MarshallerImpl.java:527)
at com.openmind.primecast.service.SmilService.getSmilDefinitionFromSlides(SmilService.java:56)
at com.openmind.primecast.service.mapper.TemplateMapper.parseSlideDTOList(TemplateMapper.java:56)
at com.openmind.primecast.service.mapper.TemplateMapperImpl.toEntity(TemplateMapperImpl.java:96)
at com.openmind.primecast.service.impl.TemplateServiceImpl.create(TemplateServiceImpl.java:85)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
appreciate any help tried all options but it didnt work thank you