gpt4 book ai didi

java - XStream 并使用 'ToAttributedValueConverter' 表示两个相似但不同的 XML 字段

转载 作者:行者123 更新时间:2023-11-30 04:06:02 28 4
gpt4 key购买 nike

在我的 XML 文件中,有这样的行;

    <text id="name">Bu ilk rule</text>
<text id="bundleName">Rule.behavior</text>

所以我的 Text 类是这样的:

@XStreamConverter(value = ToAttributedValueConverter.class, strings = {"value"})
@XStreamAlias("text")
public class Text {

@XStreamAsAttribute
String id = "unset";
String value = "0";
}

而且它有效。但今天我在 XML 中得到了这个:

        <text id="targetAttribute">game.devices.touches.touch1.y</text>
<text id="RHS">
<expression>
<string>acos(x)</string>
</expression>
</text>

而且它不起作用。如果我删除 @XStreamConverter我得到这样的输出:

        <text id="targetAttribute"/>
<text id="RHS">
<expression>
<string>acos(x)</string>
</expression>
</text>

解决这个问题的方法是什么?

编辑:

在马蒂亚斯回答之后,我能得到的最接近所需的格式是:

        <text id="RHS">
<expression>
<string>acos(x)</string>
</expression>
</text>

但是这个方法失败了,它开始不显示“value”字段:

        <text id="name"/>
<text id="bundleName"/>
<text id="targetAttribute"/>

对于这个类:

@XStreamAlias("text")

class Test {

@XStreamAsAttribute
String id = "unset";
String value = "0";
myString string = new myString();
Expression expression = new Expression();

}

另外,我是这样阅读的,所以这样做时它必须有效:

String xml1 = readFile("C:\\1.xml");
Actor actorNew = (Actor) xstream.fromXML(xml1);

String xmlNew = xstream.toXML(actorNew);
System.out.println(xmlNew);

编辑2:

<string> 的 myString 类在 XML 中:

@XStreamConverter(value = ToAttributedValueConverter.class, strings = {"value"})
public class myString {

@XStreamAlias("string")
String value = "";

}

这是我正在尝试读取的 XML 的缩小 View :

<behaviors>
<behavior id="id384781" class="ChangeAttributeAction" enabled="true">
<attributes>
<text id="name">Change Attribute</text>
<text id="bundleName">ChangeAttribute.behavior</text>
<text id="targetAttribute">game.devices.touches.touch1.y</text>
<text id="RHS">
<expression>
<string>acos(x)</string>
</expression>
</text>
</attributes>
</behavior>
</behaviors>

一切正常,直到 id =“RHS”的文本

编辑3:

我在自定义转换器上尝试了类似的方法,但没有成功:

@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
Test test = new Test();
test.expression = new Expression();
test.expression.string = new myString();
test.value = reader.getValue();
test.id = reader.getAttribute("id");
reader.moveDown();
reader.moveDown();
test.expression.string.value = reader.getValue();
return test;
}

编辑4

WOOOOOOOOOOOOOOOOOOO我终于明白了!

这是我的做法:

class ValueConverter implements Converter {

@Override
public boolean canConvert(Class type) {
return Test.class.isAssignableFrom(type);
}

@Override
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
Test src = (Test) source;
writer.addAttribute("id", src.id);
writer.setValue(src.value);

if (src.expression.string.value.length() > 0) {
writer.startNode("expression");
writer.startNode("string");
writer.setValue(src.expression.string.value);
writer.endNode();
}

}

@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
Test test = new Test();
test.id = reader.getAttribute("id");
test.value = reader.getValue();
if (reader.hasMoreChildren()) {
reader.moveDown();
reader.moveDown();
test.expression.string.value = reader.getValue();

}
return test;
}

}

最佳答案

嗯,您显示的代码无法生成此 XML。我猜想在你的真实代码中的某个地方,有一些继承(扩展你的测试类的类表达式)正在产生这个结果。似乎 ToAttributedValueConverter 并不真正适用于派生类,并且在那里被忽略。

如果您想要将更复杂的对象结构映射到非常具体的 XML 表示形式,则应该使用转换器。如果有疑问,您始终可以创建一个自定义转换器来处理编码/解码。对于这些转换器,您可以轻松定义应将它们用于哪些类(请参阅 canConvert(Class) 方法。

我为您做了一段非常相似的代码:

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

@XStreamAlias("text")
class Test {

String id = "unset";
String value = "0";
}

@XStreamAlias("expression")
class Expression extends Test {
Expression() {
value = "acos(x)";
}
}

class ValueConverter implements Converter {

@Override
public boolean canConvert(Class type) {
return Test.class.isAssignableFrom(type);
}

@Override
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
Test src = (Test)source;
writer.addAttribute("id", src.id);
writer.setValue(src.value);
}

@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
Test test = new Test();
test.value = reader.getValue();
test.id = reader.getAttribute("id");
return test;
}

}

public class Example {
public static void main(String[] args) {
XStream xStream = new XStream();
xStream.autodetectAnnotations(true);
System.out.println("BEFORE setting a converter:");
System.out.println(xStream.toXML(new Test()));
System.out.println(xStream.toXML(new Expression()));
xStream.registerConverter(new ValueConverter());
System.out.println("\nAFTER setting a converter:");
System.out.println(xStream.toXML(new Test()));
System.out.println(xStream.toXML(new Expression()));
}
}

运行此命令,您将得到以下输出:

<text>
<id>unset</id>
<value>0</value>
</text>
<expression>
<id>unset</id>
<value>acos(x)</value>
</expression>

AFTER setting a converter:
<text id="unset">0</text>
<expression id="unset">acos(x)</expression>

关于java - XStream 并使用 'ToAttributedValueConverter' 表示两个相似但不同的 XML 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20694210/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com