xstream对xmlnode的属性(attribute)解析的问题。

xstream是个好东西。对于配置文件的读取很方便。在mybog中我就用到了。不过今天打算用yupoo的api来做相册。发现xstream对于xmlnode的attribute解析支持不是那么的好。
对于这种节点格式的非常的简单
< result >
    
< page > 1 </ page >
    
< pages > 1 </ pages >
    
< perpage > 100 </ perpage >
    
< total > 19 </ total >
    
< photos >
        
< photo >
            
< id > ff8080810fc8ac78010fd3f158d40a52 </ id >
            
< owner > ff8080810f1a387b010f1a83d6530dfc </ owner >
            
< title > Gmail-2 </ title >
            
< host > 4 </ host >
            
< dir > 20061230 </ dir >
            
< filename > 231905_1463411198 </ filename >
        
</ photo >
    
</ photos >
</ result >

简单的alias一下就可以读到值了
File file  =   new  File( " src/test/java/com/jdkcn/test/result.xml " );
BufferedReader reader 
=   new  BufferedReader( new  InputStreamReader( new  FileInputStream(file),  " UTF-8 " ));
XStream stream 
=   new  XStream();
stream.alias(
" result " , YupooResult. class );
stream.alias(
" photo " ,YupooPhoto. class );
YupooResult result 
=  (YupooResult)stream.fromXML(reader);
可是Yupoo的api返回的xmlrpc的结果是这样的
< result  page ="1"  pages ="1"  perpage ="100"  total ="19" >
    
< photos >
        
< photo  id ="ff8080810fc8ac78010fd3f158d40a52"
            owner
="ff8080810f1a387b010f1a83d6530dfc"  title ="Gmail-2"  host ="4"
            dir
="20061230"  filename ="231905_1463411198"   />
    
</ photos >
</ result >
这样就load不到值了。没法去mailist里面找答案,果然有人问。





有人回答是看Converter的文档。果然找到答案了。
自己写一个converter就可以了。
下面是我的converter
package  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 );


以后大家一定要注意啊。顺序问题也是很重要的。
创造共用协议:署名,非商业,保持一致   除经特别注明外,本文章版权归 莫多泡泡所有.
署名,非商业用途,保持一致.    somebody(莫多)























参考:
http://xstream.codehaus.org/converter-tutorial.html








94475.html

莫多 2007-01-17 18:24 发表评论
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值