Xstream XML to Java Object未显示xml标记文本

时间:2013-03-08 00:41:04

标签: xstream

Need help!

我有xml并使用Xstream将其转换为java对象,当我将java对象转换为xml并验证xml标签及其值时,我找不到值xml标签的文本:

XML :

<?xml version="1.0" encoding="UTF-8"?>
<record name="ncwtrial.xml" type="content">
  <item name="Root">
        <value>
          <item name="Main">
             <value>
                <item name="ContentID">
                 <value>007NMG</value>
                </item>
             </value>
           </item>
         </value>
    </item>
 </record>

//但在验证时得到此信息:(值不会到来)

<?xml version="1.0" encoding="UTF-8"?>
<record name="ncwtrial.xml" type="content">
  <item name="Root">
        <value>
          <item name="Main">
             <value>
                <item name="ContentID">
                 <value/>
                </item>
             </value>
           </item>
         </value>
    </item>
 </record>

//我的java类:

public static void main(String[] args) {
    // TODO Auto-generated method stub
    XStream xstream = new XStream(new DomDriver());

    xstream.alias("record",Record.class);
    xstream.alias("item",Item.class);
    xstream.alias("value",Value.class);



    // use attribute
    xstream.useAttributeFor("name", String.class);
    xstream.useAttributeFor("type", String.class);
    xstream.addImmutableType(Value.class);

    xstream.addImplicitCollection(Item.class, "value");
    xstream.addImplicitCollection(Value.class, "item");
   // xstream.aliasType("value", String.class);


    xstream.processAnnotations(Item.class);
    xstream.processAnnotations(Value.class);              


    Record record = (Record) xstream.fromXML(new File("C:/pankaj/temp/original.xml")); 

      // verifying Java object by converting it back to XML  
    System.out.println(xstream.toXML(record));
}

// Record.java

    /**
     * 
     */
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "item"
    })

    @XmlRootElement(name = "record")
    public class Record {

        @XmlAttribute(required = true)
        @XmlJavaTypeAdapter(CollapsedStringAdapter.class)

        @XStreamAlias(value="type")
        protected String type;

        @XmlAttribute(required = true)
        @XmlJavaTypeAdapter(NormalizedStringAdapter.class)
        @XStreamAlias(value="type")
        protected String name;

        @XStreamImplicit
        protected List<Item> item;

        /**
         * 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 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 item 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 item property.
         * 
         * <p>
         * For example, to add a new item, do as follows:
         * <pre>
         *    getItem().add(newItem);
         * </pre>
         * 
         * 
         * <p>
         * Objects of the following type(s) are allowed in the list
         * {@link Item }
         * 
         * 
         */
        public List<Item> getItem() {
            if (item == null) {
                item = new ArrayList<Item>();
            }
            return this.item;
        }

        @Override
        public String toString() {
            return "Record [type=" + type + ", name=" + name + ", item=" + item
                    + "]";
        }

    }

// Item.java

    /**
     * 
     */
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "item"
    })

    @XmlRootElement(name = "record")
    public class Item {

        @XmlAttribute(required = true)
        @XmlJavaTypeAdapter(NormalizedStringAdapter.class)
        @XStreamAlias(value="name")
        protected String name;


       /*@Override
        public String toString() {
            return "Item [name=" + name + ", value=" + value + "]";
        }*/


        @XStreamImplicit 
         protected List<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;
        }

        /**
         * Gets the value of the value 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 value property.
         * 
         * <p>
         * For example, to add a new item, do as follows:
         * <pre>
         *    getValue().add(newItem);
         * </pre>
         * 
         * 
         * <p>
         * Objects of the following type(s) are allowed in the list
         * {@link Value }
         * 
         */

       public List<Value> getValue() {
            if (value == null) {
                value = new ArrayList<Value>();
            }
             return this.value;
        }


    }

    // Value.java


    /**
     * 
     */
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "value"
    })

    @XmlRootElement(name = "item")
    public class Value {


        @XmlValue
        public String value;

        @XStreamImplicit(itemFieldName="item")
        protected List<Item> item;


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

        @Override
        public String toString() {
            return "Value [value=" + value + ", item=" + item + "]";
        }

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

        public List<Item> getItem() {
            if (item == null) {
                item = new ArrayList<Item>();
            }
            return this.item;
        }




    }

0 个答案:

没有答案