Java JAXB 编码器更改 < 但不更改 > 的编码

标签 java jaxb tags

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();

有自由文本,例如“你好,我里面有粗体文本。”在节点

生成

<freetextnode>hello i have &lt; b > bold &lt; / b > text inside.</freetextnode>

期望是:

<freetextnode>hello i have &lt; b &gt; bold &lt; / b &gt; text inside.</freetextnode>

JavaEE 7。

最佳答案

您需要将编码与 com.sun.xml.internal.bind.marshaller.DumbEscapeHandler 合并。来自JavaDoc:

Escape everything above the US-ASCII code range. A fallback position. Works with any JDK, any encoding.

如何使用它的简单示例:

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 &lt; b &gt; bold &lt; / b &gt; text inside.</freetextnode>

另请参阅:

关于Java JAXB 编码器更改 < 但不更改 > 的编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55280620/

相关文章:

git - 使用 GIT for Windows 获取 OpenCV2.4.2

html - 通过CSS向右移动div标签

java - 如果在 JDK 1.7 JVM 中禁用 ECC,将使用什么加密?

java - 我需要从 Vert.x 的 SQLConnection 获取底层 java.sql.Connection

java - 有没有办法搜索项目中使用某种方法的每个地方?

java - 如何重构 XSD 以便解码不返回 JAXBElement

java - 如何在spring controller图片/视频中检测上传的文件类型

java - JPA 实体和 JAXB 条件序列化

java - 使用 eclipselink MOXy 的 XmlPath 映射问题

使用 Matlab 的 html 标签的正则表达式