2016-04-14 56 views
0

我有这样的XML格式:字符串值Java类

<Message name="DVP__CRDAP" type="IncomingMessage" structure="WSDvpRvpDetailedIncoming"> 
<Record name="WSHeaderIncoming"> 
<Field name="messageType">1</Field> 
<Field name="sessionId">2</Field> 
<Field name="instructionVersion">3</Field> 
<Field name="transactionDate">4</Field> 
<Field name="transactionTime">5</Field> 
<Field name="memberName">6</Field> 
<Field name="userName">7</Field> 
<Field name="cursorId">8</Field> 
<Field name="lineNo">9</Field> 
</Record> 
</Message> 

而且我已经转换到这个.xsd文件,然后使用XJC转换成名为.xsd类。

现在,如何将该字符串xml转换为我已经创建的Java类?

这里Message.java

import java.util.ArrayList; 
import java.util.List; 
import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.XmlType; 
import javax.xml.bind.annotation.XmlValue; 


/** 
* <p>Java class for anonymous complex type. 
* 
* <p>The following schema fragment specifies the expected content contained within this class. 
* 
* <pre> 
* &lt;complexType> 
* &lt;complexContent> 
*  &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
*  &lt;sequence> 
*   &lt;element name="Record" maxOccurs="unbounded" minOccurs="0"> 
*   &lt;complexType> 
*    &lt;complexContent> 
*    &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
*     &lt;sequence> 
*     &lt;element name="Field" maxOccurs="unbounded" minOccurs="0"> 
*      &lt;complexType> 
*      &lt;simpleContent> 
*       &lt;extension base="&lt;http://www.w3.org/2001/XMLSchema>string"> 
*       &lt;attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" /> 
*       &lt;/extension> 
*      &lt;/simpleContent> 
*      &lt;/complexType> 
*     &lt;/element> 
*     &lt;/sequence> 
*     &lt;attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" /> 
*    &lt;/restriction> 
*    &lt;/complexContent> 
*   &lt;/complexType> 
*   &lt;/element> 
*  &lt;/sequence> 
*  &lt;attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" /> 
*  &lt;attribute name="type" type="{http://www.w3.org/2001/XMLSchema}string" /> 
*  &lt;attribute name="structure" type="{http://www.w3.org/2001/XMLSchema}string" /> 
*  &lt;/restriction> 
* &lt;/complexContent> 
* &lt;/complexType> 
* </pre> 
* 
* 
*/ 
@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "record" 
}) 
@XmlRootElement(name = "Message") 
public class Message { 

    @XmlElement(name = "Record") 
    protected List<Message.Record> record; 
    @XmlAttribute(name = "name") 
    protected String name; 
    @XmlAttribute(name = "type") 
    protected String type; 
    @XmlAttribute(name = "structure") 
    protected String structure; 

    /** 
    * Gets the value of the record property. 
    * 
    * <p> 
    * This accessor method returns a reference to the live list, 
    * not a snapshot. Therefore any modification you make to the 
    * returned list will be present inside the JAXB object. 
    * This is why there is not a <CODE>set</CODE> method for the record property. 
    * 
    * <p> 
    * For example, to add a new item, do as follows: 
    * <pre> 
    * getRecord().add(newItem); 
    * </pre> 
    * 
    * 
    * <p> 
    * Objects of the following type(s) are allowed in the list 
    * {@link Message.Record } 
    * 
    * 
    */ 
    public List<Message.Record> getRecord() { 
     if (record == null) { 
      record = new ArrayList<Message.Record>(); 
     } 
     return this.record; 
    } 

    /** 
    * Gets the value of the name property. 
    * 
    * @return 
    *  possible object is 
    *  {@link String } 
    *  
    */ 
    public String getName() { 
     return name; 
    } 

    /** 
    * Sets the value of the name property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setName(String value) { 
     this.name = value; 
    } 

    /** 
    * Gets the value of the type property. 
    * 
    * @return 
    *  possible object is 
    *  {@link String } 
    *  
    */ 
    public String getType() { 
     return type; 
    } 

    /** 
    * Sets the value of the type property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setType(String value) { 
     this.type = value; 
    } 

    /** 
    * Gets the value of the structure property. 
    * 
    * @return 
    *  possible object is 
    *  {@link String } 
    *  
    */ 
    public String getStructure() { 
     return structure; 
    } 

    /** 
    * Sets the value of the structure property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setStructure(String value) { 
     this.structure = value; 
    } 


    /** 
    * <p>Java class for anonymous complex type. 
    * 
    * <p>The following schema fragment specifies the expected content contained within this class. 
    * 
    * <pre> 
    * &lt;complexType> 
    * &lt;complexContent> 
    *  &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
    *  &lt;sequence> 
    *   &lt;element name="Field" maxOccurs="unbounded" minOccurs="0"> 
    *   &lt;complexType> 
    *    &lt;simpleContent> 
    *    &lt;extension base="&lt;http://www.w3.org/2001/XMLSchema>string"> 
    *     &lt;attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" /> 
    *    &lt;/extension> 
    *    &lt;/simpleContent> 
    *   &lt;/complexType> 
    *   &lt;/element> 
    *  &lt;/sequence> 
    *  &lt;attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" /> 
    *  &lt;/restriction> 
    * &lt;/complexContent> 
    * &lt;/complexType> 
    * </pre> 
    * 
    * 
    */ 
    @XmlAccessorType(XmlAccessType.FIELD) 
    @XmlType(name = "", propOrder = { 
     "field" 
    }) 
    public static class Record { 

     @XmlElement(name = "Field") 
     protected List<Message.Record.Field> field; 
     @XmlAttribute(name = "name") 
     protected String name; 

     /** 
     * Gets the value of the field property. 
     * 
     * <p> 
     * This accessor method returns a reference to the live list, 
     * not a snapshot. Therefore any modification you make to the 
     * returned list will be present inside the JAXB object. 
     * This is why there is not a <CODE>set</CODE> method for the field property. 
     * 
     * <p> 
     * For example, to add a new item, do as follows: 
     * <pre> 
     * getField().add(newItem); 
     * </pre> 
     * 
     * 
     * <p> 
     * Objects of the following type(s) are allowed in the list 
     * {@link Message.Record.Field } 
     * 
     * 
     */ 
     public List<Message.Record.Field> getField() { 
      if (field == null) { 
       field = new ArrayList<Message.Record.Field>(); 
      } 
      return this.field; 
     } 

     /** 
     * Gets the value of the name property. 
     * 
     * @return 
     *  possible object is 
     *  {@link String } 
     *  
     */ 
     public String getName() { 
      return name; 
     } 

     /** 
     * Sets the value of the name property. 
     * 
     * @param value 
     *  allowed object is 
     *  {@link String } 
     *  
     */ 
     public void setName(String value) { 
      this.name = value; 
     } 


     /** 
     * <p>Java class for anonymous complex type. 
     * 
     * <p>The following schema fragment specifies the expected content contained within this class. 
     * 
     * <pre> 
     * &lt;complexType> 
     * &lt;simpleContent> 
     *  &lt;extension base="&lt;http://www.w3.org/2001/XMLSchema>string"> 
     *  &lt;attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" /> 
     *  &lt;/extension> 
     * &lt;/simpleContent> 
     * &lt;/complexType> 
     * </pre> 
     * 
     * 
     */ 
     @XmlAccessorType(XmlAccessType.FIELD) 
     @XmlType(name = "", propOrder = { 
      "value" 
     }) 
     public static class Field { 

      @XmlValue 
      protected String value; 
      @XmlAttribute(name = "name") 
      protected String name; 

      /** 
      * Gets the value of the value property. 
      * 
      * @return 
      *  possible object is 
      *  {@link String } 
      *  
      */ 
      public String getValue() { 
       return value; 
      } 

      /** 
      * Sets the value of the value property. 
      * 
      * @param value 
      *  allowed object is 
      *  {@link String } 
      *  
      */ 
      public void setValue(String value) { 
       this.value = value; 
      } 

      /** 
      * Gets the value of the name property. 
      * 
      * @return 
      *  possible object is 
      *  {@link String } 
      *  
      */ 
      public String getName() { 
       return name; 
      } 

      /** 
      * Sets the value of the name property. 
      * 
      * @param value 
      *  allowed object is 
      *  {@link String } 
      *  
      */ 
      public void setName(String value) { 
       this.name = value; 
      } 

     } 

    } 

} 

回答

0

一个解决办法是取消元帅的字符串到你的类javax.xml

public static Message unmarshal(final String content) throws JAXBException, 
     XMLStreamException, FactoryConfigurationError { 

    final JAXBContext jc = JAXBContext.newInstance(Message.class); 
    final Unmarshaller unmarshaller = jc.createUnmarshaller(); 
    final StringReader stringReader = new StringReader(content); 
    try { 
     final XMLStreamReader xmlReader = XMLInputFactory.newInstance().createXMLStreamReader(stringReader); 
     try { 
      return unmarshaller.unmarshal(xmlReader, Message.class).getValue(); 
     } finally { 
      xmlReader.close(); 
     } 
    } finally { 
     stringReader.close(); 
    } 
} 

对于未来重复使用,你可以想想通用的方法,并将此方法转换为接受任何字符串并转换为任何类。

或者,您可以看看像this answer中所述的XMLBeam。在这种情况下,你不需要你的Message类(只有一个接口),你不需要用xjc生成类。

0

按类你的意思是实例吗?你需要使用编组器。比如我使用Jaxb2Marshaller(Spring框架的一部分)。

@Bean 
public Jaxb2Marshaller productMarshaller() { 
    final Jaxb2Marshaller productMarshaller = new Jaxb2Marshaller(); 
    productMarshaller.setMappedClass(Catalog.Products.Product.class); 
    productMarshaller.setClassesToBeBound(Catalog.Products.Product.class); 
    return productMarshaller; 
} 

Catalog.Products.Product是由xjc生成的类。您也可以使用标准的JAXB解组器。

JAXBContext context = JAXBContext.newInstance(classessToBeBound); 
Unmarshaller unmarshaller = context.createUnmarshaller(); 
unmarshaller.unmarshal(...);