Java 使用 JAXB Marshaller 处理 XML 转义字符(例如引号)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4435934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Handling XML escape characters (e.g. quotes) using JAXB Marshaller
提问by javdev
I need to serialize an XML java object to a XML file using the JAXB Marshaller (JAXB version 2.2). Now in the xml object, I have a tag which contains String valuesuch that:
我需要使用 JAXB Marshaller(JAXB 2.2 版)将 XML java 对象序列化为 XML 文件。现在在 xml 对象中,我有一个包含String 值的标签,这样:
"<"tagA>
**"<"YYYYY>done"<"/YYYYY>**
"<"/tagA>
Now as you can see that this string value again contains tags. I want this to be written in the same way in the xml file.
现在你可以看到这个字符串值再次包含标签。我希望在 xml 文件中以相同的方式编写它。
But JAXB Marshaller converts these values such as:
但是 JAXB Marshaller 会转换这些值,例如:
"&"lt;YYYYY"&"gt;"&"#xD;done
...& so on
"&"lt;YYYYY"&"gt;"&"#xD;done
...& 很快
I am not able to treat these escape characters separately using JAXB 2.2 Is it possible anyways?
我无法使用 JAXB 2.2 分别处理这些转义字符,这可能吗?
Any help in this regard will be great..
在这方面的任何帮助都会很棒..
Thanks in advance, Abhinav Mishra
提前致谢,阿比纳夫·米什拉
采纳答案by javdev
Done it by setting the following property for the JAXB Marshaller:
通过为 JAXB Marshaller 设置以下属性来完成它:
marshaller.setProperty("jaxb.encoding", "Unicode");
回答by bdoughan
You can leverage the CDATA structure. Standard JAXB does not cover this structure. There is an extension in EclipseLink JAXB (MOXy)for this (I'm the tech lead). Check out my answer to a related question:
您可以利用 CDATA 结构。标准 JAXB 不包括这种结构。EclipseLink JAXB (MOXy) 中有一个扩展(我是技术负责人)。查看我对相关问题的回答:
It describes the @XmlCDATA annotation in MOXy:
它描述了 MOXy 中的 @XmlCDATA 注释:
import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.oxm.annotations.XmlCDATA;
@XmlRootElement(name="c")
public class Customer {
private String bio;
@XmlCDATA
public void setBio(String bio) {
this.bio = bio;
}
public String getBio() {
return bio;
}
}
For more information see:
有关更多信息,请参阅:
回答by fred
Depending on what you are exactly looking for you can either :
根据您正在寻找的内容,您可以:
- disable character escaping
- or use CDATA string which support can be added into JAXB with just a bit of configuration
- 禁用字符转义
- 或使用 CDATA 字符串,只需稍加配置即可将其支持添加到 JAXB 中
回答by preetham
There is one simpler way. First use custom escape sequence:
有一种更简单的方法。首先使用自定义转义序列:
m.setProperty(CharacterEscapeHandler.class.getName(), new CharacterEscapeHandler() {
@Override
public void escape(char[] ch, int start, int length, boolean isAttVal, Writer out) throws IOException {
out.write( ch, start, length );
}
});
Then marshal it to a String like mentioned below
然后将其编组为如下所述的字符串
StringWriter writer = new StringWriter();
m.marshal(marshalObject, writer);
and then create a document object from the writer mentioned below
然后从下面提到的作者创建一个文档对象
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource( new StringReader( writer.toString() ) );
Document doc = builder.parse( is );
escape characters issue will be resolved
转义字符问题将得到解决
回答by Patrik Bego
With JAXB marshaller if you want full control over which characters to escape(e.g. "\'") you will have to add property :
使用 JAXB 编组器,如果您想完全控制要转义的字符(例如“\'”),则必须添加属性:
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(CharacterEscapeHandler.class.getName(), new CustomCharacterEscapeHandler());
and create a new CustomCharacterEscapeHandler class
并创建一个新的 CustomCharacterEscapeHandler 类
import com.sun.xml.bind.marshaller.CharacterEscapeHandler;
import java.io.IOException;
import java.io.Writer;
public class CustomCharacterEscapeHandler implements CharacterEscapeHandler {
public CustomCharacterEscapeHandler() {
super();
}
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 == '\'' || (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;
case '\'':
out.write("'");
break;
}
}
}
if( start!=limit )
out.write(ch,start,limit-start);
}
}
Hope that helps.
希望有帮助。