如何解析XML并使用SimpleXML将其存储在Map中?

时间:2016-08-23 13:42:51

标签: java android simple-framework

我在使用简单的xml框架解析xml时遇到了问题。我想在Map / hashMap中存储类别ID和锦标赛列表我该怎么做?我按照简单的xml教程,但它对我没用。

我将它存储在这样的列表中:

 @ElementList(entry = "Category", inline = true, required = false)
List<Category> category;

但现在我想把它存放在地图上。

这是xml: enter image description here

我遵循的教程: enter image description here

任何帮助将不胜感激,tnx。

2 个答案:

答案 0 :(得分:2)

{3}}仍然是 (好)选项

这样做意味着:为@ElementList实施转换器合并类别ID /锦标赛列表对。

这是如何做到的:

首先是必要的(数据)类。

赛:

@ElementMap

注意: 我给了Tournaments一些数据字符串,因为没有显示真实数据。但是,实际上它包含的数据并不重要。

分类

@Root(name = "Tournament")
public class Tournament
{
    @Text
    private String data;

    public Tournament(String data)
    {
        this.data = data;
    }

    private Tournament() { /* Required */ }

    // ...
}

Tournament

@Root(name = "Category") public class Category { private String id; // ... } 将使用以下@Root(name = "Tournaments") @Convert(TournamentsConverter.class) // Specify the Converter used for this class public class Tournaments { private Map<String, List<Tournament>> tournaments; public Tournaments() { tournaments = new HashMap<>(); } protected void put(String id, List<Tournament> tournaments) { this.tournaments.put(id, tournaments); } // ... } 进行@Convert的序列化/反序列化。我没有在这个例子中实现序列化部分(虽然它并不困难)。

重要提示: Converter需要Tournaments(请参阅下面的示例)!

锦标赛转换器

@Convert

所有&#34;魔法&#34; 由转换器完成:

  1. 遍历所有AnnotationStrategy子节点
  2. 如果它是public class TournamentsConverter implements Converter<Tournaments> { private final Serializer serializer = new Persister(); @Override public Tournaments read(InputNode node) throws Exception { Tournaments tournaments = new Tournaments(); InputNode childNode = node.getNext(); // Iterate over all childs of 'Tournaments' while( childNode != null ) { if( childNode.getName().equals("Category") == true ) { final String categoryId = childNode.getAttribute("category_id").getValue(); List<Tournament> tournamentList = new ArrayList<>(); InputNode child = childNode.getNext(); // Iterate over all childs of 'Category' while( child != null ) { // Use a Serializer to read the nodes data Tournament tournament = serializer.read(Tournament.class, child); tournamentList.add(tournament); child = childNode.getNext(); } // Insert the Id / Tournament's pair tournaments.put(categoryId, tournamentList); } childNode = node.getNext(); } return tournaments; } @Override public void write(OutputNode node, Tournaments value) throws Exception { // Implement as needed throw new UnsupportedOperationException("Not supported yet."); } }
    1. 获取Tournaments
    2. 对所有孩子进行迭代,并将Category添加到列表中
    3. id / Tournament列表添加到结果id
  3. 如上所示,可以使用Tournament进行(反)序列化节点。无需手动实施。

    实施例

    最后,这是一个例子。请注意Tournaments

    Serializer

    <强>输出:
    使用添加到每个类的生成的AnnotationStrategy

    Serializer ser = new Persister(new AnnotationStrategy());
    
    final String xml = "<Tournaments>\n"
            + "   <Category category_id=\"289\">\n"
            + "      <Tournament>aaaa</Tournament>\n"
            + "   </Category>\n"
            + "   <Category category_id=\"32\">\n"
            + "      <Tournament>bbbd</Tournament>\n"
            + "      <Tournament>cccc</Tournament>\n"
            + "   </Category>\n"
            + "</Tournaments>";
    
    Tournaments t = ser.read(Tournaments.class, xml);
    
    System.out.println(t);
    

答案 1 :(得分:1)

问题是您正在尝试创建类似Map<String, <List<Tournament>>的结构,而ElementMap注释似乎无法实现。您需要一种ElementMapList注释。也许你可以file an issue和加拉格尔先生加上这个。