在android中使用简单的XML,找到一种捕获元素顺序的方法?

时间:2013-07-09 18:55:51

标签: android xml-parsing simple-framework

我一直在寻找一种方法来捕捉标签中列出的元素的顺序,但是还不是很成功..

编辑新:

<android>
  <leags>
     <leag name = "someName">
       <headlines>
           <pageType>headlines</pageType>
           <story>someStoryURL</story>
           <fullStory>someFullStoryURL</fullStory>
       </headlines>
       <scores></scores>
       <statistics></statistics>
     </leag>
  <leags>
</android>

- 想要捕捉leag中元素的顺序为1)标题2)得分3)统计。如果xml发生变化并且在标题之前列出了分数,那么它将是1)分数2)标题3)统计数据。

我只解析android - 就像这样:

@Root(name = "android", strict = false)
public class android
{
    @ElementList(name = "leags", required = false)
    private List<Leag> leags;

        public Leag getLeagByName(String name)
        { // go through list of leags and return leag with matching name}
}

所以在我的“leag”对象中,我想要捕获元素的顺序 - 有没有办法做到这一点?

我假设您需要像这样设置新的AnnotionStrategy():

    tAndroid android = null;
    Serializer serial = new Persister(new AnnotationStrategy());
    android = serial.read(android .class, source);

League x= android.getLeagueByName("oeoe");
for(String s: x.getOrder())
{
    Log.i("Order", s);
}

编辑前:

假设上面的xml是以下代码所示的内容:

@Element(name="headlines")
public class Headlines 
{
  @Element(name="pageType", required = false)
  private String pageType;

  @Element(name="story", required = false)
  private String storiesURL;

  @Element(name="fullStory", required = false)
  private String fullStoryURL;

  public String getStoriesURL()
  {
    return storiesURL;
  }
  public String getFullStoryURL()
  {
    return fullStoryURL;
  }
  @Override
  public String toString()
  {
    return  "PageType: " + this.pageType +
            "\nStoriesURL: " + this.storiesURL + 
            "\nFullStoryURL: " + this.fullStoryURL;
  }
}

有没有办法以某种方式返回解析元素的顺序?

就像一个方法,它将返回一个具有正确顺序的某种字符串,如:

  1. 的pagetype
  2. 故事
  3. fullStory

2 个答案:

答案 0 :(得分:1)

您可以使用Converter来获取订单。但是你无法从中返回订单(或者更好:你可以,但最好不要这样做)。

获得订单相对容易 - 诀窍是从Converter中获取。在途中是为您的班级添加一个列表并将其存储在那里。

实现:

@Root(name = "headlines")
@Convert(value = Headlines.HeadlinesConverter.class)
public class Headlines
{
    @Element(name="pageType", required = false)
    private String pageType;
    @Element(name="story", required = false)
    private String storiesURL;
    @Element(name="fullStory", required = false)
    private String fullStoryURL;

    private List<String> order; // Here we save the order of the elements


    public String getPageType()
    {
        return pageType;
    }


    public String getStoriesURL()
    {
        return storiesURL;
    }


    public String getFullStoryURL()
    {
        return fullStoryURL;
    }


    public List<String> getOrder()
    {
        return order;
    }



    @Override
    public String toString()
    {
        return "Headlines{" + "pageType=" + pageType 
                + ", storiesURL=" + storiesURL 
                + ", fullStoryURL=" + fullStoryURL + '}';
    }


    // You can implement the converter as an extra class too
    static class HeadlinesConverter implements Converter<Headlines>
    {
        @Override
        public Headlines read(InputNode node) throws Exception
        {
            Headlines h = new Headlines();
            h.order = new ArrayList<>(3);

            InputNode next = node.getNext();

            while( next != null )
            {
                final String value = next.getValue();

                /*
                 * You can use reflection (= slower) instead the switch, or
                 * remove the switch:
                 * 
                 *     h.order.add(next.getName());
                 * 
                 * and do this after the while loop:
                 * 
                 *     h.pageType = node.getNext("pageType");
                 *     ...
                 */
                switch(next.getName())
                {
                    case "pageType":
                        h.pageType = value;
                        break;
                    case "story":
                        h.storiesURL = value;
                        break;
                    case "fullStory":
                        h.fullStoryURL = value;
                        break;
                    default:
                        /* Maybe some error-handling here?! */
                        break;
                }

                h.order.add(next.getName()); // add 'value' if you want the order of the values

                next = node.getNext();
            }

            return h;
        }


        @Override
        public void write(OutputNode node, Headlines value) throws Exception
        {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }
}

注意:我这里没有使用 setter - 但最好这样做。

示例代码:

final File f = new File("test.xml");

Serializer ser = new Persister(new AnnotationStrategy()); /* ! */
Headlines h = ser.read(Headlines.class, f);

int i = 1;

for( String s : h.getOrder() )
{
    System.out.println((i++) + ". " + s);
}

最后是输出

1. pageType
2. story
3. fullStory

答案 1 :(得分:0)

您需要使用@Order注释。这里可以看到示例http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#xpath