首页 \ 问答 \ 使用XStream读取XML(Read XML using XStream)

使用XStream读取XML(Read XML using XStream)

如何编写java类来读取这个XMl文件

<Response> 
<Models Make="SAMSUNG"> 
        <ModelNo>AB0-12343</ModelNo> 
        <ModelNo>BB1232222</ModelNo> 
    </Models>
     &t;Models Make="PANASONIC"> 
        <ModelNo>AB0-12343</ModelNo>
        <ModelNo>BB1232222</ModelNo> 
    </Models> 
</Response>

我试过这样的

@XStreamAlias("Response")
public class SL_LookupModelsResponse  {
    private ArrayList<Models> Models;

    public ArrayList<Models> getModels() {
        return Models;
    }

    public void setModels(ArrayList<Models> models) {
        Models = models;
    }

    @Override
    public String toString() {
        return "SL_LookupModelsResponse [Models=" + Models + "]";
    }

    @XStreamAlias("Models")
    class Models{
        private ArrayList<String> ModelNo;

        @XStreamAsAttribute  
        private String Make;  

        public ArrayList<String> getModelNos() {
            return ModelNo;
        }

        public void setModelNos(ArrayList<String> ModelNo) {
            this.ModelNo = ModelNo;
        }

        public String getMake() {  
            return Make;  
        }  

        public void setMake(String make) {  
            this.Make = make;  
        }  

        @Override
        public String toString() {
            return "Models [ModelNo=" + ModelNo + "Make="+ Make +"]";
        }

    }
  }

但我得到这个错误

Exception in thread "main" com.thoughtworks.xstream.converters.ConversionException: ModelNo : ModelNo
---- Debugging information ----
message             : ModelNo
cause-exception     : com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message       : ModelNo
class               : java.util.ArrayList
required-type       : java.util.ArrayList
converter-type      : com.thoughtworks.xstream.converters.collections.CollectionConverter
path                : /Response/Models/ModelNo
class[1]            : com.samples.SL_LookupModelsResponse
converter-type[1]   : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
version             : null

请帮我解决这个问题......

谢谢..


How to write java class to read this XMl file

<Response> 
<Models Make="SAMSUNG"> 
        <ModelNo>AB0-12343</ModelNo> 
        <ModelNo>BB1232222</ModelNo> 
    </Models>
     <Models Make="PANASONIC"> 
        <ModelNo>AB0-12343</ModelNo>
        <ModelNo>BB1232222</ModelNo> 
    </Models> 
</Response>

I tried like this

@XStreamAlias("Response")
public class SL_LookupModelsResponse  {
    private ArrayList<Models> Models;

    public ArrayList<Models> getModels() {
        return Models;
    }

    public void setModels(ArrayList<Models> models) {
        Models = models;
    }

    @Override
    public String toString() {
        return "SL_LookupModelsResponse [Models=" + Models + "]";
    }

    @XStreamAlias("Models")
    class Models{
        private ArrayList<String> ModelNo;

        @XStreamAsAttribute  
        private String Make;  

        public ArrayList<String> getModelNos() {
            return ModelNo;
        }

        public void setModelNos(ArrayList<String> ModelNo) {
            this.ModelNo = ModelNo;
        }

        public String getMake() {  
            return Make;  
        }  

        public void setMake(String make) {  
            this.Make = make;  
        }  

        @Override
        public String toString() {
            return "Models [ModelNo=" + ModelNo + "Make="+ Make +"]";
        }

    }
  }

But Im getting this error

Exception in thread "main" com.thoughtworks.xstream.converters.ConversionException: ModelNo : ModelNo
---- Debugging information ----
message             : ModelNo
cause-exception     : com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message       : ModelNo
class               : java.util.ArrayList
required-type       : java.util.ArrayList
converter-type      : com.thoughtworks.xstream.converters.collections.CollectionConverter
path                : /Response/Models/ModelNo
class[1]            : com.samples.SL_LookupModelsResponse
converter-type[1]   : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
version             : null

Please help me on how to solve this...

Thank you..


原文:https://stackoverflow.com/questions/18610197
更新时间:2022-10-11 18:10

最满意答案

这很有效,但它非常不直观。 赋值给你的属性(实际上是一个索引器)必须包含某种布尔值,它会被忽略和丢弃:

Attribute a = new Attribute();
a["test"] = true;                 // Adds "test" as expected -- but WTF does the true mean?
a["foo"] = false;                 // Adds "foo" -- the false means nothing
Console.Out.WriteLine(a["test"]); // returns true

所以是的,你可以做到这一点。 但这是一个糟糕的主意,因为没有人维护代码就会知道发生了什么。 那个悬而未决的布尔看起来像它有某种意义,但它不; 它违反了最小惊讶原则时,分配true与分配false相同的事情! 更不用说,您作为“索引”传入的价值实际上并不是任何东西的索引。 这是合法的,但是以与预期目的完全不同的方式使用语法。

