在Android中解析XML {Java}

huangapple go评论66阅读模式
英文:

XML Parsing in Android {Java}

问题

以下是你要求的代码部分的翻译:

public List parse(InputStream in) throws XmlPullParserException, IOException {
    try {
        XmlPullParser parser = Xml.newPullParser(); /* 创建新的解析器 */
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); /* 设置命名空间处理特性 */
        parser.setInput(in, null);
        parser.nextTag();
        return readFeed(parser); /* 提取和处理数据 */
    } finally {
        in.close();
    }
}

private List readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
    List girlNameList = new ArrayList();
    List boyNameList = new ArrayList();
    parser.require(XmlPullParser.START_TAG, ns, "data");

    while (parser.next() != XmlPullParser.END_TAG) {
        parser.next();
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String currentTag = parser.getName();
        if (currentTag.equals("emily")) {
            girlNameList.add(currentTag);
        } else if (currentTag.equals("robert")) {
            boyNameList.add(currentTag);
        } else {
            skip(parser);
        }
    }
    return namesList;
}
<data>
    <emily type="short" msg="======something=========="> </emily>
    <emily type="tall" msg="======another message=========="> </emily>

    <robert type="short"> </robert>

    <emily type="tall"> </emily>

    <robert type="tall" msg="use this later"> </robert>
    <tester type="n/a" msg="should be skipped"> </tester>
</data>
英文:

I am trying to parse a XML file in my android app and tried following the Android Developers examples on the site. However, even when all of the tags within the 'start' tags have been processed, the program continues in an infinite loop, never gets out of 'while (parser.next() != XmlPullParser.END_TAG)' loop even though my XML file starts and ends with &lt;data&gt; and &lt;/data&gt; tags.
What am I doing wrong?

 public List parse(InputStream in) throws XmlPullParserException, IOException { /* Function that takes Assay file as input stream */
        try {
            XmlPullParser parser = Xml.newPullParser(); /* Create new parser  */
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); /*  */
            parser.setInput(in, null);
            parser.nextTag();
            return readFeed(parser); /* extracts and processes the data */
        } finally {
            in.close();
        }
    }
 private List readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
        List girlNameList = new ArrayList();
        List boyNameList = new ArrayList();
        parser.require(XmlPullParser.START_TAG, ns, &quot;data&quot;); 

        while (parser.next() != XmlPullParser.END_TAG) { 
            parser.next();
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String currentTag = parser.getName();         
            if(currentTag.equals(&quot;emily&quot;)) {            
                girlNameList.add(currentTag);            
            } else if(currentTag.equals(&quot;robert&quot;) {
                 boyNameList.add(currentTag); 
            } else {
                skip(parser); 
            }                          
        }
        return namesList;
    }
&lt;data&gt;
    &lt;emily type=&quot;short&quot; msg=&quot;======something==========&quot;&gt;  &lt;/emily&gt;
    &lt;emily type=&quot;tall&quot; msg=&quot;======another message==========&quot;&gt;  &lt;/emily&gt;

    &lt;robert type=&quot;short&quot; &gt; &lt;/robert&gt;

    &lt;emily type=&quot;tall&quot; &gt; &lt;/emily&gt;

    &lt;robert type=&quot;tall&quot; msg=&quot;use this later&quot; &gt; &lt;/robert&gt;
    &lt;tester type=&quot;n/a&quot; msg=&quot;should be skipped&quot; &gt; &lt;/tester&gt;
&lt;/data&gt;

答案1

得分: 1

使用:

while (parser.next() != XmlPullParser.END_DOCUMENT)

在 while 循环的条件检查中,您跳过了最后一个 END_TAG。

英文:

Use

 while (parser.next() != XmlPullParser.END_DOCUMENT)

You're skipping the last END_TAG in the condition check of the while loop.

huangapple
  • 本文由 发表于 2020年4月11日 10:24:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/61151355.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定