XML解析给出错误

时间:2014-04-25 01:09:03

标签: android xml-parsing

我正在尝试在我的android项目中解析在线XML,但是我收到了一个错误。我尝试将xml解析为一个基本的java项目,它可以工作,但我不知道如何在我的android项目中实现它。控制台上的错误说android.os.networkmainthreadException。我确实添加了对App manifest的权限。我的代码如下:

package com.example.medrec;

import java.io.IOException;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
import android.os.Build;

public class Details extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_details);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }   
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.details, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_details,
                    container, false);

            //Code to parse starts here
            String name[] = null;
            String id[] = null;
            String type[] = null;
            String dosage[] = null;
            String remaining[] = null;

            DocumentBuilderFactory builderFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder builder = null;
            try {
                builder = builderFactory.newDocumentBuilder();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            }

            Document document = null;
            String xmlDocument = "http://www2.southeastern.edu/Academics/Faculty/jburris/emr.xml";
            try {
                document = builder.parse(new URL(xmlDocument).openStream());
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


            int total = 0;

            // Creates a list of nodes for the root element, emr
            NodeList emrRoot = document.getElementsByTagName("emr");
            // Gets the first node in the list... really just our root node
            Node emrNode = (Node) emrRoot.item(0);
            if (emrNode instanceof Element) {
                Element emrElement = (Element) emrNode;
                // Gets the list of all patient_info nodes
                NodeList patInfoList = emrElement
                        .getElementsByTagName("patient_order");

                // Total Number of Records
                total = patInfoList.getLength();
                System.out.println("Total number of records is: " + total);

                name = new String[total];
                id = new String[total];
                type = new String[total];
                dosage = new String[total];
                remaining = new String[total];
                // Gets the first patient_info node... the first in the list of
                // patients
                // Fill in the values of our strings. The first is tricky because
                // it is an attribute and not the content of the element.
                for (int i = 0; i < total; i++) {
                    Node patInfoNode = (Node) patInfoList.item(i);
                    if (patInfoNode instanceof Element) {
                        // Get the element information for parient_info
                        Element pat_info = (Element) patInfoNode;
                        // Get the name...
                        Node n = pat_info.getElementsByTagName("patientName").item(
                                0);
                        name[i] = n.getTextContent();
                        // Get the id...
                        n = pat_info.getElementsByTagName("patientID").item(0);
                        id[i] = n.getTextContent();
                        // Get the type...
                        n = pat_info.getElementsByTagName("type").item(0);
                        type[i] = n.getTextContent();
                        // Get the dosage...
                        n = pat_info.getElementsByTagName("dosage").item(0);
                        dosage[i] = n.getTextContent();
                        // Get the remaining...
                        n = pat_info.getElementsByTagName("refillsremaining").item(
                                0);
                        remaining[i] = n.getTextContent();
                    }
                }
            }

            //Toast.makeText(getApplicationContext(), type[5], Toast.LENGTH_SHORT).show();


            //Code ends here

            return rootView;
        }
    }

}

使用asyncTask的新代码

package com.example.medrec;

import java.io.IOException;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
import android.os.Build;

public class Details extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_details);
        new LongOperation().execute("");
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }   
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.details, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    private class LongOperation extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {
            String name[] = null;
            String id[] = null;
            String type[] = null;
            String dosage[] = null;
            String remaining[] = null;

            DocumentBuilderFactory builderFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder builder = null;
            try {
                builder = builderFactory.newDocumentBuilder();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            }

            Document document = null;
            String xmlDocument = "http://www2.southeastern.edu/Academics/Faculty/jburris/emr.xml";
            try {
                document = builder.parse(new URL(xmlDocument).openStream());
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


            int total = 0;

            // Creates a list of nodes for the root element, emr
            NodeList emrRoot = document.getElementsByTagName("emr");
            // Gets the first node in the list... really just our root node
            Node emrNode = (Node) emrRoot.item(0);
            if (emrNode instanceof Element) {
                Element emrElement = (Element) emrNode;
                // Gets the list of all patient_info nodes
                NodeList patInfoList = emrElement
                        .getElementsByTagName("patient_order");

                // Total Number of Records
                total = patInfoList.getLength();
                System.out.println("Total number of records is: " + total);

                name = new String[total];
                id = new String[total];
                type = new String[total];
                dosage = new String[total];
                remaining = new String[total];
                // Gets the first patient_info node... the first in the list of
                // patients
                // Fill in the values of our strings. The first is tricky because
                // it is an attribute and not the content of the element.
                for (int i = 0; i < total; i++) {
                    Node patInfoNode = (Node) patInfoList.item(i);
                    if (patInfoNode instanceof Element) {
                        // Get the element information for parient_info
                        Element pat_info = (Element) patInfoNode;
                        // Get the name...
                        Node n = pat_info.getElementsByTagName("patientName").item(
                                0);
                        name[i] = n.getTextContent();
                        // Get the id...
                        n = pat_info.getElementsByTagName("patientID").item(0);
                        id[i] = n.getTextContent();
                        // Get the type...
                        n = pat_info.getElementsByTagName("type").item(0);
                        type[i] = n.getTextContent();
                        // Get the dosage...
                        n = pat_info.getElementsByTagName("dosage").item(0);
                        dosage[i] = n.getTextContent();
                        // Get the remaining...
                        n = pat_info.getElementsByTagName("refillsremaining").item(
                                0);
                        remaining[i] = n.getTextContent();
                    }
                }
            }

            Toast.makeText(getApplicationContext(), type[5], Toast.LENGTH_SHORT).show();




            return null;
        }

        @Override
        protected void onPostExecute(String result) {
        }

        @Override
        protected void onPreExecute() {
        }

        @Override
        protected void onProgressUpdate(Void... values) {
        }
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_details,
                    container, false);



            return rootView;
        }
    }

}

enter image description here

2 个答案:

答案 0 :(得分:1)

这是一个NullPoint错误,我发现你可能在pat_info.getElementsByTagName(“refillsremaining”),“refillsremaining”应该是“refillsrRmaining”.r是R。

答案 1 :(得分:0)

NetworkOnMainThreadException表示您正在尝试在UI线程上执行长时间阻塞I / O操作。将XML下载和解析移动到自己的线程 - 我建议AsyncTask完成工作。操作完成后,AsyncTask将向您的UI线程发出回调。

AsyncTask示例:AsyncTask Android example

另一个例子:How to fix android.os.NetworkOnMainThreadException?