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

问题描述 投票:-1回答:1

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"));

}

}

注意:使用xsd,xjc命令生成Invoice类。

怎么解决这个问题?

java xml jaxb marshalling unmarshalling
1个回答
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中

© www.soinside.com 2019 - 2024. All rights reserved.