简体   繁体   English

JAXB2 删除 XML 上的独立 =“是”

[英]JAXB2 Removing standalone="yes" on XML

I need to remove the standalone="yes" on my XML.我需要删除 XML 上的 Standalone="yes"。

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

I tried with this code:我试过这段代码:

    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE);            
marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\">");

The result on XML - Duplicate information: XML 上的结果 - 重复信息:

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

Does anyone know how to remove standalone="yes"?有谁知道如何删除standalone="yes"?

Marshall code:马歇尔代码:

        try {

        JAXBContext context = JAXBContext.newInstance(DataToXML.class);

        Marshaller marshaller = context.createMarshaller();

        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE);
        marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\">");

        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        StringWriter writer = new StringWriter();
        marshaller.marshal(xml, writer);

        System.out.println(writer.toString());
        
        marshaller.marshal(xml, new File("test.xml"));

    } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

XSLT transformation to the rescue. XSLT 转型救援。

It is possible to control almost any XML aspect via <xsl:output.../> clause attributes.通过<xsl:output.../>子句属性可以控制几乎任何 XML 方面。

One of them is standalone = 'yes' or 'no'其中之一是独立= 'yes' 或 'no'

XSLT XSLT

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" standalone="no" omit-xml-declaration="no" encoding="UTF-8" indent="yes" />
   <xsl:strip-space elements="*"/>

   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
   </xsl:template>
</xsl:stylesheet>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM