2012-11-12 14 views
0

我懷疑是否可以忽略轉換窗體上的xml根元素將對象轉換爲帶有XStream的XML,或者是否有任何方法可以用其他方法替換根元素意思是:用於XML轉換的XStream上的等效DROP_ROOT_MODE

我有一個函數來分析自定義運行時創建的對象到XML,是這樣的:

public static String entityToXML(GenericResponseObject entity) { 
    XStream xstream = new XStream(new StaxDriver()); 

    xstream.autodetectAnnotations(true); 
    xstream.registerConverter(new GenericResponseAttributeConverter()); 
    String xml = xstream.toXML(entity); 

    return xml; 
} 

對於這一點,我做了一個爛攤子:

我有GenericResponseObject和GenericResponseAttribute clases,這個想法是哈哈已經一個對象,具有多達需要在運行時自定義屬性:

@XStreamAlias("objectResult") 
public class GenericResponseObject { 

    @XStreamAlias("attributes") 
    @XStreamImplicit 
    private ArrayList<GenericResponseAttribute> attributes; 

    public GenericResponseObject() { 
     this.attributes = new ArrayList(); 
    } 

    public void addAttribute(String name, Object value) { 
     this.attributes.add(new GenericResponseAttribute(name, value)); 
    } 
} 

和類GenericResponseAttribute:

@XStreamAlias("attribute") 
public class GenericResponseAttribute { 

    private String name; 
    private Object value; 

    public GenericResponseAttribute(String name, Object value) { 
     this.name = name; 
     this.value = value; 
    } 

    public String getName() { 
     return this.name; 
    } 

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

    public Object getValue() { 
     return this.value; 
    } 

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

正如你可以看到,每類有混淆和隱性列出他們的XStream的註解,那麼,讓我告訴你定製轉換我對GenericResponseAttribute類所做的:

public class GenericResponseAttributeConverter implements Converter { 

    @Override 
    public boolean canConvert(Class type) { 
     return type.equals(GenericResponseAttribute.class); 
    } 

    @Override 
    public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContext mc) { 
     GenericResponseAttribute value = (GenericResponseAttribute)o; 
     writer.startNode(value.getName()); 
     mc.convertAnother(value.getValue()); 
     writer.endNode(); 
    } 

    @Override 
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext uc) { 
     throw new UnsupportedOperationException("Not supported yet."); 
    } 
} 

所以,如果我在運行時建立一個GenericResponseObject,並解析它與我的靜態方法XML,我得到的東西,如:

response = new GenericResponseObject(); 
response.addAttribute("user", "alex"); 
response.addAttribute("otherAtt", "TEST"); 
System.out.println(XMLUtil.entityToXML(response)); 

中的println()函數的結果是:

<objectResult> 
    <attribute> 
     <user>hugo</user> 
    </attribute> 
    <attribute> 
     <otherAtt>TEST</otherAtt> 
    </attribute> 
</objectResult> 

這是我想要差不多的東西,但我真的需要省略GenericResponseAttribute類的根元素,其重要的一點在於,對於我在上面顯示的根節點,總是隻存在一個節點,即屬性名稱與屬性值的內容。所以,這將是一個根元素始終如果我刪除當前的一個,例如,結果我需要的previos XML是:

<objectResult> 
    <user>hugo</user> 
    <otherAtt>TEST</otherAtt> 
</objectResult> 

我的需要是非常基本的,但我不知道如何執行它,我已經搜索,似乎沒有像HierarchicalStreamWriter類中的deletRootNode()或replaceRootNode()方法,並沒有@XStreamNoRoot或@XStreamMakeRoot註釋,我可以在GenericResponseAttribute中使用,所以,這就是爲什麼我要問在這裏,如果你知道如何做到這一點,請幫助我。

謝謝。

回答

2

通過的那一刻我通過解析對象到XML後的字符串替換結束了,我的意思是:

public static String entityToXML(Object entity) { 
    XStream xstream = new XStream(new StaxDriver()); 

    xstream.autodetectAnnotations(true); 
    xstream.registerConverter(new GenericResponseAttributeConverter()); 
    String xml = xstream.toXML(entity); 

    xml = xml.replace("<attribute>", ""); 
    xml = xml.replace("</attribute>", ""); 

    return xml; 
} 

但是,這不是一個很酷的解決方案,如果您有其它的請告訴我。

謝謝。

+2

+工作,但這將是糟糕的做到這一點在XML太大 –