解析名称不同的元素,使用Android的简单的XML库中的单一列表库中、元素、不同、名称

2023-09-06 14:55:37 作者:红薇染露 ゅ

我想知道是否有反正来治疗COL_1,COL_2 ...等作为一个列表,而不是单独的元素使用针对Android的简单的XML库。我一直在阅读一些关于换人,但我仍然感到困惑。

I was wondering if there was anyway to treat col_1,col_2...etc as a list rather than separate elements, using the SIMPLE XML Library for Android. I have been reading a bit about substitutions but I'm still confused.

当前格式:

<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>

我希望能够解析出山坳作为某种类型的列表,以便我可以处理任意数量的COLS。这可能吗?

推荐答案

作为的纳克的面前说:使用 转换 这一点。简单的是让你定制加工的每一步(而在另一方面,它可能让你(解)序列,甚至复杂的结构与code的一些行)的辉煌。

As ng said before: Use a Converter for this. Simple is brilliant in letting you customize every step of processing (while on the other hand it's possible to let you (de-)serialize even complex structures with some lines of code).

所以这里有一个例子:

将从列表中保存的值A类:

@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;
    }

    //...
}

转换

The Converter:

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 检索它们,并将它们设置成你的输出。

This example uses only col_xy attributes, everything else is dropped. If you need those values too it's easy to implement them. You only have to retrieve them from the InputNode and set them into your output.