使用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
最满意答案
这很有效,但它非常不直观。 赋值给你的属性(实际上是一个索引器)必须包含某种布尔值,它会被忽略和丢弃:
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 existingContains()
andAdd()
methods.
相关问答
更多-
C#数组和属性(C# Arrays & Properties)[2024-01-25]
您可以使用索引器定义“视图”类,该索引器基本上是带参数的属性: 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 ...
-
用于存储除C#中的Dictionary以外的对象对的数据类型(Data type for storing object pair other than Dictionary in C#)[2023-07-08]
我认为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, ...
-
存储在Keychain中后,OAuth令牌返回null属性(OAuth token returning with null properties after storing in Keychain)[2022-03-13]
是的,钥匙串是存储令牌的正确位置,因为它将被加密。 我尝试了与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暗示:这 ...