<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Rory's Blog
    Happy study,Happy work,Happy life
    posts - 22,  comments - 46,  trackbacks - 0
    xstream是個(gè)好東西。對(duì)于配置文件的讀取很方便。在mybog中我就用到了。不過(guò)今天打算用yupoo的api來(lái)做相冊(cè)。發(fā)現(xiàn)xstream對(duì)于xmlnode的attribute解析支持不是那么的好。
    對(duì)于這種節(jié)點(diǎn)格式的非常的簡(jiǎn)單
    <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>

    簡(jiǎn)單的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的結(jié)果是這樣的
    <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不到值了。沒(méi)法去mailist里面找答案,果然有人問(wèn)。
    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的文檔。果然找到答案了。
    自己寫一個(gè)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);
    ????}
    }

    然后調(diào)用的地方修改一下就ok了。
    XStream?stream?=?new?XStream();
    stream.registerConverter(
    new?YupooResultConverter());
    stream.alias(
    "result",?YupooResult.class);



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

    2007年1月18日更新。
    這里感謝網(wǎng)友 Ivan Chen(西濱)?的提示。原來(lái)新版的xstream可以簡(jiǎn)單的解決了。在1.2.1的doc里面找到了這個(gè)兩個(gè)方法。

    useAttributeFor

    public void useAttributeFor(java.lang.String?fieldName,
                                java.lang.Class?type)
    Use an XML attribute for a field or a specific type.

    Parameters:
    fieldName - the name of the field
    type - the Class of the type to be rendered as XML attribute
    Throws:
    XStream.InitializationException - if no AttributeMapper is available
    Since:
    1.2

    useAttributeFor

    public void useAttributeFor(java.lang.Class?type)
    Use an XML attribute for an arbotrary type.

    Parameters:
    type - the Class of the type to be rendered as XML attribute
    Throws:
    XStream.InitializationException - if no AttributeMapper is available
    Since:
    1.2

    這兩個(gè)方法都是從1.2開始支持的。
    也不用自己寫converter了。這樣就可以了
    ????????stream.alias("result",?YupooResult.class);
    ????????stream.useAttributeFor(
    "page",?Integer.class);
    ????????stream.useAttributeFor(
    "pages",?Integer.class);
    ????????stream.useAttributeFor(
    "perpage",?Integer.class);
    ????????stream.useAttributeFor(
    "total",?Integer.class);
    ????????stream.alias(
    "photo",?YupooPhoto.class);
    ????????stream.useAttributeFor(
    "id",?String.class);
    ????????stream.useAttributeFor(
    "owner",?String.class);
    ????????stream.useAttributeFor(
    "title",?String.class);
    ????????stream.useAttributeFor(
    "host",?String.class);
    ????????stream.useAttributeFor(
    "dir",?String.class);
    ????????stream.useAttributeFor(
    "filename",?String.class);

    創(chuàng)造共用協(xié)議:署名,非商業(yè),保持一致
    ?? 除經(jīng)特別注明外,本文章版權(quán)歸莫多泡泡所有.
    署名,非商業(yè)用途,保持一致.???somebody(莫多)
    posted on 2007-01-17 18:24 莫多 閱讀(7998) 評(píng)論(2)  編輯  收藏 所屬分類: Other

    FeedBack:
    # re: xstream對(duì)xmlnode的屬性(attribute)解析的問(wèn)題。
    2007-01-18 09:19 | Ivan Chen(西濱)
    新版的xstream不用那么麻煩了(說(shuō)實(shí)話,要自己寫一個(gè)converter倒不如直接用jdom好了)
    對(duì)于attribute,這樣寫
    stream.useAttributeFor("id",String.class);
    就可以。好像也沒(méi)有順序問(wèn)題?  回復(fù)  更多評(píng)論
      
    # re: xstream對(duì)xmlnode的屬性(attribute)解析的問(wèn)題。[未登錄](méi)
    2007-01-18 10:05 | 莫多
    哦。是么?趕緊下載一個(gè)新版試試。  回復(fù)  更多評(píng)論
      

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    常用鏈接

    留言簿(1)

    隨筆分類(27)

    隨筆檔案(22)

    Friends

    搜索

    •  

    積分與排名

    • 積分 - 62211
    • 排名 - 845

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 亚洲一区二区三区香蕉| 免费看又黄又无码的网站| 亚洲另类自拍丝袜第1页| AV在线播放日韩亚洲欧| 毛片大全免费观看| 91在线手机精品免费观看| 国产免费久久精品丫丫| 日韩亚洲人成在线综合| ASS亚洲熟妇毛茸茸PICS| 激情内射亚洲一区二区三区| 亚洲国产综合无码一区| 亚洲国产成人精品女人久久久| 女人18毛片水真多免费播放| 亚洲精品视频在线观看免费| 嫩草影院在线播放www免费观看| 日韩精品免费一线在线观看| 婷婷亚洲综合一区二区| 中文字幕无码亚洲欧洲日韩| 亚洲欧洲自拍拍偷午夜色| 亚洲高清在线观看| 亚洲香蕉网久久综合影视| 久久乐国产精品亚洲综合| 亚洲精品成a人在线观看| 国产国产人免费人成免费视频| 麻豆国产精品入口免费观看| 日韩精品成人无码专区免费| 69av免费视频| 在线观看免费人成视频色| 67194熟妇在线永久免费观看| 5555在线播放免费播放| 最近新韩国日本免费观看| 18禁止看的免费污网站| 很黄很黄的网站免费的| 成人免费一级毛片在线播放视频| 国产在线a免费观看| 国产v精品成人免费视频400条| 免费av欧美国产在钱| 国产精品免费一级在线观看| www.亚洲色图.com| 亚洲综合伊人久久综合| 亚洲国产精品成人久久|