Java类的字符串值

Java类的字符串值,java,xml,string,object,xsd,Java,Xml,String,Object,Xsd,我有以下xml格式: <Message name="DVP__CRDAP" type="IncomingMessage" structure="WSDvpRvpDetailedIncoming"> <Record name="WSHeaderIncoming"> <Field name="messageType">1</Field> <Field name="sessionId">2</Field> <Field na

我有以下xml格式:

<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>
* * * *列表中允许以下类型的对象 *{@link Message.Record} * * */ 公共列表getRecord(){ if(记录==null){ 记录=新的ArrayList(); } 返回此记录; } /** *获取name属性的值。 * *@返回 *可能的对象是 *{@link String} * */ 公共字符串getName(){ 返回名称; } /** *设置name属性的值。 * *@param值 *允许的对象是 *{@link String} * */ 公共void集合名(字符串值){ this.name=值; } /** *获取类型属性的值。 * *@返回 *可能的对象是 *{@link String} * */ 公共字符串getType(){ 返回类型; } /** *设置类型属性的值。 * *@param值 *允许的对象是 *{@link String} * */ 公共void集合类型(字符串值){ this.type=值; } /** *获取结构属性的值。 * *@返回 *可能的对象是 *{@link String} * */ 公共字符串getStructure(){ 回报结构; } /** *设置结构属性的值。 * *@param值 *允许的对象是 *{@link String} * */ 公共void集合结构(字符串值){ 这个结构=价值; } /** *匿名复杂类型的Java类。 * *以下架构片段指定此类中包含的预期内容。 * * * * * *列表中允许以下类型的对象 *{@link Message.Record.Field} * * */ 公共列表getField(){ 如果(字段==null){ 字段=新的ArrayList(); } 返回此.field; } /** *获取name属性的值。 * *@返回 *可能的对象是 *{@link String} * */ 公共字符串getName(){ 返回名称; } /** *设置name属性的值。 * *@param值 *允许的对象是 *{@link String} * */ 公共void集合名(字符串值){ this.name=值; } /** *匿名复杂类型的Java类。 * *以下架构片段指定此类中包含的预期内容。 * * *complexType> *simpleContent> *扩展基=”http://www.w3.org/2001/XMLSchema>字符串“> *属性名称=“名称”类型=”{http://www.w3.org/2001/XMLSchema}字符串“/> */分机> */simpleContent> */complexType> * * * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name=),比例={ “价值” }) 公共静态类字段{ @XmlValue 保护字符串值; @XmlAttribute(name=“name”) 受保护的字符串名称; /** *获取value属性的值。 * *@返回 *可能的对象是 *{@link String} * */ 公共字符串getValue(){ 返回值; } /** *设置值属性的值。 * *@param值 *允许的对象是 *{@link String} * */ 公共void设置值(字符串值){ 这个值=值; } /** *获取name属性的值。 * *@返回 *可能的对象是 *{@link String} * */ 公共字符串getName(){ 返回名称; } /** *设置name属性的值。 * *@param值 *允许的对象是 *{@link String} * */ 公共void集合名(字符串值){ this.name=值; } } } }
解决方案是使用
javax.xml
将字符串解封送到类:

对于将来的重用,您可以考虑一种通用方法,并将此方法转换为接受任何字符串并转换为任何类


或者,您可以查看XMLBeam,如中所述。在这种情况下,您不需要
消息
类(仅一个接口),也不需要使用
xjc

类生成类。您的意思是实例对吗

您需要使用marshaller。例如,我使用Jaxb2Marshaller(spring框架的一部分)

Catalog.Products.Product是由xjc生成的类

您还可以使用标准的JAXB解组器

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;
            }

        }

    }

}
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();
    }
}
@Bean
public Jaxb2Marshaller productMarshaller() {
    final Jaxb2Marshaller productMarshaller = new Jaxb2Marshaller();
    productMarshaller.setMappedClass(Catalog.Products.Product.class);
    productMarshaller.setClassesToBeBound(Catalog.Products.Product.class);
    return productMarshaller;
}
JAXBContext context = JAXBContext.newInstance(classessToBeBound);
Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.unmarshal(...);