【问题标题】:XML output is not showing when parsing XML to Android将 XML 解析到 Android 时未显示 XML 输出
【发布时间】:2016-10-20 04:56:33
【问题描述】:

我想将 XML 解析到我的 Android 应用程序。我试过一个教程。在这里它使用 DOMParser。应用程序中没有错误。但它没有显示 XML 输出。请帮我解决问题。这是我到目前为止所尝试的。

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    LinearLayout layout = new LinearLayout(this);



    TextView name[];
    TextView website[];
    TextView category[];

    try {

        URL url = new URL(
                "http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml");

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new InputSource(url.openStream()));
        doc.getDocumentElement().normalize();

        NodeList nodeList = doc.getElementsByTagName("item");


        name = new TextView[nodeList.getLength()];
        website = new TextView[nodeList.getLength()];
        category = new TextView[nodeList.getLength()];

        for (int i = 0; i < nodeList.getLength(); i++) {

            Node node = nodeList.item(i);

            name[i] = new TextView(this);
            website[i] = new TextView(this);
            category[i] = new TextView(this);

            Element fstElmnt = (Element) node;
            NodeList nameList = fstElmnt.getElementsByTagName("name");
            Element nameElement = (Element) nameList.item(0);
            nameList = nameElement.getChildNodes();
            name[i].setText("Name = "
                    + ((Node) nameList.item(0)).getNodeValue());

            NodeList websiteList = fstElmnt.getElementsByTagName("website");
            Element websiteElement = (Element) websiteList.item(0);
            websiteList = websiteElement.getChildNodes();
            website[i].setText("Website = "
                    + ((Node) websiteList.item(0)).getNodeValue());

            category[i].setText("Website Category = "
                    + websiteElement.getAttribute("category"));

            layout.addView(name[i]);
            layout.addView(website[i]);
            layout.addView(category[i]);

        }
    } catch (Exception e) {
        System.out.println("XML Pasing Excpetion = " + e);
    }


    setContentView(layout);


}

【问题讨论】:

  • 你看到了什么?
  • 只有一个空白活动
  • 您是否在 AndroidManifest.xml 中定义了 Internet 权限?
  • @Haresh Chhelana- 是的,已经在 Manifest 中定义了它。
  • 您正在主线程上调用服务器请求

标签: android xml-parsing domparser


【解决方案1】:

您不能在UI/Main 线程上执行网络或长时间运行任务,因此您需要使用AsyncTask 在后台线程中执行该任务。尝试从AsyncTask 中的 url 加载 xml,然后将 xml 加载到 UI 线程中的布局中。

public class XMLParsingDOMExample extends Activity {

    LinearLayout layout;
    TextView name[];
    TextView website[];
    TextView category[];

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        loadXML("http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml");
    }


    private void loadXML(final String stringUrl){
        new AsyncTask<Void,Void,Document>(){
            @Override
            protected Document doInBackground(Void... params) {
                try {
                    URL url = new URL(stringUrl);
                    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                    DocumentBuilder db = dbf.newDocumentBuilder();
                    return db.parse(new InputSource(url.openStream()));
                }catch (Throwable e){
                    e.printStackTrace();
                }

                return null;
            }

            @Override
            protected void onPostExecute(Document document) {
                super.onPostExecute(document);
                parseXML(document);
            }
        }.execute();
    }

    private void parseXML(Document doc){

        try {
            doc.getDocumentElement().normalize();

            NodeList nodeList = doc.getElementsByTagName("item");

            name = new TextView[nodeList.getLength()];
            website = new TextView[nodeList.getLength()];
            category = new TextView[nodeList.getLength()];

            for (int i = 0; i < nodeList.getLength(); i++) {

                Node node = nodeList.item(i);

                name[i] = new TextView(this);
                website[i] = new TextView(this);
                category[i] = new TextView(this);

                Element fstElmnt = (Element) node;
                NodeList nameList = fstElmnt.getElementsByTagName("name");
                Element nameElement = (Element) nameList.item(0);
                nameList = nameElement.getChildNodes();
                name[i].setText("Name = "
                        + ((Node) nameList.item(0)).getNodeValue());

                NodeList websiteList = fstElmnt.getElementsByTagName("website");
                Element websiteElement = (Element) websiteList.item(0);
                websiteList = websiteElement.getChildNodes();
                website[i].setText("Website = "
                        + ((Node) websiteList.item(0)).getNodeValue());

                category[i].setText("Website Category = "
                        + websiteElement.getAttribute("category"));

                layout.addView(name[i]);
                layout.addView(website[i]);
                layout.addView(category[i]);
            }

        } catch (Exception e) {
            System.out.println("XML Pasing Excpetion = " + e);
        }
        setContentView(layout);
    }
}

【讨论】:

  • 非常感谢
  • @hiranya,如果答案可以帮助您解决问题,那么您可以接受并投票赞成答案,以便其他有同样问题的人有所帮助。
  • 我在添加评论之前已经这样做了。对我来说还是没有好名声所以就不加了。只有它被记录下来。再次感谢
  • @hiranya,我认为接受回答您不需要任何最低声誉。你不能接受答案吗?
  • 刚刚登录。是的,它有效。以前我点击这个答案很有用
猜你喜欢
  • 1970-01-01
  • 2012-11-13
  • 2016-11-27
  • 1970-01-01
  • 2013-07-04
  • 1970-01-01
  • 2012-04-22
  • 1970-01-01
  • 2023-03-25
相关资源
最近更新 更多