English 中文(简体)
Android 不是工作,而是工作
原标题:Android XML Parser not working but runs

我仍不敢确定问题是什么......它只是什么,但却没有显示。 想法?

package com.androidpeople.xml.parsing;

import java.net.URL;

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

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 android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

public class XMLParsingDOMExample extends Activity {

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

  /** Create a new layout to display the view */
  LinearLayout layout = new LinearLayout(this);
  layout.setOrientation(1);

  /** Create a new textview array to display the results */
  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");

   /** Assign textview array lenght by arraylist size */
   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);
  }

  /** Set the layout view to display */
  setContentView(layout);

 }
}
问题回答

准许使用安妮斯。 XML档案:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

你的法典运作良好。

完整的XML是:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.androidpeople.xml.parsing"
      android:versionCode="1"
      android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>


    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".XMLParsingDOMExample"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>




相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

How to get instance from string in C#?

Is it possible to get the property of a class from string and then set a value? Example: string s = "label1.text"; string value = "new value"; label1.text = value; <--and some code that makes ...

XML DOM parsing br tag

I need to parse a xml string to obtain the xml DOM, the problem I m facing is with the self closing html tag like <br /> giving me the error of Tag mismatch expected </br>. I m aware this ...

Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

Locating specific string and capturing data following it

I built a site a long time ago and now I want to place the data into a database without copying and pasting the 400+ pages that it has grown to so that I can make the site database driven. My site ...

热门标签