简体   繁体   English

XML解析:如何将XML文件中的下一个元素放入字符串中? (Java / Android)

[英]XML-Parsing: How put next Elements from XML-File into String? (Java / Android)

I wanted to parse my own XML-Files, so I downloaded a simple Parser from the Internet. 我想解析自己的XML文件,因此我从Internet下载了一个简单的Parser。

public class ParsingXML extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // setContentView(R.layout.main);
    setContentView(R.layout.relativelayout);
    TextView journal = (TextView) findViewById(R.id.journal);
    TextView publisher = (TextView) findViewById(R.id.publisher);
    TextView edition1 = (TextView) findViewById(R.id.edition1);
    TextView title1 = (TextView) findViewById(R.id.title1);
    TextView author1 = (TextView) findViewById(R.id.author1);

    TextView edition2 = (TextView) findViewById(R.id.edition2);
    TextView title2 = (TextView) findViewById(R.id.title2);
    TextView author2 = (TextView) findViewById(R.id.author2);

    try {
        XmlResourceParser xpp = getResources().getXml(R.xml.catalog);

        xpp.next();
        int eventType = xpp.getEventType();
        int iter = 0;
        String elemtext = null;

        while (eventType != XmlPullParser.END_DOCUMENT) {

            if (eventType == XmlPullParser.START_TAG) {

                String elemName = xpp.getName();
                if (elemName.equals("catalog")) {
                    String journalAttr = xpp.getAttributeValue(null,
                            "journal");
                    String publisherAttr = xpp.getAttributeValue(null,
                            "publisher");
                    journal.setText(journalAttr);
                    publisher.setText(publisherAttr);
                }
                if (elemName.equals("article")) {
                    iter = iter + 1;
                }

                if (elemName.equals("edition")) {
                    elemtext = "edition";
                }
                if (elemName.equals("title")) {
                    elemtext = "title";
                }
                if (elemName.equals("author")) {
                    elemtext = "author";
                }
            }

            else if (eventType == XmlPullParser.TEXT) {
                if (iter == 1) {
                    if (elemtext.equals("edition")) {
                        edition1.setText(xpp.getText());
                    } else if (elemtext.equals("title")) {
                        title1.setText(xpp.getText());
                    } else if (elemtext.equals("author")) {
                        author1.setText(xpp.getText());
                    }
                }

                else if (iter == 2) {
                    if (elemtext.equals("edition")) {
                        edition2.setText(xpp.getText());
                    } else if (elemtext.equals("title")) {
                        title2.setText(xpp.getText());
                    } else if (elemtext.equals("author")) {
                        author2.setText(xpp.getText());
                    }

                }
            }
            eventType = xpp.next();
        }

    } catch (XmlPullParserException e) {
    } catch (IOException e) {
    }

}
}









<?xml version = '1.0' encoding = 'UTF-8'?>
<catalog journal="sample journal" publisher="sample journal">
<article>
    <edition>EDITION 1</edition>
    <title>TITLE 1</title>
    <author>AUTHOR 1</author>
</article>
<article>
    <edition>EDITION 2</edition>
    <title>TITLE 2</title>
    <author>AUTHOR 2</author>
</article><article>
    <edition>EDITION 3</edition>
    <title>TITLE 3</title>
    <author>AUTHOR 3</author>
</article><article>
    <edition>EDITION 4</edition>
    <title>TITLE 4</title>
    <author>AUTHOR 4</author>
</article><article>
    <edition>EDITION 5</edition>
    <title>TITLE 5</title>
    <author>AUTHOR 5</author>
</article>
</catalog>

Problem: There are some 2 TextViews for every Element, but my XML-File contains more Elements. 问题:每个元素都有一些2个TextView,但是我的XML文件包含更多的元素。 How can I do that the parser writes the next Elements to the String, so that the TextView shows the next Elements? 解析器如何将下一个元素写入字符串,以便TextView显示下一个元素?

Thanks 谢谢

And what if you has 1000 elements in your xml file? 如果您的xml文件中有1000个元素,该怎么办? would you create a layout with that much views? 您会创建一个具有这么多视图的布局吗?

You should create a layout file which represent the article element and while reading the xml, initialize the layout from the xml data and inflate it to your activity view. 您应该创建一个表示article元素的布局文件,并在读取xml时,从xml数据初始化布局并将其充气到活动视图中。

This is the LayoutInflater: http://developer.android.com/reference/android/view/LayoutInflater.html 这是LayoutInflater: http : //developer.android.com/reference/android/view/LayoutInflater.html

Example: How to inflate one view with a layout 示例: 如何使用布局使一个视图膨胀

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

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