java - 使用适用于 android 的 SIMPLE XML 库将不同命名的元素解析为单个列表

标签 java android xml simple-framework

我想知道是否可以使用 SIMPLE XML Library for Android 将 col_1、col_2...等视为列表而不是单独的元素。我一直在阅读一些关于替换的内容,但我仍然感到困惑。

当前格式:

<box-headers 
   dataTable="boxscore" 
   col_1="FINAL" 
   col_2="1" 
   col_3="2" 
   col_4="3" 
   col_5="4" 
   col_6="5" 
   col_7="6" 
   col_8="7" 
   col_9="8" 
   col_10="9" 
   col_11="R" 
   col_12="H" 
   col_13="E">
             table
 </box-headers>

我希望能够将列解析为某种列表,这样我就可以处理任意数量的列。这可能吗?

最佳答案

正如 ng 之前所说:使用 Converter为了这。 Simple 在让您自定义处理的每个步骤方面非常出色(而另一方面,您可以使用某些代码行(反)序列化甚至复杂的结构)。

举个例子:

将保存列表中的值的类:

@Root(name = "example")
@Convert(value = ListConverter.class) // Specify the Converter that's used for this class
public class Example
{
    // This element will be set with the values from 'box-headers' element
    @ElementList(name = "box-headers")
    private List<String> values;


    // This constructor is used to set the values while de-serializing
    // You can also use setters instead
    Example(List<String> values)
    {
        this.values = values;
    }

    //...
}

转换器:

public class ExampleConverter implements Converter<Example>
{
    @Override
    public Example read(InputNode node) throws Exception
    {
        List<String> list = new ArrayList<>(); // List to insert the 'col_' values

        NodeMap<InputNode> attributes = node.getAttributes(); // All attributes of the node
        Iterator<String> itr = attributes.iterator();

        while( itr.hasNext() ) // Iterate over all attributes
        {
            final String name = itr.next(); // The name of the attribute

            if( name.startsWith("col_") ) // Check if it is a 'col' attribute
            {
                // Insert the value of the 'col_' attribute
                list.add(attributes.get(name).getValue());
            }
        }

        // Return the result - instead of a constructor you can use setter(s) too
        return new Example(list); 
    }


    @Override
    public void write(OutputNode node, Example value) throws Exception
    {
        // TODO: Implement serializing here - only required if you want to serialize too
    }
}

使用方法:

// Serializer, don't forget `AnnotationStrategy` - without it wont work
Serializer ser = new Persister(new AnnotationStrategy());

// Deserialize the object - here the XML is readen from a file, other sources are possible
Example ex = ser.read(Example.class, new File("test.xml")); 

此示例仅使用 col_xy 属性,其他所有内容均被删除。如果您也需要这些值,那么很容易实现它们。您只需从 InputNode 中检索它们并将它们设置到您的输出中。

关于java - 使用适用于 android 的 SIMPLE XML 库将不同命名的元素解析为单个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17068785/

相关文章:

java - 如何创建这样的 Java swing UI?

java - 安卓 : convert binary string to byte

android - 错误 : Uh oh! ENOENT,没有这样的文件或目录 '<meteor-app>/.meteor/local/cordova-build/platforms/android/local.properties'

android - 显示 ListView 后,尝试以编程方式设置 LISTVIEW 内 SWITCH 的状态

Java GUI 自动调整大小

java - 多处理kafka消息

javascript - GetElementsByTagName 不适用于 XML

xml - 返回 "application/xml"而不是 "text/plain"ASP.NET Core Web API

sql-server - 需要将 SQL Server xml blob 转换为表进行操作

javascript - 我有2个POJO的学院和流。学院有一套流。在这种情况下如何使Pojo对应正确的json?