Ich versuche, die Flucht von Sonderzeichen in JAXB -Codierung zu überschreiben.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;
if( c=='&' || c=='' || (c=='\"' && isAttVal) ) {
if(i!=start)
out.write(ch,start,i-start);
start = i+1;
switch (ch) {
case '&' :
out.write("&2;");
break;
case '' :
out.write(">2;");
break;
case '\"' :
out.write(""2;");
break;
}
}
}
if( start!=limit )
out.write(ch,start,limit-start);
}
}
< /code>
Aus meiner Hauptklasse versuche ich die Marschall < /code> Methode: < /p>
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();
}
}
< /code>
Ich erhalte diese Fehlermeldung, wenn ich sie ausführe: < /p>
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)
< /code>
Kann mir jemand helfen? Ich habe die letzten 5 Stunden damit verbracht, das Web dafür zu durchsuchen und viele Lösungen auszuprobieren. Jede Hilfe wird sehr geschätzt. Danke
JAXB Marschall SetProperty com.sun.xml.bind.characterescapeHandler ⇐ Java
-
- Similar Topics
- Replies
- Views
- Last post
-
-
Paket in beiden Modul org.glassfish.jaxb.runtime und Modul com.sun.xml.bind
by Anonymous » » in Java - 0 Replies
- 7 Views
-
Last post by Anonymous
-