在使用XStream将XML读取到对象时,如果我不知道XML将包含多少个字段呢?(While using XStream to read XML to object, what if I don�

编程入门 行业动态 更新时间:2024-10-24 06:38:29
在使用XStream将XML读取到对象时,如果我不知道XML将包含多少个字段呢?(While using XStream to read XML to object, what if I don't know how many fields the XML will have?)

我已经建立了一个从XML读取的系统,但我需要在读取XML之前手动构建对象。

XML:

<actor id="" PGFVersion="" GSCVersion=""> <attributes> <text id="name">Actor 1</text> <real id="time">0</real> <point id="position"> <real id="x">0</real> <real id="y">0</real> </point> <size id="size"> <real id="width">0</real> <real id="height">0</real> </size> <angle id="rotation">0</angle> <color id="color"> <real id="red">0</real> <real id="green">0</real> <real id="blue">0</real> <real id="alpha">0</real> </color> <image id="image">0</image> <text id="tags">tag1 tag2</text> <boolean id="preloadArt">true</boolean> </attributes> </actor>

我如何设置对象:

public class Attributes { @XStreamImplicit public List fields = new ArrayList(); public Attributes(){ this.addText("name", "Actor 1"); this.addReal("time", "0"); this.addPoint("position", "0", "0"); this.addSize("0", "0"); this.addAngle("rotation", "0"); this.addColor("0", "0", "0", "0"); this.addImage("0"); this.addText("tags", "tag1 tag2"); this.addBoolean("preloadArt","true"); } public void addText(String id, String value){ Text text = new Text(id,value); this.fields.add(text); } //other adders }

但是我应该如何处理随机的字段数。 如果XML上有2个<color>字段,我会读到怎么办? 如何实现自动化?

I've set up a system to read from XML but I need to build the object manually before reading XML to it.

XML:

<actor id="" PGFVersion="" GSCVersion=""> <attributes> <text id="name">Actor 1</text> <real id="time">0</real> <point id="position"> <real id="x">0</real> <real id="y">0</real> </point> <size id="size"> <real id="width">0</real> <real id="height">0</real> </size> <angle id="rotation">0</angle> <color id="color"> <real id="red">0</real> <real id="green">0</real> <real id="blue">0</real> <real id="alpha">0</real> </color> <image id="image">0</image> <text id="tags">tag1 tag2</text> <boolean id="preloadArt">true</boolean> </attributes> </actor>

How I set-up the object:

public class Attributes { @XStreamImplicit public List fields = new ArrayList(); public Attributes(){ this.addText("name", "Actor 1"); this.addReal("time", "0"); this.addPoint("position", "0", "0"); this.addSize("0", "0"); this.addAngle("rotation", "0"); this.addColor("0", "0", "0", "0"); this.addImage("0"); this.addText("tags", "tag1 tag2"); this.addBoolean("preloadArt","true"); } public void addText(String id, String value){ Text text = new Text(id,value); this.fields.add(text); } //other adders }

But what should I do with random count of fields. What if there are 2 <color> fields on the XML I'll read. How to automate this?

最满意答案

通常,XStream对象具有表示XML中的字段的字段。 请参阅XStream教程中的Person -object。

如果你有这种类型的对象,我认为你应该能够使用@XStreamImplicit来注释color属性来处理XML中的多个出现。

在这个未经测试的代码的某处:

public class Attributes { // 'name' and 'text' occurs only once. public String name; public String text; public Size size; // The other attributes // color can occur multiple times. @XStreamImplicit public int color; }

Usually the XStream object have fields representing the fields in the XML. See the Person-object in the XStream tutorial.

If you have this type of object, I think you should be able to annotate the color property with @XStreamImplicit to handle multiple occurences in the XML.

Somewhere along the lines of this untested code:

public class Attributes { // 'name' and 'text' occurs only once. public String name; public String text; public Size size; // The other attributes // color can occur multiple times. @XStreamImplicit public int color; }

更多推荐

本文发布于:2023-07-30 21:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340329.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我不   字段   多少个   对象   XML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!