如何从默认属性更改xml属性?

时间:2019-03-16 11:03:45

标签: java xml jaxb marshalling unmarshalling

enter image description here

上面的屏幕截图显示了预期的输出和实际的输出,红色表示它与绿色显示的实际输出不同。

为了创建xml文档,我使用了marshall概念。下面给出了用于创建xml文档的Java代码。

 import com.ehf.bean.Invoice;
 import com.sap._0050089212_one_off.ypt74nkey_.StandardFaultMessage;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Marshaller;
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.transform.TransformerException;
 import org.xml.sax.SAXException;

 public class Ehf {

 public static void main(String[] args) throws ParserConfigurationException, 
 TransformerException, SAXException, IOException, StandardFaultMessage, 
 com.sap.xi.a1s.global.StandardFaultMessage, JAXBException {

    JAXBContext contextObj = JAXBContext.newInstance(Invoice.class);

    Marshaller marshallerObj = contextObj.createMarshaller();
    marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    Invoice invoice = new Invoice();
    invoice.setCustomizationID("dsf");
    invoice.setInvoiceTypeCode(0);
    marshallerObj.marshal(invoice, new FileOutputStream("question.xml"));

}

}

注意:发票类是通过xjc命令使用xsd生成的。

如何解决此问题?

1 个答案:

答案 0 :(得分:0)

@javax.xml.bind.annotation.XmlSchema(
namespace = "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2", 
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED,
xmlns ={@XmlNs(prefix="cac", namespaceURI="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"), 
        @XmlNs(prefix="cbc", namespaceURI="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"),
        @XmlNs(prefix="", namespaceURI="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2")
       })

上面的代码按预期对我有用,此代码应写在 package-info.java