在JAXB中从XML中删除前缀

时间:2017-07-25 13:12:52

标签: java xml xsd jaxb xml-namespaces

我尝试使用JAXB Annotations生成XML文件

所以,我将生成JAXB Classes& amp;来自XSD的package-info.java

让我们去:

1 - Package-info.java

 //
    // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.8-b130911.1802 
    // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
    // Any modifications to this file will be lost upon recompilation of the source schema. 
    // Generated on: 2017.01.05 at 01:51:40 PM CET  
    //

    @XmlSchema(
    xmlns = {
            @XmlNs(namespaceURI = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2",             prefix = "cac"),
            @XmlNs(namespaceURI = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2",                 prefix = "cbc"),
            @XmlNs(namespaceURI = "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2",                               prefix = "") //this must be empty prefix
    },

    elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)

    package com.audaxis.compiere.osg.ei.ubl2;
    import javax.xml.bind.annotation.XmlNs;
    import javax.xml.bind.annotation.XmlSchema;

2 - 然后我使用以下代码生成XML:

JAXBContext context = JAXBContext.newInstance(InvoiceType.class);
            Marshaller  m       = context.createMarshaller();

            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            m.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, TUEInvoiceConstants.UBLInvoiceShcemaLocation);

            // Write to File
            File f = new File(System.getProperty("java.io.tmpdir"), getOutputFileNameSimple(root));  
            m.marshal(root, f);

3 - 结果生成的XML:

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <Invoice 
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" 
    xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"  
    xmlns:ns11="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" 
    xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 UBL-Invoice-2.1.xsd">
    <cbc:UBLVersionID>2.1</cbc:UBLVersionID>
    <-- XML tags -->
    <END XML>

4 - 正如您所看到的,第三个命名空间是使用前缀= ns11创建的,这将在下一步中为我带来问题。

问题:如何让它生成XML 没有任何前缀 ??

0 个答案:

没有答案