Rory's Blog
Happy study,Happy work,Happy life |
Hello, I am not sure about the subject but here is what I needed help for: XML: <field name="value">I am a Field.</field> I have already tried several structures and nothing seem to work. Is this possible for XStream? :) How is the Java class form to support this? Thanks!
有人回答是看Converter的文檔。果然找到答案了。
自己寫一個converter就可以了。
下面是我的converterpackage?com.jdkcn.xstream;
import?java.util.ArrayList;
import?java.util.List;
import?com.jdkcn.yupoo.YupooPhoto;
import?com.jdkcn.yupoo.YupooResult;
import?com.thoughtworks.xstream.converters.Converter;
import?com.thoughtworks.xstream.converters.MarshallingContext;
import?com.thoughtworks.xstream.converters.UnmarshallingContext;
import?com.thoughtworks.xstream.io.HierarchicalStreamReader;
import?com.thoughtworks.xstream.io.HierarchicalStreamWriter;
/**
?*?@author?<a?href="mailto:rory.cn@gmail.com">somebody</a>
?*?@since?Jan?16,?2007?6:12:35?PM
?*?@version?$Id?YupooResultConverter.java$
?*/
public?class?YupooResultConverter?implements?Converter?{
????/*?(non-Javadoc)
?????*?@see?com.thoughtworks.xstream.converters.Converter#marshal(java.lang.Object,?com.thoughtworks.xstream.io.HierarchicalStreamWriter,?com.thoughtworks.xstream.converters.MarshallingContext)
?????*/
????public?void?marshal(Object?obj,?HierarchicalStreamWriter?writer,?MarshallingContext?context)?{
????????//?FIXME?unfinish.
????}
????/*?(non-Javadoc)
?????*?@see?com.thoughtworks.xstream.converters.Converter#unmarshal(com.thoughtworks.xstream.io.HierarchicalStreamReader,?com.thoughtworks.xstream.converters.UnmarshallingContext)
?????*/
????public?Object?unmarshal(HierarchicalStreamReader?reader,?UnmarshallingContext?context)?{
????????YupooResult?result?=?new?YupooResult();
????????result.setPage(new?Integer(reader.getAttribute("page")));
????????result.setPages(new?Integer(reader.getAttribute("pages")));
????????result.setPerpage(new?Integer(reader.getAttribute("perpage")));
????????result.setTotal(new?Integer(reader.getAttribute("total")));
????????reader.moveDown();
????????List<YupooPhoto>?photos?=?new?ArrayList<YupooPhoto>();
????????while(reader.hasMoreChildren())?{
????????????reader.moveDown();
????????????YupooPhoto?photo?=?new?YupooPhoto();
????????????photo.setDir(reader.getAttribute("dir"));
????????????photo.setFilename(reader.getAttribute("filename"));
????????????photo.setHost(reader.getAttribute("host"));
????????????photo.setId(reader.getAttribute("id"));
????????????photo.setOwner(reader.getAttribute("owner"));
????????????photo.setTitle(reader.getAttribute("title"));
????????????photos.add(photo);
????????????reader.moveUp();
????????}
????????result.setPhotos(photos);
????????return?result;
????}
????/*?(non-Javadoc)
?????*?@see?com.thoughtworks.xstream.converters.ConverterMatcher#canConvert(java.lang.Class)
?????*/
????public?boolean?canConvert(Class?clazz)?{
????????return?clazz.equals(YupooResult.class);
????}
}
然后調用的地方修改一下就ok了。XStream?stream?=?new?XStream();
stream.registerConverter(new?YupooResultConverter());
stream.alias("result",?YupooResult.class);
參考:
http://xstream.codehaus.org/converter-tutorial.html
public void useAttributeFor(java.lang.String?fieldName, java.lang.Class?type)
fieldName
- the name of the field
type
- the Class of the type to be rendered as XML attribute
XStream.InitializationException
- if no AttributeMapper
is available
public void useAttributeFor(java.lang.Class?type)
type
- the Class of the type to be rendered as XML attribute
XStream.InitializationException
- if no AttributeMapper
is available
? 上周更新了一下myblog,添加了一個Filter,做統計訪問用。可是后來發現出現亂碼問題了。找了很久都沒有找到問題。debug的時候看到 CharacterEncodingFilter確實是執行了。不過就是沒有效果。執行之前是ISO-8859-1編碼的,執行之后還是, CharacterEncodingFilter就沒有起到作用。后來終于找到問題的原因了。原來是Filter配置先后順序的原因。
?????? 剛開始的配置是這樣的:
? 先經過那個統計的filter然后再經過編碼的filter。這樣的話編碼的filter就不起作用了。只要吧編碼的filter放到最前面就沒有問題了。改成這樣就好。
以后大家一定要注意啊。順序問題也是很重要的。?? 除經特別注明外,本文章版權歸莫多泡泡所有.
署名,非商業用途,保持一致.???somebody(莫多)
| |||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
---|---|---|---|---|---|---|---|---|---|
26 | 27 | 28 | 29 | 30 | 1 | 2 | |||
3 | 4 | 5 | 6 | 7 | 8 | 9 | |||
10 | 11 | 12 | 13 | 14 | 15 | 16 | |||
17 | 18 | 19 | 20 | 21 | 22 | 23 | |||
24 | 25 | 26 | 27 | 28 | 29 | 30 | |||
31 | 1 | 2 | 3 | 4 | 5 | 6 |