将xml作为字符串而不是使用xstream的类

时间:2014-05-15 12:57:56

标签: java xml-parsing xml-serialization xstream

我有像

这样的xml
<parent>
  <message>
   <type>15</type>
  </message>
</parent>

我需要将消息表示为String,而不是在父对象中创建消息对象。因此,当我执行parent.message时,输出为<type> 15 </type>而不是消息对象。

1 个答案:

答案 0 :(得分:4)

idia是通过处理HierarchicalStreamReader来构建消息的xml。如果您通过调用<messsage>进入reader.goDown(),那么reader.getValue()不会返回此元素的全部内容。

模型

    @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 />而我没有测试像&apos;等XML实体。

也许最好将邮件封在CDATA代码中?像

    <parent>
      <message>
       <![CDATA[
       <type>15</type>
       ]]>
      </message>
    </parent>