繁体   English   中英

难以将xml转换为Java对象

[英]Having difficulty to convert xml to java object

我正在使用XStream库。

xml服务的链接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”中却为空。

试试JAXB,这是一种标准的方法...!

请参阅链接www.javatpoint.com/jaxb-unmarshalling-example

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM