1

XStream 1.4.4 を使用します。

次の XML があります。

<app name="MyApp">
    <properties>
        <property name="fizz" value="buzz" />
        <property name="foo" value="bar" />
    </properties>

    <fruit type="apple" />

    <!-- ...etc. -->
</app>

プロパティのリストとプロパティ自体のそれぞれの POJO:

@XStreamAlias("properties")
public class Properties {
    private List<Property> properties = new ArrayList<Property>();

    public List<Property> getProperties() {
        return properties;
    }

    public void setProperties(List<Property> properties) {
        this.properties = properties;
    }
}

@XStreamAlias("property")
public class Property {
    private String name = null;

    private String value = null;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

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

次のコードを実行しようとすると:

XStream xs = new XStream();
Strnig xml = getXML(); // Fetches the string of XML from above
App app = (App)xs.fromXML(xml);

私は得る:

Exception in thread "main" com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$UnknownFieldException: No such field com.me.myapp.Properties.property
---- Debugging information ----
field               : property
class               : com.me.myapp.Properties
required-type       : com.me.myapp.Properties
converter-type      : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path                : /app/properties/property
line number         : 4
class[1]            : com.me.myapp.App
version             : null
-------------------------------
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.determineType(AbstractReflectionConverter.java:453)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:294)
    ...rest of stack trace omitted for brevity

どこが間違っていますか?

4

4 に答える 4

3

注釈はまったく必要ありません。エイリアスProperty.classを「プロパティ」として使用するだけで、XStream が他のすべての処理を行います。

于 2013-07-10T13:37:28.003 に答える
0

問題

  • com.me.myapp.Propertiesにエイリアスされたクラスがありますproperties
  • その中に、というインスタンス変数がありますproperties
  • それは2つのレベルですproperties
  • あなたのXMLには1つのレベルがありますproperties

私のおすすめ

  • 宣言のすぐ上に注釈を付けます。

    @XStreamImplicit(itemFieldName="property")
    private List<Property> properties = new ArrayList<Property>()
    
  • <properties>これは、リストをラップする要素 (暗黙のリスト)を省略し<property>、リスト内の各項目に要素を使用することを示しています。

テスト:

  • 正しい設定ができたと思ったら、まず XML を生成してテストします。

    XStream xs = new XStream();
    App app = ...;              // Construct & populate a new App object
    String xml = xs.toXML(app); // Convert to XML
    System.out.println(xml);
    // This shows the XML format that must be used to parse XML to App.
    
于 2013-07-04T02:25:49.467 に答える
0

このコードは、XML (指定) を App オブジェクトに変換しています。与えられたコードが不十分だったので、このコード全体をここに貼り付けます。私はあなたが言及するのを忘れていると思いますxstream.processAnnotations(App.class);

<app name="MyApp">
    <properties>
        <property name="fizz" value="buzz" />
        <property name="foo" value="bar" />
    </properties>
</app>

クラス

package com.xstream;

import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("app")
public class App {

  @XStreamAlias("properties")
  private Properties properties;

  /**
   * @param properties
   */
  public App(Properties properties) {
    super();
    this.properties = properties;
  }

  /**
   * 
   */
  public App() {
    super();
    // TODO Auto-generated constructor stub
  }

  /**
   * @return the properties
   */
  public Properties getProperties() {
    return properties;
  }

  /**
   * @param properties the properties to set
   */
  public void setProperties(Properties properties) {
    this.properties = properties;
  }

  /* (non-Javadoc)
   * @see java.lang.Object#toString()
   */
  @Override
  public String toString() {
    return "App [properties=" + properties + "]";
  }

}


----------

package com.xstream;                                                                                                   

import java.util.ArrayList;                                                                                            
import java.util.List;                                                                                                 

import com.thoughtworks.xstream.annotations.XStreamAlias;                                                              
import com.thoughtworks.xstream.annotations.XStreamImplicit;                                                           

@XStreamAlias("properties")                                                                                            
public class Properties {                                                                                              

    @XStreamImplicit(itemFieldName = "property")                                                                       
    private List<Property> property = new ArrayList<Property>();                                                       

    public List<Property> getProperties() {                                                                            
        return property;                                                                                               
    }                                                                                                                  

    public void setProperties(List<Property> properties) {                                                             
        this.property = properties;                                                                                    
    }                                                                                                                  

    /**                                                                                                                
     * @return the property                                                                                            
     */                                                                                                                
    public List<Property> getProperty() {                                                                              
      return property;                                                                                                 
    }                                                                                                                  

    /**                                                                                                                
     * @param property the property to set                                                                             
     */                                                                                                                
    public void setProperty(List<Property> property) {                                                                 
      this.property = property;                                                                                        
    }                                                                                                                  

    /* (non-Javadoc)                                                                                                   
     * @see java.lang.Object#toString()                                                                                
     */                                                                                                                
    @Override                                                                                                          
    public String toString() {                                                                                         
      return "Properties [property=" + property + "]";                                                                 
    }                                                                                                                  


}                                                                                                                      
----------------

package com.xstream;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

@XStreamAlias("property")
public class Property {

  @XStreamAsAttribute
    private String name = null;

  @XStreamAsAttribute
    private String value = null;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

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

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
      return "Property [name=" + name + ", value=" + value + "]";
    }


}

メインクラスのコードは.

XStream xstream = new XStream();
xstream.processAnnotations(App.class);

App app = null;
try {
  app = (App) xstream.fromXML(new FileInputStream(new File("D:/property.xml")));
}
catch (FileNotFoundException e) {
  e.printStackTrace();
}

System.out.println(app.toString());  
于 2013-07-10T10:04:45.233 に答える