如何在jaxb marshaller

时间:2017-07-27 07:47:04

标签: java xml jaxb

我正在使用jdk1.8的spring boot。我可以使用jaxb xml将Java对象Marshall转换为XML,但我想在XML输出上添加DOCTYPE。

当前xml输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

我想要的是:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Example SYSTEM "example.dtd">

以下是我的JAXB

package com.example.demo.model;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;
import java.io.File;
import java.io.StringWriter;


 public class JAXBExample {

 public String  CreateXML() {

 PDXIReq pdxiReq = new PDXIReq(new Header("KO0haj000", "SS_ASED", 
     "CTY_IPC"), new Request("ACT_ACTION", 20131106, 111816, 
     9181626L, 0,1024, "1AJHSYQW36276354"));
    StringWriter sw = new StringWriter();

 try {
        JAXBContext jaxbContext = JAXBContext.newInstance(PDXIReq.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        // output pretty printed
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", 
      "\n<!DOCTYPE Example SYSTEM  \"example.dtd\">");
        jaxbMarshaller.marshal(pdxiReq, sw);


       } catch (JAXBException e) {
        e.printStackTrace();
      }
      return sw.toString();

   }
  }

1 个答案:

答案 0 :(得分:1)

当我再次跑步时,上面的帖子已经正确了。

JAXBContext jaxbContext = JAXBContext.newInstance(PDXIReq.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    // output pretty printed
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", 
  "\n<!DOCTYPE Example SYSTEM  \"example.dtd\">");