诚实地说,直接访问List<string>并调用其现有的Contains()Add()方法会更好。


This works, but it's horrendously counter-intuitive. Assignments to your property (which is actually an indexer) would have to contain some kind of Boolean value, which gets ignored and thrown away:

Attribute a = new Attribute();
a["test"] = true;                 // Adds "test" as expected -- but WTF does the true mean?
a["foo"] = false;                 // Adds "foo" -- the false means nothing
Console.Out.WriteLine(a["test"]); // returns true

So yes, you can do this. But it's a bad idea, because no one maintaining the code will have a clue what's going on. That dangling Boolean looks like it has some kind of meaning, but it doesn't; it violates the Principle of Least Astonishment when assigning true does the same thing as assigning false! Not to mention, the value you pass in as an "index" really isn't an index into anything at all. It's legal, but using syntax in a completely different way from what it's intended for.

In all honesty you'd be better served just accessing the List<string> directly and calling its existing Contains() and Add() methods.

相关问答

更多
  • 您可以使用索引器定义“视图”类,该索引器基本上是带参数的属性: private Terrain[,,] rawArray = ...; private View transformedArray = new View(rawArray); private class View { private Terrain[,,] array; public View(Terrain[,,] array) { this.array = array; } pu ...
  • 杰夫不喜欢物业的原因是因为他们看起来像领域 - 所以不了解差异的开发人员会把它们视为田野,假设他们执行得很便宜。 就个人而言,我不同意这一点 - 我发现属性使得客户端代码比等效的方法调用更简单。 我同意开发人员需要知道的是,这些属性基本上是伪装的方法 - 但是我认为教育开发人员比使用方法更难阅读代码。 (特别是,在同一个语句中看到有几个getter和setter被看到的Java代码,我知道等同的C#代码将会变得更简单,Demeter的法则在理论上是非常好的,但有时候是foo.Name.Length真的是正确 ...
  • 你没有在上面的代码中写任何属性 - 你已经声明了字段 。 get和set的确会产生非常显着的区别:) 如果您想获取字段,只需使用GetFields - 但我建议您改为使用它们的属性。 属性在绑定等方面效果更好,并且通常是封装更好的开始 。 You haven't written any properties in the code above - you've declared fields. The get and set does indeed make a very significant diffe ...
  • 不,这不行。 想一想 - 你的吸气者和制定者真的在做些什么。 你不能在不完全改变语义的情况下绕过它们,我不会指望一个setter只是因为我改变了getter返回的内容而运行。 你真的必须要有可变的结构吗? 你会发现各种角落的情况和怪异。 为什么不让它们成为不可变的,并写道: myTransform.Position = myTransform.Position.OffsetBy(3.0f, 0f, 0f); 要么 myTransform.OffSetPosition(3.0f, 0f, 0f); 这样可 ...
  • 你所要做的就是让WDay拥有所有日子的属性: public class WDay { public Day Monday {get;set;} ... 然后让Day类具有TempHiF属性,依此类推: public class Day { public string TempHif {get;set;} ... } 确保WDay的构造函数使用新实例初始化其所有Day属性以避免空引用异常。 All you have to do is make WDay have prope ...
  • 我认为List>在这里最适合。 List Tuple I think a List> would work best here. List Tuple
  • 这很有效,但它非常不直观。 赋值给你的属性(实际上是一个索引器)必须包含某种布尔值,它会被忽略和丢弃: Attribute a = new Attribute(); a["test"] = true; // Adds "test" as expected -- but WTF does the true mean? a["foo"] = false; // Adds "foo" -- the false means nothing Consol ...
  • 编辑完全误解了要求。 如果您尝试在运行时创建此设置,请稍后再次阅读,以下是代码更改: //ensures no runtime errors if you try and view the setting before its created private bool _customSettingExists = false; public Form1() { InitializeComponent(); } private void radButton1_Click(object sender, ...
  • 是的,钥匙串是存储令牌的正确位置,因为它将被加密。 我尝试了与Podio和JNKeychain完全相同的事情,对我来说它确实有效。 您使用的是哪个版本的PodioKit和JNKeychain? 似乎unarchiving无法在您的情况下恢复这些属性值。 PKOAuth2Token *token = [[PKOAuth2Token alloc] initWithAccessToken:@"access1234" ...
  • 不,有。 公共API上的类型必须是显式的。 唯一不明确的是var ,它仅限于方法变量。 此外,您无法更改C#中的签名(在子类中添加公共getter) - 您必须重新声明它: // base type protected string Foo {get;set;} // derived type new public string Foo { get { return base.Foo; } protected set { base.Foo = value; } } 但正如new暗示:这 ...

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)