在分解的XML哪里属性决定类型 [英] demarshalling an xml where attributes determine type

查看:211
本文介绍了在分解的XML哪里属性决定类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被给定的XML文档(这是我无法控制它的格式)。我想知道,如果有可能demarshal基于元素属性的id这个XML。因此,不同的对象类型将共享相同的元素名称。

I'm being given xml documents (which I have no control its format). I would like to know if it is possible to demarshal this xml based on element attribute id. So different object types will share the same element name.

另外,我打算使用Simpile XML,因为我是在Android和JAXB不喜欢Android系统。

Also, I'm planning on using Simpile XML because I'm on Android and JAXB doesn't like Android.

所以我想是这样的:

class Root {
  Person p;
  Car c;
  ArrayList<Person> plist;
}

class Person{
  String name;
}

class Car {
  String type;
}

<root>
  <object id="person">
    <name> bob </name>
  </object>
  <object id="car">
    <model> ford </ford>
  </object>
  <sequence id="personSequence">
    <object id="person">
      <name> bob </name>
    </object>
    <object id="person">
      <name> bob </name>
    </object>
  </sequence>
</root>

不过,我知道肯定是我收到的文件将始终以相同的顺序与相同数量的顶级元素。例如,

However, I know for sure that the document I am receiving will always be in the same order with the same number of top level elements. For example,

ROOL总会有:1人1辆,1人,2辆汽车,和汽车的unknnown大小顺序

Rool will always have: 1 person, 1 car, 1 person, 2 cars, and a unknnown-size sequence of cars.

<root>
  <person />
  <car />
  <person />
  <car />
  <car />
  <sequence >
    I don't know how many objects will be in here
  <sequence >
<root>

太感谢了。

推荐答案

您可以使用<一个href=\"http://simple.sourceforge.net/download/stream/doc/javadoc/org/simpleframework/xml/convert/Converter.html\"相对=nofollow> 转换 - 实施做到这一点。这可以让你实现你自己的(默认)序列化的过程。

You can use a Converter-implementation to do this. This allows you to implement the process of (de-)serialization at your own.

下面是一个建议如何做到这一点:

Here's an suggestion how to do it:


  • 的抽象类为基础的所有对象(人,车等)

  • 对于那些实现

  • 包含所有其他元素的根对象

  • A 转换它创建具体从节点的根对象

  • An abstract class as base for all objects (persons, cars etc.)
  • Implementations for those
  • A root-object containing all other elements
  • A Converter which creates concrete a root-object from nodes

顺便说一句。您不必如果使用转换器来添加这些简单的注解 - 但是,它总是一个好主意,以防万一你需要他们。

Btw. you don't have to add those simple-Annotations if you use converters - however, it's always a good idea, just in case you need them.

(摘要)的所有对象的基类:

(Abstract) Base class for all objects:

@Root()
public abstract class ObjectElement
{
    @Attribute(name = "id")
    private String id;


    public ObjectElement(String id)
    {
        this.id = id;
    }

    // ...
}

Person类:

Person class:

@Root(name = "object")
public class PersonObject extends ObjectElement
{
    @Element(name = "name")
    private String name;


    public PersonObject(String name)
    {
        super("person");
        this.name = name;
    }

    // ...
}

车类:

@Root(name = "object")
public class CarObject extends ObjectElement
{
    @Element(name = "model")
    private String model;


    public CarObject( String model)
    {
        super("car");
        this.model = model;
    }

    // ...
}

序类:

@Root(name = "sequence")
public class SequenceObject extends ObjectElement
{
    @ElementList(inline = true)
    private final List<ObjectElement> elements;


    public SequenceObject()
    {
        super("personSequence");
        this.elements = new ArrayList<>();
    }

    // ...
}

-element;它有它的实例是孩子的(汽车等)和映射&LT;根和GT; ...&LT; /根方式&gt; 元素

The root-element; it has instances of it's childs (cars, etc.) and maps the <root> ... </root> element.

@Root(name = "root")
@Convert(RootElementConverter.class) // Set Converter here!
public class RootElement
{
    private final List<ObjectElement> elememts;


    public RootElement()
    {
        this.elememts = new ArrayList<>();
    }


    public void add(ObjectElement element)
    {
        this.elememts.add(element);
    }

    // ...
}

转换

public class RootElementConverter implements Converter<RootElement>
{
    @Override
    public RootElement read(InputNode node) throws Exception
    {
        RootElement root = new RootElement();

        /*
         * Iterate over all nodes and add them to root. You can use 
         * "node.getName()" and "node.getAttribute(...)" to test what
         * instance you have to add.
         * 
         * Use "root.add(...)" to add nodes.
         */

        return root;
    }


    @Override
    public void write(OutputNode node, RootElement value) throws Exception
    {
        /*
         * Implement this if you also serialize to xml.
         */
        throw new UnsupportedOperationException("Not supported yet.");
    }
}

最后如何调用:

final String xml = ...

// Important: Don't forget the AnnotationStrategy!
Serializer ser = new Persister(new AnnotationStrategy());

// Read root-object from xml-string or whatever
RootElement root = ser.read(RootElement.class, xml);

这篇关于在分解的XML哪里属性决定类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