在XML期间将ROOT元素名称更改为Java对象(JAXB)转换

时间:2018-03-30 16:35:09

标签: java xml xsd namespaces jaxb

我有一个供应商提供.xsd文件和库,可以处理XML格式的数据。

我无法修改XSD文件中的根元素名称,因为这些XSD文件会发生变化,从而导致维护开销。

示例XSD文件:

Bar

使用此架构生成的Java类(CUSTOMERV3Input.java):

isFoo

我使用以下代码将POJO转换为XML String:

<?xml version = '1.0' encoding = 'UTF-8'?>
<xsd:schema targetNamespace="http://xmlns.xxx.com/MyVendor/schemas/MyOperation" 
elementFormDefault="qualified" 
xmlns="http://xmlns.xcv.com/MyVendor/schemas/MyOperation" 
xmlns:op="http://www.xcv.com/schemas/MyOperation" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema">

   <xsd:element name="CUSTOMER_V3_input">
      <xsd:complexType>
         <xsd:sequence>
            <xsd:element name="ACCOUNT_NO" minOccurs="1" maxOccurs="1">
               <xsd:simpleType>
                  <xsd:restriction base="xsd:string">
                     <xsd:maxLength value="60"/>
                  </xsd:restriction>
               </xsd:simpleType>
            </xsd:element>
            <xsd:element name="EMAIL_ADDR" minOccurs="0" maxOccurs="1">
               <xsd:simpleType>
                  <xsd:restriction base="xsd:string">
                     <xsd:maxLength value="1023"/>
                  </xsd:restriction>
               </xsd:simpleType>
            </xsd:element>
            <xsd:element name="FIRST_NAME" minOccurs="0" maxOccurs="1">
               <xsd:simpleType>
                  <xsd:restriction base="xsd:string">
                     <xsd:maxLength value="90"/>
                  </xsd:restriction>
               </xsd:simpleType>
            </xsd:element>
            </xsd:element>
         </xsd:sequence>
      </xsd:complexType>
   </xsd:element>
</xsd:schema>

生成的XML数据是:

import java.io.Serializable;
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;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(
    name = "",
    propOrder = {"accountno", "emailaddr", "firstname"}
)
@XmlRootElement(
    name = "CUSTOMER_V3_input"
)
public class CUSTOMERV3Input implements Serializable {
    private static final long serialVersionUID = 1L;

    @XmlElement(
        name = "ACCOUNT_NO",
        required = true
    )
    protected String accountno;

    @XmlElement(
        name = "EMAIL_ADDR"
    )
    protected String emailaddr;

    @XmlElement(
        name = "FIRST_NAME"
    )
    protected String firstname;


    public CUSTOMERV3Input() {
    }

    public String getACCOUNTNO() {
        return this.accountno;
    }

    public void setACCOUNTNO(String value) {
        this.accountno = value;
    }

    public String getEMAILADDR() {
        return this.emailaddr;
    }

    public void setEMAILADDR(String value) {
        this.emailaddr = value;
    }

    public String getFIRSTNAME() {
        return this.firstname;
    }

    public void setFIRSTNAME(String value) {
        this.firstname = value;
    }
}

但是,库希望根标记代替

public static String jaxbObjectToXML(Object payload) {
    String xmlString = null;
    try {
        JAXBContext context = JAXBContext.newInstance(payload.getClass());
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); // To format XML
        StringWriter sw = new StringWriter();
        m.marshal(payload, sw);
        xmlString = sw.toString();
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return xmlString;
}

字符串 inputXmlString 应为:

<CUSTOMER_V3_input xmlns="http://xmlns.xxx.com/MyVendor/schemas/MyOperation">
    <ACCOUNT_NO>123147174283024</ACCOUNT_NO>
    <FIRST_NAME>Bharath</FIRST_NAME>
</CUSTOMER_V3_input>

OR

public String makeRequest(String inputXmlString, 
                           OperationType type, 
                           String transactionId) 
        throws XXXException {

返回的数据是:

<data>
    <ACCOUNT_NO>123147174283024</ACCOUNT_NO>
    <FIRST_NAME>Bharath</FIRST_NAME>
</data>

但要将其转换为java对象,我需要更改根元素名称和添加命名空间 xmlns =“http://xmlns.xxx.com/MyVendor/schemas/ MyOperation“

来自:<data xmlns="http://xmlns.xxx.com/MyVendor/schemas/MyOperation"> <ACCOUNT_NO>123147174283024</ACCOUNT_NO> <FIRST_NAME>Bharath</FIRST_NAME> </data> 收件人:<data> <ACCOUNT_NO>123147174283024</ACCOUNT_NO> <FIRST_NAME>Bharath</FIRST_NAME> <EMAIL_ADDR>retrieved@email.address.com</EMAIL_ADDR> </data>

<data>

这样我就可以将此XMLString传递给此方法以将其转换为Java Object

<CUSTOMER_V3_input xmlns="http://xmlns.xxx.com/MyVendor/schemas/MyOperation">

我目前正在使用String.replace()函数进行这些更改,我感觉不是正确的方法。

任何建议都会有所帮助。

0 个答案:

没有答案