0

在下面的代码中,尝试在 DOM 中解析 XML 时出现空白屏幕。我也没有在清单中设置任何权限......请帮助!我有我的清单权利。

它工作正常......

package com.example.techgeek;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import android.os.*;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

 public class second extends ListActivity {
    String name;
    public static String pass,pass2;
    String[] getname=new String[100];
    Element element2;Node node;NodeList nList;
    ArrayList<String> list = new ArrayList<String>();
    ArrayList<String> list2 = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        try {
            //InputStream is = getAssets().open("file.xml");
            URL url = new URL("http://sre.hostoi.com/xml_eg.xml");

            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            //Document doc = dBuilder.parse(is);
            Document doc = dBuilder.parse(new InputSource(url.openStream()));
            Element element=doc.getDocumentElement();
            element.normalize();

            nList = doc.getElementsByTagName("employee");
            for (int i=0; i < nList.getLength(); i++) {
                node = nList.item(i);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    element2 = (Element) node;
                    list.add((getValue("name", element2)+"\n"));
                    list2.add((getValue("salary", element2)+"\n"));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        setListAdapter (new ArrayAdapter<String>(
                            second.this, android.R.layout.simple_list_item_1,list));                
    }

    private static String getValue(String tag, Element element) {
        NodeList nodeList = element.getElementsByTagName(tag).item(0).getChildNodes();
        Node node = (Node) nodeList.item(0);
        return node.getNodeValue();
    }
4

1 回答 1

0

试试这个..在尝试捕获并确保从xml dom解析你是否获得所有值之后..我希望这会有所帮助..

    LinearLayout linearLayout = new LinearLayout(this);
    ListView  listView = new ListView(this);
    ArrayAdapter<String> aR = new ArrayAdapter<String>(second.this, android.R.layout.simple_list_item_1,list);
    listView.setAdapter(aR);
    linearLayout.addView(listView);
    setContentView(linearLayout);
于 2013-10-04T16:07:26.807 回答