将xml作为字符串而不是使用xstream的类 [英] get xml as string instead of class with xstream
问题描述
我有xml类似
<parent>
<message>
<type>15</type>
</message>
</parent>
我需要将消息表示为String,而不是在父对象中创建消息对象。所以,当我做parent.message时,输出是< type> 15< / type>
而不是消息对象。
Instead of creating a message object inside parent object, I need to represent message just as a String . So , when I do parent.message , the output is <type> 15 </type>
instead of a message object.
推荐答案
idia是建立起来的处理 HierarchicalStreamReader
的消息的xml。如果您通过调用 reader.goDown()
进入< messsage>
,很遗憾读者.getValue()
不会返回此元素的全部内容。
The idia is to build up the xml of message by processing the HierarchicalStreamReader
. If you go down into <messsage>
by calling reader.goDown()
unfortunately reader.getValue()
does not return the whole content of this element.
模型
@XStreamAlias("parent")
@XStreamConverter(value = ParentConverter.class)
public class Parent {
private final String message;
public Parent(final String message) { this.message = message; }
public String getMessage() { return message; }
}
转换器
public class ParentConverter implements Converter {
@Override
public boolean canConvert(@SuppressWarnings("rawtypes") final Class type) {
return Parent.class.isAssignableFrom(type);
}
@Override
public void marshal(Object source, HierarchicalStreamWriter writer,
MarshallingContext context) {
throw new UnsupportedOperationException("unmarshaling only");
}
@Override
public Object unmarshal(HierarchicalStreamReader reader,
UnmarshallingContext context) {
reader.moveDown();
if (!"message".equals(reader.getNodeName())) {
throw new ConversionException("Expected message, but was "
+ reader.getNodeName());
}
final StringBuilder message = new StringBuilder();
while (reader.hasMoreChildren()) {
reader.moveDown();
buildRecursiveMessage(reader, message);
reader.moveUp();
}
reader.moveUp();
final Parent parent = new Parent(message.toString());
return parent;
}
private void buildRecursiveMessage(final HierarchicalStreamReader reader,
final StringBuilder sb) {
// Build start-tag
final String nodeName = reader.getNodeName();
sb.append("<" + nodeName);
// Build attributes
final int numAttributes = reader.getAttributeCount();
if (numAttributes > 0) {
sb.append(" ");
for (int i = 0; i < numAttributes; i++) {
final String attributeName = reader.getAttributeName(i);
final String attributeValue = reader.getAttribute(i);
sb.append(attributeName + "=\"" + attributeValue + "\"");
final boolean lastAttribute = (i == numAttributes - 1);
if (!lastAttribute) {
sb.append(", ");
}
}
}
// Build children
final boolean containsChildren = reader.hasMoreChildren();
final boolean containsValue = !reader.getValue().isEmpty();
final boolean empty = !containsChildren && !containsValue;
sb.append(!empty ? ">" : " />");
if (containsChildren) {
while (reader.hasMoreChildren()) {
reader.moveDown();
buildRecursiveMessage(reader, sb);
reader.moveUp();
}
} else if (containsValue) {
sb.append(reader.getValue());
}
// Build end-tag
if (!empty) {
sb.append("</" + nodeName + ">");
}
}
}
此测试
public static void main(String[] args) {
final XStream xstream = new XStream();
xstream.processAnnotations(Parent.class);
// Deserialize
final String xml = "<parent><message><type>15</type></message></parent>";
final Parent parent = (Parent) xstream.fromXML(xml);
System.out.println(parent.getMessage());
}
打印出
<type>15</type>
但并非所有内容都相同!它忽略了例如空格,< foo>< / foo>
将映射到< foo />
我没有测试像&
等XML实体。
But it is not every the same content! It ignores for example whitespaces, <foo></foo>
would mapped to <foo />
and I don't tested XML entities like '
etc.
也许最好把你的信息包括在内在 CDATA 标签?喜欢
Maybe it's better to enclose your message in CDATA tags? Like
<parent>
<message>
<![CDATA[
<type>15</type>
]]>
</message>
</parent>
这篇关于将xml作为字符串而不是使用xstream的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!