JAXB元帅setProperty com.sun.xml.bind.CharacterEscapeHandler

问题描述 投票:0回答:2

我试图覆盖 JAXB 编码中特殊字符的转义。 为此,我使用了一个实现

CharacterEscapeHandler
.

的接口

这是界面:

package project1;
import com.sun.xml.bind.marshaller.CharacterEscapeHandler;
import java.io.IOException;
import java.io.Writer;
public class MinimumEsc implements CharacterEscapeHandler {
    private MinimumEsc( ) {

        System.out.println("Function called");


    }  // no instanciation please

    public static final CharacterEscapeHandler theInstance = new MinimumEsc(); 


    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.

            System.out.println("Function called");
            int limit = start+length;
            for (int i = start; i < limit; i++) {
                char c = ch[i];
                if( c=='&' || c=='<' || c=='>' || (c=='\"' && isAttVal) ) {
                    if(i!=start)
                        out.write(ch,start,i-start);
                    start = i+1;
                 switch (ch[i]) {
                    case '&' :
                        out.write("&amp2;");
                        break;
                    case '<' :
                        out.write("&lt2;");
                        break;
                    case '>' :
                        out.write("&gt2;");
                        break;
                    case '\"' :
                        out.write("&quot2;");
                        break;
                    }
                }
            }

            if( start!=limit )
                out.write(ch,start,limit-start);
        }
}

从我的主课我试图调用

marshal
方法:

public static void main(String[] args){
    System.out.println("hello world");



    try{
        //generate the java object
        Shiporder so = new Shiporder();
        so.setOrderid("123456");
        so.setOrderperson("Elio  < > ' \"  e  <> \" Khattar");


        //generate the file
        File f = new File("C:\\tst_encode.xml");
        if(!f.exists()){
          f.createNewFile();
        }


        JAXBContext context = JAXBContext.newInstance("xsdobjects");
        Marshaller jaxbMarshaller = context.createMarshaller();
     //  Marshaller jaxbMarshaller=  new JAXBContext().newMarshaller();


        System.out.println(CharacterEscapeHandler.class.getName());
        CharacterEscapeHandler escapeHandler =  MinimumEsc.theInstance;
        jaxbMarshaller.setProperty("com.sun.xml.bind.characterEscapeHandler",escapeHandler);



        jaxbMarshaller.marshal(so, f);
    }catch(JAXBException e){
        e.getCause();
        e.getErrorCode();
        e.printStackTrace();

    }catch(IOException ioe){
        ioe.printStackTrace();        
    }
}

我在运行它时收到此错误消息:

javax.xml.bind.PropertyException: name: com.sun.xml.bind.characterEscapeHandler value: project1.MinimumEsc@100bac2
    at org.eclipse.persistence.jaxb.JAXBMarshaller.setProperty(JAXBMarshaller.java:520)
    at project1.Class1.main(Class1.java:56)

有人可以帮助我吗?我花了最后 5 个小时在网上搜索它并尝试了许多解决方案。非常感谢任何帮助。谢谢

java jakarta-ee jaxb
2个回答
0
投票

根据堆栈跟踪,您正在使用 EclipseLink MOXy 作为您的 JAXB (JSR-222) 提供程序。我们在 EclipseLink 2.4 中添加了对参考实现

com.sun.xml.bind.CharacterEscapeHandler
的支持。在 EclipseLink 发布之前,您会得到您所看到的异常。

更新

您可能遇到了以下错误,该错误已在 EclipseLink 2.5.1 流中修复。

您可以从以下位置下载夜间构建:


0
投票

检查类路径,com.sun.xml.bind.CharacterEscapeHandler。另外,检查mavan依赖

<dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.3.6</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jaxb</groupId>
        <artifactId>jaxb-runtime</artifactId>
        <version>2.3.6</version>
        <scope>compile</scope>
    </dependency>

如果使用依赖会报错

<!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.1</version>
    </dependency>
© www.soinside.com 2019 - 2024. All rights reserved.