简体   繁体   English

简单框架xml反序列化 - 查找属性的存在

[英]Simple framework xml deserialize - looking up for existence of attributes

Is it possible to look up for existence of specified attributes in the Simple framework for java? 是否有可能在Java的Simple框架中查找指定属性的存在? Let's say I have the following xml: 假设我有以下xml:

<packet id="250">
  <msg id="1" uniqueAttribute1="some value" x="7" />
  <msg id="2" someStuff="123" />
  <msg id="3" someOtherStuff="abc" />
</packet>

All the msgs above should be deserialized into objects of different classes perhaps inherited from a common base class. 上面的所有消息都应该被反序列化为可能从公共基类继承的不同类的对象。 Which msg should instantiate an object of which class should depend on name of a attribute. 哪个msg应该实例化哪个类的对象应该依赖于属性的名称。 Meaning if there in the msg exists attribute named 'uniqueAttribute' then an object of class Foo should be created, if there is 'someStuff' an object of class Bar and so on. 意味着如果在msg中存在名为'uniqueAttribute'的属性,那么应该创建类Foo的对象,如果有'someStuff'是类Bar的对象,依此类推。

The only way which comes to my mind is to try to deserialize to objects of the classes and catching exceptions and retrying which sounds bad. 我想到的唯一方法是尝试反序列化类的对象并捕获异常并重试哪些听起来很糟糕。

If it is not possible with the 'Simple' framework maybe you could suggest some other method? 如果“简单”框架不可能,你可以建议一些其他方法吗?

I'm not seeing the need for a framework with such a basic requirement but then again I may not understand your needs totally. 我没有看到需要一个具有这样一个基本要求的框架,但我再也不能理解你的需求了。 I would initially start with something as simple as a SAX parse handler. 我最初会从像SAX解析处理程序这样简单的东西开始。 Totally hand-coded and untested but for a basic idea see here: 完全手工编码和未经测试,但基本想法见这里:

import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

class XmlHandler extends DefaultHandler {
    Map msgClasses = new HashMap(){{ put("uniqueAttribute1", "Foo"); put("someStuff", "Bar"); }};

    public MessageBase messageFromAttributes(final org.xml.sax.Attributes attributes) {
        for(int i=0; i<attributes.getLength(); i++) {
            if(msgClasses.contains(attributes.getValue(i))) {
                try{
                    Class msgClass = Class.forName(attributes.getValue(i));
                    return msgClass.newInstance();
                } catch(ClassNotFoundException e) {
                    return null;
                }
            }
        }
    }

    @Override
    public final void startElement(String uri, String localName, String name,
            final org.xml.sax.Attributes attributes) throws SAXException {
        if (localName.equals("msg")) {
            MessageBase message = messageFromAttributes(attributes);
        }
    }

    @Override
    public final void endElement(String uri, String localName, String name) throws SAXException {

        if (localName.equals("msg")) {
        }
    }

}

The idea of my above example is to map class names to specific attributes, then while handling the SAX parse events instantiate find any of the given attributes in the mapping which should point to the correct class name. 我上面的例子的想法是将类名映射到特定属性,然后在处理SAX解析事件实例化时,找到映射中应该指向正确类名的任何给定属性。 At this point you can instantiate the class using reflection and carry on. 此时,您可以使用反射实例化该类并继续。 You could optionally populate the class using the remaining attibute values if necessary. 如有必要,您可以选择使用剩余的attibute值填充类。

There are a number of ways to do this. 有很多方法可以做到这一点。 Typically a Visitor or Strategy can be used. 通常可以使用访客或策略。 See the following for how to use a Visitor. 有关如何使用访客的信息,请参阅以下内容。 This uses a namespace to determine the type, but you could change this to use an attribute or a number of them. 这使用命名空间来确定类型,但您可以将其更改为使用属性或其中的一些属性。

https://simple.svn.sourceforge.net/svnroot/simple/trunk/download/stream/src/test/java/org/simpleframework/xml/strategy/ClassToNamespaceVisitor.java https://simple.svn.sourceforge.net/svnroot/simple/trunk/download/stream/src/test/java/org/simpleframework/xml/strategy/ClassToNamespaceVisitor.java

Also, you could use a Converter to do this. 此外,您可以使用转换器执行此操作。 Take a look at the tutorial. 看一下教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM