微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何向 XMLrootElement 添加更多标签

如何解决如何向 XMLrootElement 添加更多标签

我想用 JAXB 生成一个 xml 文件,我需要在请求中添加一个特定的标签 schemaVersion="10.0" schemaVersion="10.0" > ...
这是我生成xml文件代码

packege-info.java

@XmlSchema(namespace = "http://www.dhl.com/datatypes_global",location="datatypes_global_v10"   )
// elementFormDefault = XmlNsForm.QUALIFIEDc'est à dire que les @XmlElement seront pas prefixé
//// xmlns:req='http://www.dhl.com' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.dhl.com ship-val-req_AP.xsd'
package dhl.request.vo_10.document;
/**Il faut que ce fichier sera nommé package dhl.-info : décrit nomspace xml request*/

import javax.xml.bind.annotation.XmlSchema;

OprDocumentVO.java

package dhl.request.vo_10.document;

import javax.xml.bind.annotation.XmlAccesstype;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import dhl.request.vo_10.billing.OprBillingVO;
import dhl.request.vo_10.commodity.OprcommodityVO;
import dhl.request.vo_10.consignee.OprConsigneeVO;
import dhl.request.vo_10.document.exportDeclaration.OprExportDeclarationVO;
import dhl.request.vo_10.document.label.OprLabelVO;
import dhl.request.vo_10.dutiable.OprDutiableVO;
import dhl.request.vo_10.reference.OprReferenceVO;
import dhl.request.vo_10.request.OprRequestVO;
import dhl.request.vo_10.shipmentDetails.OprShipmentDetailsVO;
import dhl.request.vo_10.shipper.OprShipperVO;


@XmlAccessorType(XmlAccesstype.FIELD)
@XmlType(name="OprDocumentVO",propOrder = {
        "request","languageCode","billing","consignee","commodity","dutiable","reference","shipmentDetails","shipper","specialService","regionCode","requestedPickupTime","newShipper","usedhlInvoice","dhlInvoiceLanguageCode","dhlInvoiceType","exportDeclaration","numberOfArchiveDoc","requestArchiveDoc","labelImageFormat","eProcShip","label","getPriceEstimate","singlePieceImage"
        
        
 
                
})

 
 @XmlRootElement(name="ShipmentRequest"   )
 



public class OprDocumentVO 
{   
    
    public OprRequestVO getRequest() {
        return request;
    }

    public void setRequest(OprRequestVO request) {
        this.request = request;
    }

    public String getLanguageCode() {
        return languageCode;
    }

    public void setLanguageCode(String languageCode) {
        this.languageCode = languageCode;
    }
 

    public OprBillingVO getBilling() {
        return billing;
    }

    public void setBilling(OprBillingVO billing) {
        this.billing = billing;
    }
    
    public OprConsigneeVO getConsignee() {
        return consignee;
    }

    public void setConsignee(OprConsigneeVO consignee) {
        this.consignee = consignee;
    }

    public OprcommodityVO getcommodity() {
        return commodity;
    }

    public void setcommodity(OprcommodityVO commodity) {
        this.commodity = commodity;
    }

    public OprDutiableVO getDutiable() {
        return dutiable;
    }

    public void setDutiable(OprDutiableVO dutiable) {
        this.dutiable = dutiable;
    }

    public OprReferenceVO getReference() {
        return reference;
    }

    public void setReference(OprReferenceVO reference) {
        this.reference = reference;
    }

    public OprShipmentDetailsVO getShipmentDetails() {
        return shipmentDetails;
    }

    public void setShipmentDetails(OprShipmentDetailsVO shipmentDetails) {
        this.shipmentDetails = shipmentDetails;
    }

    public OprShipperVO getShipper() {
        return shipper;
    }

    public void setShipper(OprShipperVO shipper) {
        this.shipper = shipper;
    }
    
    
    @XmlElement(name = "GetPriceEstimate",nillable = false,required = true)
    private String getPriceEstimate;
    
    @XmlElement(name = "SinglePieceImage",required = true)
    private String singlePieceImage;
    
    
    
    
    
    
    
    @XmlElement(name = "LAbel",required = true)
    private OprLabelVO label;

 

    
    @XmlElement(name = "NumberOfArchiveDoc",required = true)
    private String numberOfArchiveDoc;
    
    @XmlElement(name = "RequestArchiveDoc",required = true)
    private String requestArchiveDoc;
    
