拥有
String translationXsd = TranslationPropertyHelper.getFileLocation(PropertyKey.TRANSLATE_XSD_FILE);
File translationXsdFile = new File(translationXsd);
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(translationXsdFile);
JAXBContext jaxbContext = JAXBContext
.newInstance(translationJob.getClass().getPackage().getName());
Marshaller marshaller = jaxbContext.createMarshaller();
OutputStream os = new FileOutputStream(pOutputFile);
XMLOutputFactory xmlof = XMLOutputFactory.newInstance();
XMLStreamWriter xsw = new IndentingXMLStreamWriter(xmlof.createXMLStreamWriter(os));
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, translationXsdFile.getName());
marshaller.setSchema(schema);
marshaller.marshal(translationJob, xsw);
xsw.close();
有一个自由文本,例如"hello,我在里面有粗体文本。“在节点中
生成
<freetextnode>hello i have < b > bold < / b > text inside.</freetextnode>
期望值是:
<freetextnode>hello i have < b > bold < / b > text inside.</freetextnode>
JavaEE 7。
发布于 2019-03-22 07:28:40
您需要将编组与com.sun.xml.internal.bind.marshaller.DumbEscapeHandler
合并。来自JavaDoc
对US-ASCII码范围以上的所有代码进行转义。后备位置。适用于任何JDK,任何编码。
简单示例使用方法:
import com.sun.xml.internal.bind.marshaller.DataWriter;
import com.sun.xml.internal.bind.marshaller.DumbEscapeHandler;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
import java.io.PrintWriter;
public class JaxbApp {
public static void main(String[] args) throws Exception {
FreeTextNode dataFile = new FreeTextNode();
dataFile.setValue("hello i have < b > bold < / b > text inside.");
JAXBContext jaxbContext = JAXBContext.newInstance(FreeTextNode.class);
Marshaller marshaller = jaxbContext.createMarshaller();
PrintWriter printWriter = new PrintWriter(System.out);
DataWriter dataWriter = new DataWriter(printWriter, "UTF-8", DumbEscapeHandler.theInstance);
marshaller.marshal(dataFile, dataWriter);
}
}
@XmlRootElement(name = "freetextnode")
class FreeTextNode {
private String value;
@XmlValue
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
上面的代码打印:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<freetextnode>hello i have < b > bold < / b > text inside.</freetextnode>
另请参阅:
https://stackoverflow.com/questions/55280620
复制相似问题