14

私は現在、そこからダウンロードして解析したxmlを使用しています。URL自体から直接やりたいと思います。どうやってやるの?

以下に使用しているスニペットを追加しました。スニペットまたは例を教えてください。ありがとうございます。

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
File file = new File(this.getFilesDir(), FILE_EXTRACTED);
Document dom = builder.parse(file);
Element root = dom.getDocumentElement();
root.normalize();
4

3 に答える 3

25

アンドロイドのURLからxmlを解析するために以下のコードを使用してください

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);

    }
}
于 2012-08-22T04:43:37.830 に答える
5

このコードを使用してください。

public class XMLResourceDemo extends ListActivity {

private final static String TAG = XMLResourceDemo.class.getSimpleName();
TextView selection;
ArrayList<String> items = new ArrayList<String>();

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    selection = (TextView) findViewById(R.id.selection);

    try {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();       
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser(); 
        xpp.setInput(new InputStreamReader(
            getUrlData("url")));

        while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {
            Log.i(TAG, "doc started");
            if (xpp.getEventType() == XmlPullParser.START_TAG) {
                if (xpp.getName().equals("entry")) {
                    items.add(xpp.getAttributeValue(0));
                }
            }
            xpp.next();
        }
    } catch (Throwable t) {
        Toast.makeText(this, "Request failed: " + t.toString(),
                Toast.LENGTH_LONG).show();
    }

    setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, items));
}

public InputStream getUrlData(String url) throws URISyntaxException, ClientProtocolException, IOException {

    DefaultHttpClient client = new DefaultHttpClient();
    HttpGet method = new HttpGet(new URI(url));
    HttpResponse res = client.execute(method);
    return res.getEntity().getContent();
}
于 2012-08-22T03:40:02.620 に答える