1

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

prasanth
  • 323
  • 1
  • 2
  • 13
  • Hi sagar i have added the jaxb-impl in the pom.xml now getting a different exception . property "com.sun.xml.bind.characterEscapeHandler" must be an instance of type com.sun.xml.bind.marshaller.CharacterEscapeHandler, not com.openmind.primecast.config.MepCharacterEscapeHandler thank you – prasanth Feb 18 '19 at 11:26

1 Answers1

2

Try by adding following maven dependency.

<dependency>
   <groupId>com.sun.xml.bind</groupId>
   <artifactId>jaxb-impl</artifactId>
   <version>2.2.3</version>
</dependency>

As given in this https://stackoverflow.com/a/45595929/5543072

Sagar P. Ghagare
  • 542
  • 2
  • 12
  • 25
  • @prasanth did this helped you. – Sagar P. Ghagare Feb 18 '19 at 11:04
  • but now it throws a different exception property "com.sun.xml.bind.characterEscapeHandler" must be an instance of type com.sun.xml.bind.marshaller.CharacterEscapeHandler, not com.openmind.primecast.config.MepCharacterEscapeHandler – prasanth Feb 18 '19 at 11:24
  • can you post that exception in question by updating question. – Sagar P. Ghagare Feb 18 '19 at 11:24
  • hi sagar it works now marshaller.setProperty(CharacterEscapeHandler.class.getName(), new MepCharacterEscapeHandler()); earlier it was marshaller.setProperty( MepCharacterEscapeHandler.class.getName(), new MepCharacterEscapeHandler() ); ... thank you – prasanth Feb 18 '19 at 11:36