JAXB嵌套标记编组

时间:2019-03-29 19:38:27

标签: java xml xsd jaxb marshalling

我正在基于XML模板生成的类,使用JAXB创建XSD文件。

但是XML输出不是预期的。

public class ProcessXMLService {

    public static void main(String[] args) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance("com.ford.xml.poc");      

         StringBuilder xmlSB = new StringBuilder();
         try {
         FileWriter out = new FileWriter("C:\\gopal\\sales.xml");
         int count=0;
             while(count<2){         
                 StringWriter xmlStr = new StringWriter();
                 Marshaller marshaller = context.createMarshaller();             
                 marshaller.setProperty("jaxb.formatted.output",Boolean.FALSE);
                 marshaller.marshal(getMSGBody("DONE"), xmlStr);
                 xmlSB.append(xmlStr.toString()+"\n");
                 count++;
             }
         out.write(xmlSB.toString());
         out.close();
         }
         catch (Exception e) {
             e.printStackTrace();
         }

    }


    static JAXBElement<MessageBodyType> getMSGBody(String serviceName){
        ObjectFactory factory = new ObjectFactory();
        RequestInfoType reqInfoType = factory.createRequestInfoType();
        reqInfoType.setRequesterName("ETL Sales");
        reqInfoType.setRequestId("123");
        //JAXBElement<RequestInfoType> reqInfoJaxB = factory.createRequestInfo(reqInfoType);
        SalesDataType saleDataType = factory.createSalesDataType();
        saleDataType.setGlobalSourceRecordId("c0a460e0-60aa-4bd5-9155-5645b4afe1de");// Update GSRID
        //JAXBElement<SalesDataType> saleDataJaxB = factory.createSaleData(saleDataType);
        MessageBodyType msgBdType = factory.createMessageBodyType();
        msgBdType.setRequestInfo(reqInfoType);
        msgBdType.getSaleData().add(saleDataType);

        JAXBElement<MessageBodyType> msgBd = factory.createMessageBody(msgBdType);
        return msgBd;

    }
}

获取以下XML输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns4:MessageBody
    xmlns:ns2="urn:ford/identifier/v1.0"
    xmlns:ns4="urn:ford/Consumer/Sales/v1.0"
    xmlns:ns3="urn:ford/crfcommon/v1.0">
    <ns3:RequestInfo>
        <RequestId>123</RequestId>
        <RequesterName>ETL Sales</RequesterName>
    </ns3:RequestInfo>
    <SaleData>
        <ns2:GlobalSourceRecordId>c0a460e0-60aa-4bd5-9155-5645b4afe1de</ns2:GlobalSourceRecordId>
    </SaleData>
</ns4:MessageBody>

但是预期的XML输出是:

<?xml version="1.0" encoding="UTF-8"?>
<tns:MessageBody
    xmlns:tns="urn:ford/Consumer/Sales/v1.0">
    <com:RequestInfo
        xmlns:com="urn:ford/crfcommon/v1.0">
        <com:RequestId>123</com:RequestId>
        <com:RequesterName>ETL Sales</com:RequesterName>
    </com:RequestInfo>
    <tns:SaleData>
        <ns1:GlobalSourceRecordId
            xmlns:ns1="urn:ford/identifier/v1.0">c0a460e0-60aa-4bd5-9155-5645b4afe1de
        </ns1:GlobalSourceRecordId>
    </tns:SaleData>ns4:MessageBody>

0 个答案:

没有答案