【发布时间】:2013-12-14 12:39:04
【问题描述】:
我一直在使用 XStream 进行一些简单的 XML 转换,但我被困在了这个上。我要做的是创建一个 Java 程序来读取和调整这种 XML;
<?xml version="1.0" encoding="utf-8"?>
<actor id="id273212" PGFVersion="0.19" GSCVersion="0.10.4">
<attributes>
<text id="name">Actor 1c</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">120</real>
<real id="height">120</real>
</size>
<angle id="rotation">0</angle>
<color id="color">
<real id="red">1</real>
<real id="green">1</real>
<real id="blue">1</real>
<real id="alpha">1</real>
</color>
<image id="image" />
<text id="tags" />
<boolean id="preloadArt">true</boolean>
</attributes>
<behaviors />
<aspects>
<graphics>
<attributes>
<boolean id="visible">true</boolean>
<enumeration id="blendingMode">0</enumeration>
<enumeration id="horizontalWrap">0</enumeration>
<enumeration id="verticalWrap">0</enumeration>
<enumeration id="horizontalAnchor">0</enumeration>
<enumeration id="verticalAnchor">0</enumeration>
<boolean id="flipHorizontally">false</boolean>
<boolean id="flipVertically">false</boolean>
<integer id="tileWidth">0</integer>
<integer id="tileHeight">0</integer>
</attributes>
</graphics>
<motion>
<attributes>
<point id="linearVelocity">
<real id="x">0</real>
<real id="y">0</real>
</point>
<real id="angularVelocity">0</real>
<real id="maxSpeed">0</real>
<boolean id="applyMaxSpeed">false</boolean>
</attributes>
</motion>
<physics>
<attributes>
<real id="density">1</real>
<real id="friction">3</real>
<real id="restitution">1</real>
<boolean id="fixedRotation">false</boolean>
<boolean id="movable">true</boolean>
<enumeration id="collisionShape">0</enumeration>
<real id="drag">0</real>
<real id="angularDrag">0</real>
</attributes>
</physics>
</aspects>
</actor>
看不懂怎么办:<text id="name">Actor 1c</text>
我能得到的最接近的是:
<text id="name">
<variables>Actor 1c</variables>
</text>
我所做的是,我创建了一个“文本”类,它接受一个字符串(“Actor 1c”)到“变量”中。
我尝试使用“addImplicitCollection”,但它不起作用。我知道这个问题没有简单的答案,但我应该如何构建我的 Java 以便我可以读取这些 XML 文件?
【问题讨论】:
-
下面是如何使用
@XmlValue将其与 JAXB 映射:blog.bdoughan.com/2011/06/… -
@Blaise Doughan 我还是不明白。你在那里描述的就像使用
xstream.useAttributeFor(PhoneNumber.class, "type");我已经能够做到这一点。当我在 PhoneNumber 中有类似Text text1 = new Text();的内容时,我无法将其打印出来。