1

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

编辑新:

<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”对象中,我想捕捉元素的顺序——有没有办法做到这一点?

我假设您需要像这样设置 new 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. 页面类型
  2. 故事
  3. 全文
4

2 回答 2

1

您可以使用 aConverter来获取订单。但是你不能从它那里退回订单(或者更好:你可以,但最好不要这样做)。

获得订单相对容易 - 诀窍是从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
于 2013-07-11T19:58:32.027 回答
0

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

于 2016-10-31T09:45:25.347 回答