首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java JAXB编组程序更改<但不是>的编码

Java JAXB编组程序更改<但不是>的编码
EN

Stack Overflow用户
提问于 2019-03-21 20:38:04
回答 1查看 674关注 0票数 0

拥有

代码语言:javascript
复制
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,我在里面有粗体文本。“在节点中

生成

代码语言:javascript
复制
<freetextnode>hello i have &lt; b > bold &lt; / b > text inside.</freetextnode>

期望值是:

代码语言:javascript
复制
<freetextnode>hello i have &lt; b &gt; bold &lt; / b &gt; text inside.</freetextnode>

JavaEE 7。

EN

回答 1

Stack Overflow用户

发布于 2019-03-22 07:28:40

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

对US-ASCII码范围以上的所有代码进行转义。后备位置。适用于任何JDK,任何编码。

简单示例使用方法:

代码语言:javascript
复制
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;
    }
}

上面的代码打印:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<freetextnode>hello i have &lt; b &gt; bold &lt; / b &gt; text inside.</freetextnode>

另请参阅:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55280620

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档