Java 使用带有自定义字符转义处理程序的jaxb将对象转换为xml时出错

Java 使用带有自定义字符转义处理程序的jaxb将对象转换为xml时出错,java,jaxb,Java,Jaxb,您好,我正在尝试使用JAXB marshaller将JAVA对象转换为xml。我正在使用自定义异常处理程序,因为我想摆脱JAXB将一些特殊字符转换为html&。 我正在使用Java8。我不确定我的代码中有什么错误。下面是我尝试使用jaxb类将对象转换为xml的代码 JAXBContext jaxbContext = JAXBContext.newInstance(SmilDTO.class); Marshaller marshaller = jaxbContext.c

您好,我正在尝试使用JAXB marshaller将JAVA对象转换为xml。我正在使用自定义异常处理程序,因为我想摆脱JAXB将一些特殊字符转换为html&。 我正在使用Java8。我不确定我的代码中有什么错误。下面是我尝试使用jaxb类将对象转换为xml的代码

 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();
        }
这是我的转义字符处理程序类。 公共类MepCharacterEscapeHandler实现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);
    }

}
谢谢你的帮助 尝试了所有的选择,但没有成功
谢谢

尝试添加以下maven依赖项

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

com.sun.xml.bind

@prasanth这样做对您有所帮助。但现在它抛出了一个不同的异常属性“com.sun.xml.bind.characterEscapeHandler”必须是com.sun.xml.bind.marshaller.CharacterEscapeHandler类型的实例,而不是com.openmind.primecast.config.MepCharacterEscapeHandler您可以通过更新question.hi sagar发布有问题的异常吗marshaller.setProperty(CharacterEscapeHandler.class.getName(),new MepCharacterEscapeHandler());之前是marshaller.setProperty(MepCharacterEscapeHandler.class.getName(),新的MepCharacterEscapeHandler());…谢谢sagar,我在pom.xml中添加了jaxb impl,现在得到了不同的exception.property“com.sun.xml.bind.characterEscapeHandler”必须是com.sun.xml.bind.marshaller.CharacterEscapeHandler类型的实例,而不是com.openmind.primecast.config.MepCharacterEscapeHandler谢谢
<dependency>
   <groupId>com.sun.xml.bind</groupId>
   <artifactId>jaxb-impl</artifactId>
   <version>2.2.3</version>
</dependency>