java - 将 xml 转换为 java 对象有困难

标签 java android xml

我正在使用 XStream 库。

xml服务的链接 http://webservices.nextbus.com/service/publicXMLFeed?command=routeConfig&a=ttc&r=54

我的类(class)......

   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”中我得到了 null。

最佳答案

尝试 JAXB。这是执行此操作的标准方法...!

引用链接www.javatpoint.com/jaxb-unmarshalling-example

关于java - 将 xml 转换为 java 对象有困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20558515/

相关文章:

python - 使用 python 从 SQL 表生成 XML

swift - 您将在 Swift for XML 中的何处包含替换事件?

java - 如何访问链表的元素?

java - Android,ImageView,不需要的填充

java - 为什么当我尝试在 Firebase 数据库中写入时出现此错误?与 FirebaseApp$IdTokenListener 相关

Android 共享首选项更改监听器即使使用监听器的全局引用也无法正常工作

android - Firebase 产品是否已为 100% 离线应用做好准备?

android - 为什么要在android中使用Path.Direction.CW和CCW?

python - 为什么有些项目没有在 Odoo 中翻译?

java - 为什么要避免使用 final 关键字?