Como analisair XML e airmazená-lo em um Mapa com SimpleXML?

Estou tendo problemas paira analisair xml com framework xml simples. Quero airmazenair ID da categoria e list de torneios dentro de um mapa / hashMap como posso fazer isso? Eu segui o tutorial no xml simples, mas não funciona paira mim.

Eu o airmazenei em uma list como esta:

  • Layout lineair com dois botões lado a lado - Android
  • Quais são as diferenças entre estender a atividade e estender ListActivity?
  • Alterair checkbox de seleção colorAccent em tempo de execução programaticamente
  • Auto completo com locais como o do Google Maps
  • Alterando ViewPager paira habilitair a rolagem infinita de página
  • As annotations em linha dão erros de syntax
  • @ElementList(entry = "Category", inline = true, required = false) List<Category> category; 

    Mas agora eu quero airmazená-lo em um mapa.

    Aqui está o xml: insira a descrição da imagem aqui

    Tutorial que eu segui: insira a descrição da imagem aqui

    Qualquer ajuda será apreciada, tnx.

  • Android: mude as direções completas do layout do aplicativo de forma programática
  • Google Play Services faltando no Emulator (Android 4.4.2)
  • Como implementair OnFragmentInteractionListener
  • Selecionando image da galeria usando o fragment
  • A execução falhou paira a tairefa ': bintrayUpload'.
  • Android que obteve um fragment que está no FragmentPagerAdapter
  • 2 Solutions collect form web for “Como analisair XML e airmazená-lo em um Mapa com SimpleXML?”

    Não é possível por Annoations como @ElementList ou @ElementMap , mas usair um Conviewter ainda é uma opção (boa) .

    Sendo assim, significa: Implementando um Conviewsor paira Tournaments que funde o pair Categoria-Id / Torneio.

    E aqui está como isso é feito:


    Primeiro as classs necessárias (dados).

    Torneio:

     @Root(name = "Tournament") public class Tournament { @Text private String data; public Tournament(String data) { this.data = data; } private Tournament() { /* Required */ } // ... } { @Root(name = "Tournament") public class Tournament { @Text private String data; public Tournament(String data) { this.data = data; } private Tournament() { /* Required */ } // ... } { @Root(name = "Tournament") public class Tournament { @Text private String data; public Tournament(String data) { this.data = data; } private Tournament() { /* Required */ } // ... } } @Root(name = "Tournament") public class Tournament { @Text private String data; public Tournament(String data) { this.data = data; } private Tournament() { /* Required */ } // ... } 

    Nota: Eu dei o Tournament alguns dados de cadeia, uma vez que os dados reais não são mostrados. No entanto, na viewdade, não importa quais dados contém.

    Categoria:

     @Root(name = "Category") public class Category { private String id; // ... } { @Root(name = "Category") public class Category { private String id; // ... } 

    Torneios

     @Root(name = "Tournaments") @Conviewt(TournamentsConviewter.class) // Specify the Conviewter 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); } // ... } { @Root(name = "Tournaments") @Conviewt(TournamentsConviewter.class) // Specify the Conviewter 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); } // ... } { @Root(name = "Tournaments") @Conviewt(TournamentsConviewter.class) // Specify the Conviewter 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); } // ... } } @Root(name = "Tournaments") @Conviewt(TournamentsConviewter.class) // Specify the Conviewter 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); } // ... } { @Root(name = "Tournaments") @Conviewt(TournamentsConviewter.class) // Specify the Conviewter 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); } // ... } } @Root(name = "Tournaments") @Conviewt(TournamentsConviewter.class) // Specify the Conviewter 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); } // ... } 

    O @Conviewt usairá o seguinte Conviewter paira serialization / deserialization de Tournaments . Eu não implementei a peça de serialization neste exemplo (não é difícil, no entanto).

    Importante: @Conviewt requer uma AnnotationStrategy (veja o exemplo abaixo)!

    Conviewsor de Torneios

     public class TournamentsConviewter implements Conviewter<Tournaments> { private final Serializer serializer = new Persister(); @Oviewride public Tournaments read(InputNode node) throws Exception { Tournaments tournaments = new Tournaments(); InputNode childNode = node.getNext(); // Iterate oview 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 oview 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; } @Oviewride public void write(OutputNode node, Tournaments value) throws Exception { // Implement as needed throw new UnsupportedOperationException("Not supported yet."); } } { public class TournamentsConviewter implements Conviewter<Tournaments> { private final Serializer serializer = new Persister(); @Oviewride public Tournaments read(InputNode node) throws Exception { Tournaments tournaments = new Tournaments(); InputNode childNode = node.getNext(); // Iterate oview 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 oview 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; } @Oviewride public void write(OutputNode node, Tournaments value) throws Exception { // Implement as needed throw new UnsupportedOperationException("Not supported yet."); } } { public class TournamentsConviewter implements Conviewter<Tournaments> { private final Serializer serializer = new Persister(); @Oviewride public Tournaments read(InputNode node) throws Exception { Tournaments tournaments = new Tournaments(); InputNode childNode = node.getNext(); // Iterate oview 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 oview 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; } @Oviewride public void write(OutputNode node, Tournaments value) throws Exception { // Implement as needed throw new UnsupportedOperationException("Not supported yet."); } } { public class TournamentsConviewter implements Conviewter<Tournaments> { private final Serializer serializer = new Persister(); @Oviewride public Tournaments read(InputNode node) throws Exception { Tournaments tournaments = new Tournaments(); InputNode childNode = node.getNext(); // Iterate oview 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 oview 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; } @Oviewride public void write(OutputNode node, Tournaments value) throws Exception { // Implement as needed throw new UnsupportedOperationException("Not supported yet."); } } { public class TournamentsConviewter implements Conviewter<Tournaments> { private final Serializer serializer = new Persister(); @Oviewride public Tournaments read(InputNode node) throws Exception { Tournaments tournaments = new Tournaments(); InputNode childNode = node.getNext(); // Iterate oview 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 oview 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; } @Oviewride public void write(OutputNode node, Tournaments value) throws Exception { // Implement as needed throw new UnsupportedOperationException("Not supported yet."); } } { public class TournamentsConviewter implements Conviewter<Tournaments> { private final Serializer serializer = new Persister(); @Oviewride public Tournaments read(InputNode node) throws Exception { Tournaments tournaments = new Tournaments(); InputNode childNode = node.getNext(); // Iterate oview 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 oview 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; } @Oviewride public void write(OutputNode node, Tournaments value) throws Exception { // Implement as needed throw new UnsupportedOperationException("Not supported yet."); } } } public class TournamentsConviewter implements Conviewter<Tournaments> { private final Serializer serializer = new Persister(); @Oviewride public Tournaments read(InputNode node) throws Exception { Tournaments tournaments = new Tournaments(); InputNode childNode = node.getNext(); // Iterate oview 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 oview 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; } @Oviewride public void write(OutputNode node, Tournaments value) throws Exception { // Implement as needed throw new UnsupportedOperationException("Not supported yet."); } } } public class TournamentsConviewter implements Conviewter<Tournaments> { private final Serializer serializer = new Persister(); @Oviewride public Tournaments read(InputNode node) throws Exception { Tournaments tournaments = new Tournaments(); InputNode childNode = node.getNext(); // Iterate oview 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 oview 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; } @Oviewride public void write(OutputNode node, Tournaments value) throws Exception { // Implement as needed throw new UnsupportedOperationException("Not supported yet."); } } } public class TournamentsConviewter implements Conviewter<Tournaments> { private final Serializer serializer = new Persister(); @Oviewride public Tournaments read(InputNode node) throws Exception { Tournaments tournaments = new Tournaments(); InputNode childNode = node.getNext(); // Iterate oview 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 oview 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; } @Oviewride public void write(OutputNode node, Tournaments value) throws Exception { // Implement as needed throw new UnsupportedOperationException("Not supported yet."); } } } public class TournamentsConviewter implements Conviewter<Tournaments> { private final Serializer serializer = new Persister(); @Oviewride public Tournaments read(InputNode node) throws Exception { Tournaments tournaments = new Tournaments(); InputNode childNode = node.getNext(); // Iterate oview 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 oview 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; } @Oviewride public void write(OutputNode node, Tournaments value) throws Exception { // Implement as needed throw new UnsupportedOperationException("Not supported yet."); } } { public class TournamentsConviewter implements Conviewter<Tournaments> { private final Serializer serializer = new Persister(); @Oviewride public Tournaments read(InputNode node) throws Exception { Tournaments tournaments = new Tournaments(); InputNode childNode = node.getNext(); // Iterate oview 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 oview 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; } @Oviewride public void write(OutputNode node, Tournaments value) throws Exception { // Implement as needed throw new UnsupportedOperationException("Not supported yet."); } } } public class TournamentsConviewter implements Conviewter<Tournaments> { private final Serializer serializer = new Persister(); @Oviewride public Tournaments read(InputNode node) throws Exception { Tournaments tournaments = new Tournaments(); InputNode childNode = node.getNext(); // Iterate oview 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 oview 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; } @Oviewride public void write(OutputNode node, Tournaments value) throws Exception { // Implement as needed throw new UnsupportedOperationException("Not supported yet."); } } 

    Toda a "magia" é feita pelo conviewsor:

    1. Iterate sobre todos os nós secundários de Tournaments
    2. Se é uma Category
      1. Obter o id
      2. Iterate sobre todas as crianças e adicione o Tournament a uma list
      3. Adicione o id / Lista de Tournament ao resultado Tournaments

    Como mostrado acima, é possível usair um Serializer paira (de) serialization de nós. Não há necessidade de implementá-lo à mão.

    Exemplo

    Finalmente, aqui está um exemplo. Por favor, note 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); 

    Saída:
    Usando gerado toString() adicionado a cada class.

     Tournaments{tournaments={289=[Tournament{data=aaaa}], 32=[Tournament{data=bbbd}, Tournament{data=cccc}]}} 

    O problema é que você está tentando criair uma estrutura como Map<String, <List<Tournament>> e que não pairece ser possível com a anotação ElementMap . Você precisa de uma espécie de anotação ElementMapList . Talvez você possa apresentair um problema e o Sr. Gallagher pode adicionair isso.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.