    @XmlElement(name = "LabelImageFormat",required = true)
    private String labelImageFormat;
    
    @XmlElement(name = "EProcShip",required = true)
    private String eProcShip;
    
    
    @XmlElement(name = "ExportDeclaration",required = true)
    private OprExportDeclarationVO exportDeclaration;
    
    
    @XmlElement(name = "UsedhlInvoice",required = true)
    private String usedhlInvoice;

    @XmlElement(name = "dhlInvoiceLanguageCode",required = true)
    private String dhlInvoiceLanguageCode;
    
    @XmlElement(name = "dhlInvoiceType",required = true)
    private String dhlInvoiceType;
    
 
    

    @XmlElement(name = "Request",required = true)
    private OprRequestVO request;
    
    @XmlElement(name = "LanguageCode",required = true)
    private String languageCode;
    
 
    
    @XmlElement(name = "Billing",required = true)
    private OprBillingVO billing;
    
    @XmlElement(name = "Consignee",required = true)
    private OprConsigneeVO consignee;
    
    @XmlElement(name = "commodity",required = true)
    private OprcommodityVO commodity ;
    
    @XmlElement(name = "Dutiable",required = true)
    private OprDutiableVO dutiable ;
    
    @XmlElement(name = "Reference",required = true)
    private OprReferenceVO reference ;
    
    @XmlElement(name = "ShipmentDetails",required = true)
    private OprShipmentDetailsVO shipmentDetails ;
    
    @XmlElement(name = "Shipper",required = true)
    private OprShipperVO shipper ;
    
    @XmlElement(name = "SpecialService",required = true)
    private dhl.request.vo_10.document.specialService.OprSpecialServiceVO specialService ;

    @XmlElement(name = "RegionCode",required = true)
    private String regionCode;
    
    @XmlElement(name = "RequestedPickupTime",required = true)
    private String requestedPickupTime;
    
    @XmlElement(name = "NewShipper",required = true)
    private String newShipper;

    public String getRegionCode() {
        return regionCode;
    }

    public void setRegionCode(String regionCode) {
        this.regionCode = regionCode;
    }


 

    public OprExportDeclarationVO getExportDeclaration() {
        return exportDeclaration;
    }

    public void setExportDeclaration(OprExportDeclarationVO exportDeclaration) {
        this.exportDeclaration = exportDeclaration;
    }

    public dhl.request.vo_10.document.specialService.OprSpecialServiceVO getSpecialService() {
        return specialService;
    }

    public void setSpecialService(dhl.request.vo_10.document.specialService.OprSpecialServiceVO specialService) {
        this.specialService = specialService;
    }

    public String getNumberOfArchiveDoc() {
        return numberOfArchiveDoc;
    }

    public void setNumberOfArchiveDoc(String numberOfArchiveDoc) {
        this.numberOfArchiveDoc = numberOfArchiveDoc;
    }

    public String getRequestArchiveDoc() {
        return requestArchiveDoc;
    }

    public void setRequestArchiveDoc(String requestArchiveDoc) {
        this.requestArchiveDoc = requestArchiveDoc;
    }

    public String getLabelImageFormat() {
        return labelImageFormat;
    }

    public void setLabelImageFormat(String labelImageFormat) {
        this.labelImageFormat = labelImageFormat;
    }

    public String geteProcShip() {
        return eProcShip;
    }

    public void seteProcShip(String eProcShip) {
        this.eProcShip = eProcShip;
    }

    public String getGetPriceEstimate() {
        return getPriceEstimate;
    }

    public void setGetPriceEstimate(String getPriceEstimate) {
        this.getPriceEstimate = getPriceEstimate;
    }

    public String getSinglePieceImage() {
        return singlePieceImage;
    }

    public void setSinglePieceImage(String singlePieceImage) {
        this.singlePieceImage = singlePieceImage;
    }

    public OprLabelVO getLabel() {
        return label;
    }

    public void setLabel(OprLabelVO label) {
        this.label = label;
    }

    public String getRequestedPickupTime() {
        return requestedPickupTime;
    }

    public void setRequestedPickupTime(String requestedPickupTime) {
        this.requestedPickupTime = requestedPickupTime;
    }

    public String getNewShipper() {
        return newShipper;
    }

    public void setNewShipper(String newShipper) {
        this.newShipper = newShipper;
    }

