2013-12-13 164 views
0

我正在使用XStream庫。 XML服務的難以將xml轉換爲java對象

鏈接 http://webservices.nextbus.com/service/publicXMLFeed?command=routeConfig&a=ttc&r=54

我的課表......

package com.example.myjakcontest; 

import java.util.List; 

import com.thoughtworks.xstream.annotations.XStreamAlias; 

public class Body { 

    @XStreamAlias("copyright") 
    private String _copyright; 
    private Route route; 

    public String get_copyright() { 
     return this._copyright; 
    } 

    public void set_copyright(String _copyright) { 
     this._copyright = _copyright; 
    } 

    public Route getRoute() { 
     return this.route; 
    } 

    public void setRoute(Route route) { 
     this.route = route; 
    } 
} 
package com.example.my**jakcontest;** 

    import java.util.List; 

    public class Direction{ 

    private String _branch; 
    private String _name; 
    private String _tag; 
    private String _title; 
    private String _useForUI; 
    private List<Stop> stop; 

    public String get_branch(){ 
     return this._branch; 
    } 
    public void set_branch(String _branch){ 
     this._branch = _branch; 
    } 
    public String get_name(){ 
     return this._name; 
    } 
    public void set_name(String _name){ 
     this._name = _name; 
    } 
    public String get_tag(){ 
     return this._tag; 
    } 
    public void set_tag(String _tag){ 
     this._tag = _tag; 
    } 
    public String get_title(){ 
     return this._title; 
    } 
    public void set_title(String _title){ 
     this._title = _title; 
    } 
    public String get_useForUI(){ 
     return this._useForUI; 
    } 
    public void set_useForUI(String _useForUI){ 
     this._useForUI = _useForUI; 
    } 
    public List<Stop> getStop(){ 
     return this.stop; 
    } 
    public void setStop(List<Stop> stop){ 
     this.stop = stop; 
    } 
} 

異步任務

XStream x = new XStream(); 
        x.alias("body", Body.class); 
        x.alias("stop", Stop.class); 
        x.alias("route", Route.class); 
        x.alias("direction", Direction.class); 
        x.alias("path", Path.class); 
        x.alias("point", Point.class); 

        x.addImplicitCollection(Route.class, "stop"); 
        x.addImplicitCollection(Route.class, "direction"); 
        x.addImplicitCollection(Route.class, "path"); 
        x.addImplicitCollection(Direction.class, "stop"); 
        x.addImplicitCollection(Path.class, "point"); 

        Body object = (Body) x.fromXML(httpResponse.getEntity() 
        .getContent()); 

        // Function converts XML to String 

        String xml = convertStreamToString(httpResponse.getEntity() 
          .getContent()); 

        Body b = (Body) x.fromXML(xml); 

我所有的類,但在對象 「B」 我我變得空了。

+0

請讓我知道,如果任何工具可在網上,可以從XML創建Java類。那太好了。 – user3093833

回答