简体   繁体   English

使用Java从Android解析XML文件-首次计时器

[英]Parsing an XML file from android using Java - First Timer

I've been trying to figure out how to parse an XML file and save the data i get into a file. 我一直在尝试弄清楚如何解析XML文件并将保存的数据保存到文件中。 Most of the examples i've found only consider a one depth deep list. 我发现的大多数示例仅考虑一个深度较深的列表。 My XML has multiple depths and since it's my first time loading data from XML's i'm still trying to get my head around it. 我的XML有多种深度,因为这是我第一次从XML加载数据,所以我仍在努力解决这个问题。 This is my code but i still can't debug it so please let me know if and what i'm doing wrong. 这是我的代码,但是我仍然无法调试它,所以请让我知道我做错了什么。 Thanks! 谢谢!

public TestClass {
     public TestClass() {}

    public String Name;
    public String ID;
    public int Reputation;
    public List<String> ChoosenTypes;

    public TestClass(XmlPullParser inParseData) {
        int eventType = inParseData.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {

        if (eventType == XmlPullParser.START_DOCUMENT) { }
        else if (eventType == XmlPullParser.END_DOCUMENT) { }
        else if (eventType == XmlPullParser.START_TAG) {
             String ReadingTag = inParseData.getName();
             if (ReadingTag.equals("name")) { Name = inParseData.getText(); }
             if (ReadingTag.equals("id")) { ID = inParseData.getText(); }
             if (ReadingTag.equals("rep")) { Reputation = Integer.parseInt(inParseData.getText()); }
             if (ReadingTag.equals("types")) {
                  List<String> types = new ArrayList<String>();
                  if (inParseData.getDepth() == 2) {
                       while (inParseData.getEventType() != XmlPullParser.END_TAG) {
                            String ReadingTagD2 = inParseData.getName();
                            if (ReadingTagD2.equals("mytypes")) {
                                 types.add(inParseData.getText());
                            }
                       }
                       ChoosenTypes = types;
                  }
             }
        }
        eventType = inParseData.next();
    }
}

With My XML Being: 使用我的XML:

<?xml version="1.0" encoding="UTF-8"?>
<people>
    <name>NAME</name>
    <id>012901</id>
    <rep>10</rep>
    <types>
        <mytypes>type 1</mytypes>
        <mytypes>type 2</mytypes>
        <mytypes>type 3</mytypes>
        <!-- The number of types is not fixed -->
    </types>
</people>

Please forgive me if this is completely wrong. 如果完全错误,请原谅我。 Just trying to Learn :D 只是想学习:D

You cannot fetch the value(text) of the tag when the eventType is START_TAG. 当eventType为START_TAG时,您无法获取标签的值(文本)。 The next eventType which will be XmlPullParser.TEXT shall contain the value where you can do inParseData.getText() . 下一个eventType将为XmlPullParser.TEXT ,它将包含您可以在其中执行inParseData.getText() But you need to keep a track of which tag is currently being parsed. 但是您需要跟踪当前正在解析哪个标签。

Define the ReadingTag above the while statement and add the new code. 在while语句上ReadingTag并添加新代码。 That should give you a clue how you will need to retrieve all the other values. 那应该给您一个线索,您将需要如何检索所有其他值。

 else if (eventType == XmlPullParser.START_TAG) {
   ...
   ...
 }
 // New Code 
 else if (eventType == XmlPullParser.TEXT) {
    System.out.println(inParseData.getText());
    if (ReadingTag.equals("name")) {
      Name = inParseData.getText();
    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM