XML Data Parsing on Android Platform

Asked 2 years ago, Updated 2 years ago, 143 views

public class QuestContainer {

private List<QUEST> confirmed = new List<QUEST>();
private List<QUEST> complete = new List<QUEST>();
private List<QUEST> possible = new List<QUEST>();
private List<QUEST> im_possible = new List<QUEST>();

public List<QUEST> Confirmed
{
    get { return confirmed; }
    set { confirmed = value; }
}

public List<QUEST> Complete
{
    get { return complete; }
    set { complete = value; }
}

public List<QUEST> Possible
{
    get { return possible; }
    set { possible = value; }
}

public List<QUEST> im_Possible
{
    get { return im_possible; }
    set { im_possible = value; }
}

} Above is the space to import xml data.

Called xml data is automatically classified.

[im_Possible]

[QUEST]
  [Questname]Start[/Questname]
  [Description] It's just the beginning![/Description]
  [Class]ALL[/Class]
  [Level]0[/Level]
  [STR]0[/STR]
  [CON]0[/CON]
  [NT]0[/INT]
  [WIS]0[/WIS]
  [DEX]0[/DEX]
  [Title /]
  [Item /]
  [questType]STORY[/questType]
  [difficulty]EASY[/difficulty]
  [eXPEDIENT]MOV[/eXPEDIENT]
  [questReward]GOLD[/questReward]
[/QUEST]

[/im_Possible]

Above is the form stored in the xml data.

Type[] questType = { typeof(QUEST) };

XmlSerializer serializer = new XmlSerializer(typeof(QuestContainer), questType);

TextReader textReader = new StreamReader(Application.persistentDataPath + "/Quests.xml");

QuestContainer = (QuestContainer)serializer.Deserialize(textReader);

textReader.Close();

The above is the method currently in use.

But it doesn't work on the Android platform.

I want this to work on the Android platform.

parsing xml android unity

2022-09-22 22:13

1 Answers

There's a framework called Simple that works on Android.

http://simple.sourceforge.net/download.php Get the latest version here, create a folder called lib in your Android project, and add the downloaded jar file here.

If it's Eclipse... Add Project -> Select Build Path -> Select Configure Build Path -> Library -> add External Jars.

http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php Here's how to use it.

First of all, you need to change the design of the class Quest. You have to use an annotation called @element, and you have to create a method like getQuestName for each value.

pubic class Quest{
//add an annotation called @element to each value
    @element
    private String QuestName;
    @element
    private int Level;
I'm gonna make a creator 
  public Quest(String Questname, int Level){
    this.QuestName = QuestName;
    this.Level = Level;
  }
//Create a get method for each value.
  public String getQuestname(){
    return QuestName;
  }

  public int getLevel(){
    return Level;
  }
}

When serializing,

Serialize using
//Persister.
Serializer serializer = new Persister();
Quest question = newQuest ("start", 0);
File result = new File("Quests.xml");

serializer.write(example, result);

For Deserialize

Serializer serializer = new Persister();
File source = new File("Quests.xml");

Quest quest = serializer.read(Quest.class, source);

You can do it.

I think you want to write a list, too http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#inline Note.


2022-09-22 22:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.