从Android中的URL解析XML数据,未预期的令牌位置:TEXT错误

时间:2015-12-11 06:44:23

标签: android xml xml-parsing xmlpullparser

来自网址的

的Xml数据
<ArrayOfProduct xmlns:i="http://www.w3.org/2001/XMLSchema-instance" >
    <Product>
    <Contact>98778499</Contact>
    <ID>1</ID>
    <Msg>1</Msg>
    <Name>Manojkumar</Name>
    </Product>
    <Product>
    <Contact>123456789</Contact>
    <ID>2</ID>
    <Msg>1</Msg>
    <Name>kumar</Name>
    </Product>
    <Product>
    <Contact>987654321</Contact>
    <ID>3</ID>
    <Msg>1</Msg>
    <Name>Swatantra</Name>
    </Product>
    <Product>
    <Contact>1111111111</Contact>
    </ArrayOfProduct>

我正在从我的Android应用程序中的URL进行XML解析。它低于错误,

意外的令牌(位置:TEXT [{“ID”:1,“名称”:... @ 1:437,位于va.io.InputStreamReader@416bdd60)

org.xml.sax.SAXParseException:意外的令牌(位置:TEXT [{“ID”:1,“名称”:... @ 1:437,java.io.InputStreamReader@42621690)

我提到的代码,请帮我解决这个问题。

编辑1:

我尝试了Google的其他解决方案,但无法修复此问题。

Link I referred

MainActivity.java

public class MainActivity extends Activity {
    // Declare variables
    TextView textview;
    NodeList nodelist;
    ProgressDialog pDialog;
    // Insert image URL
    String URL = "http://www.androidbegin.com/tutorial/XMLParseTutorial.xml";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from activity_main.xml
        setContentView(R.layout.activity_main);
        // Locate a TextView in your activity_main.xml layout
        textview = (TextView) findViewById(R.id.text);
        // Execute DownloadXML AsyncTask
        new DownloadXML().execute(URL);
    }

    // DownloadXML AsyncTask
    private class DownloadXML extends AsyncTask<String, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Create a progressbar
            pDialog = new ProgressDialog(MainActivity.this);
            // Set progressbar title
            pDialog.setTitle("Android Simple XML Parsing using DOM Tutorial");
            // Set progressbar message
            pDialog.setMessage("Loading...");
            pDialog.setIndeterminate(false);
            // Show progressbar
            pDialog.show();
        }

        @Override
        protected Void doInBackground(String... Url) {
            try {
                URL url = new URL(Url[0]);
                DocumentBuilderFactory dbf = DocumentBuilderFactory
                        .newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                // Download the XML file
                Document doc = db.parse(new InputSource(url.openStream()));
                doc.getDocumentElement().normalize();
                // Locate the Tag Name
                nodelist = doc.getElementsByTagName("item");

            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;

        }

        @Override
        protected void onPostExecute(Void args) {

            for (int temp = 0; temp < nodelist.getLength(); temp++) {
                Node nNode = nodelist.item(temp);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;
                    // Set the texts into TextViews from item nodes
                    // Get the title
                    textview.setText(textview.getText() + "Title : "
                            + getNode("title", eElement) + "\n" + "\n");
                    // Get the description
                    textview.setText(textview.getText() + "Description : "
                            + getNode("description", eElement) + "\n" + "\n");
                    // Get the link
                    textview.setText(textview.getText() + "Link : "
                            + getNode("link", eElement) + "\n" + "\n");
                    // Get the date
                    textview.setText(textview.getText() + "Date : "
                            + getNode("date", eElement) + "\n" + "\n" + "\n"
                            + "\n");
                }
            }
            // Close progressbar
            pDialog.dismiss();
        }
    }

    // getNode function
    private static String getNode(String sTag, Element eElement) {
        NodeList nlList = eElement.getElementsByTagName(sTag).item(0)
                .getChildNodes();
        Node nValue = (Node) nlList.item(0);
        return nValue.getNodeValue();
    }
}

0 个答案:

没有答案