    public String getUsedhlInvoice() {
        return usedhlInvoice;
    }

    public void setUsedhlInvoice(String usedhlInvoice) {
        this.usedhlInvoice = usedhlInvoice;
    }

    public String getdhlInvoiceLanguageCode() {
        return dhlInvoiceLanguageCode;
    }

    public void setdhlInvoiceLanguageCode(String dhlInvoiceLanguageCode) {
        this.dhlInvoiceLanguageCode = dhlInvoiceLanguageCode;
    }

    public String getdhlInvoiceType() {
        return dhlInvoiceType;
    }

    public void setdhlInvoiceType(String dhlInvoiceType) {
        this.dhlInvoiceType = dhlInvoiceType;
    }
    
 
 
 
}

获取这个xml文件

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<req:ShipmentRequest xmlns:req="http://www.dhl.com/datatypes_global">
    <Request>
        <ServiceHeader>
            <MessageTime>2021-05-17T11:29:06+00:00</MessageTime>
            <MessageReference>1234567890123456789012345678901</MessageReference>
            <SiteID>CIMAXMLPI</SiteID>
            <Password>3p7jW0tamq</Password>
        </ServiceHeader>
        <MetaData>
            <SoftwareName>software name</SoftwareName>
            <Softwareversion>1.10</Softwareversion>
        </MetaData>
    </Request>
    <LanguageCode>EN</LanguageCode>
    <Billing>
        <ShipperAccountNumber>359045226</ShipperAccountNumber>
        <ShippingPaymentType>S</ShippingPaymentType>
        <DutyPaymentType>R</DutyPaymentType>
    </Billing>
    <Consignee>
        <CompanyName>MOHAMED AIT OULID</CompanyName>
        <AddressLine>2717 ARLINGTON DRIVE APART N 102 AL</AddressLine>
        <AddressLine>ESCANDIRA VIRGINIA </AddressLine>
        <City>ALEXANDRI</City>
        <PostalCode>22306</PostalCode>
        <CountryCode>US</CountryCode>
        <CountryName>United States</CountryName>
        <Contact>
            <PersonName>MOHAMED AIT OULID </PersonName>
            <PhoneNumber>17034779922</PhoneNumber>
        </Contact>
        <BusinessPartyTypeCode>BUSInesSPARTYTYPECODE</BusinessPartyTypeCode>
        <RegistrationNumbers>
            <RegistrationNumbers>
                <Number>NUMBER</Number>
                <NumberTypeCode>NUMBERTYPECODE</NumberTypeCode>
                <NumberIssuerCountryCode>NUMBERISSUERCOUNTRYCODE</NumberIssuerCountryCode>
            </RegistrationNumbers>
        </RegistrationNumbers>
    </Consignee>
    <Dutiable>
        <DeclaredValue>18.00</DeclaredValue>
        <DeclaredCurrency>MAD</DeclaredCurrency>
        <ShipperEIN>MA-BEN</ShipperEIN>
        <TermsOfTrade>DDU</TermsOfTrade>
        <Filing>
            <filingType>FILINGTYPE</filingType>
            <ITN>ITN</ITN>
        </Filing>
    </Dutiable>
    <Reference>
        <ReferenceID>46009000875384D</ReferenceID>
        <ReferenceType>ID</ReferenceType>
    </Reference>
    <ShipmentDetails>
        <CurrencyCode>MAD</CurrencyCode>
        <Pieces>
            <Piece>
                <Weight>2.33</Weight>
                <Depth>0</Depth>
                <Width>0</Width>
                <Height>0</Height>
                <PieceID>1</PieceID>
                <PackageType>EE</PackageType>
            </Piece>
        </Pieces>
        <PackageType>EE</PackageType>
        <Weight>2.376</Weight>
        <DimensionUnit>C</DimensionUnit>
        <WeightUnit>K</WeightUnit>
        <GlobalProductCode>P</GlobalProductCode>
        <LocalProductCode>P</LocalProductCode>
        <Date>2021-05-17</Date>
        <Contents>VETEMENTS ET BABOUCHES</Contents>
        <IsDutiable>Y</IsDutiable>
    </ShipmentDetails>
    <Shipper>
        <ShipperID>359045226</ShipperID>
        <CompanyName>LOTFI HAYAT</CompanyName>
        <AddressLine>HAY HANAA SOK HANA N 7 </AddressLine>
        <City>CASABLANCA </City>
        <PostalCode>20000</PostalCode>
        <CountryCode>MA</CountryCode>
        <CountryName>MOROCCO</CountryName>
        <Contact>
            <PersonName>LOTIF HAYAT </PersonName>
            <PhoneNumber>0614183031</PhoneNumber>
        </Contact>
    </Shipper>
    <SpecialService>
        <SpecialServiceType>DS</SpecialServiceType>
    </SpecialService>
    <RegionCode>REGION CODE</RegionCode>
    <RequestedPickupTime>N</RequestedPickupTime>
    <NewShipper>N</NewShipper>
    <UsedhlInvoice>Y</UsedhlInvoice>
    <dhlInvoiceLanguageCode>en</dhlInvoiceLanguageCode>
    <dhlInvoiceType>CMI</dhlInvoiceType>
    <ExportDeclaration>
        <ExportReason>sales</ExportReason>
        <ExportReasonCode>P</ExportReasonCode>
        <InvoiceNumber>MydhlAPI - INV-001</InvoiceNumber>
        <InvoiceDate>2021-04-29</InvoiceDate>
        <TermsOfPayment>60 days</TermsOfPayment>
        <PayerGSTVAT>yes</PayerGSTVAT>
        <ReceiverReference>ReceiverReference</ReceiverReference>
        <ExporterId>43244325</ExporterId>
        <ExporterCode>ExporterCode</ExporterCode>
        <InvoiceInstructions>This is invoice instruction</InvoiceInstructions>
        <PlaceOfIncoterm>GAILDORF PORT</PlaceOfIncoterm>
        <ShipmentPurpose>COMMERCIAL</ShipmentPurpose>
        <DocumentFunction>IMPORT</DocumentFunction>
        <InvoicetotalNetWeight>0.400</InvoicetotalNetWeight>
        <InvoicetotalGrossWeight>0.500</InvoicetotalGrossWeight>
        <CustomsDocuments>
            <CustomsDocument>
                <CustomsDocumentType>INV</CustomsDocumentType>
                <CustomsDocumentID>MydhlAPI - CUSDOC-001</CustomsDocumentID>
            </CustomsDocument>
        </CustomsDocuments>
        <ExportLineItem>
            <LineNumber>1</LineNumber>
            <Quantity>1</Quantity>
            <QuantityUnit>PCS</QuantityUnit>
            <Description>Fall collection: knitted vegetarian top white US 5</Description>
            <Value>56.525</Value>
            <commodityCode>6109.10.0010</commodityCode>
            <Weight>
                <Weight>0.200</Weight>
                <WeightUnit>K</WeightUnit>
            </Weight>
            <ManufactureCountryCode>MA</ManufactureCountryCode>
            <ImportcommodityCode>6109.10.0010</ImportcommodityCode>
            <ItemReferences>
                <ItemReference>
                    <ItemReferenceType>PAN</ItemReferenceType>
                    <ItemReferenceNumber>10597122</ItemReferenceNumber>
                </ItemReference>
            </ItemReferences>
        </ExportLineItem>
        <InvoiceReferences>
            <InvoiceReference>
                <InvoiceReferenceType>OID</InvoiceReferenceType>
                <InvoiceReferenceNumber>OID-7839749374</InvoiceReferenceNumber>
            </InvoiceReference>
        </InvoiceReferences>
    </ExportDeclaration>
    <NumberOfArchiveDoc>1</NumberOfArchiveDoc>
    <RequestArchiveDoc>Y</RequestArchiveDoc>
    <LabelImageFormat>PDF</LabelImageFormat>
    <LAbel>
        <HideAccount>N</HideAccount>
        <LabelTemplate>8X4_thermal</LabelTemplate>
        <logo>Y</logo>
    </LAbel>
    <GetPriceEstimate>N</GetPriceEstimate>
    <SinglePieceImage>N</SinglePieceImage>
</req:ShipmentRequest>

我使用 jaxB API 中的 marchal 和 unmarchal 函数将我所有的 java 对象转换为 xml 请求

感谢您的帮助

解决方法

解决方法很简单。 您需要在根类中添加一个 schemaVersion 属性 并用 @XmlAttribute 注释,而不是用 @XmlElement

@XmlAttribute(name = "schemaVersion")
private String schemaVersion;

// + getter and setter

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。