0

好吧,我有一个需要用数据更新的 android 应用程序...我知道在 android-end 上要做什么来进行 xml 解析...但是当我尝试使用 stringbuilder 通过以下 xml 文件(a)解析数据时( b)我没有在我的应用程序中获取数据......

(a)data.xml

<?xml version="1.0" encoding="utf-8"?>
<document version="first">
<stuff code="firststuff">
<item1 build="first">This is first item.</item1>
<item2 build="second">This is second item.</item2>
<item3 build="third">This is third item.</item3>
</stuff>
<stuff code="secondtstuff">
<item1 build="first">This is first item.</item1>
<item2 build="second">This is second item.</item2>
<item3 build="third">This is third item.</item3>
</stuff>
<stuff code="thirdstuff">
<item1 build="first">This is first item.</item1>
<item2 build="second">This is second item.</item2>
<item3 build="third">This is third item.</item3>
</stuff>
</document>

(b) 我在我的 android 应用程序中使用的字符串...

http://www.xyz.com/data.xml?build=second

以下 3 个文件中的 java 代码 (c)Main (d)HandlingXMLStuff (e)XMLDataCollected

(c)主要

package com.xyz.xmlpar1;

import java.net.URL;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Main extends Activity implements OnClickListener {
    static final String BaseURL="http://www.xyz.com/data.xml?code=";
    Button b;
    TextView tv;
    EditText et1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        et1=(EditText) findViewById(R.id.editText1);
        b=(Button) findViewById(R.id.button1);
        tv=(TextView) findViewById(R.id.textView1);
        b.setOnClickListener(this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {

        // TODO Auto-generated method stub
        String item=et1.getText().toString();
        StringBuilder URL=new StringBuilder(BaseURL);
        URL.append(item);
        String fullURL=URL.toString();
        try{
            URL website=new URL(fullURL);
            SAXParserFactory spf=SAXParserFactory.newInstance();
            SAXParser sp=spf.newSAXParser();
            XMLReader xr=sp.getXMLReader();
            HandlingXMLStuff doingWork=new HandlingXMLStuff();
            xr.setContentHandler(doingWork);
            xr.parse(new InputSource(website.openStream()));
            String information=doingWork.getInformation();
            tv.setText(information);

        }catch(Exception e){
            tv.setText("error");
        }


    }

}

(d)处理XMLStuff

package com.xyz.xmlpar1;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class HandlingXMLStuff extends DefaultHandler {

    XMLDataCollected info=new XMLDataCollected();

    public String getInformation(){
        return info.datatoString();
    }

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        // TODO Auto-generated method stub
        if(localName.equals("code")){
            String element=attributes.getValue("build");
            info.setData(element);
        }
    }

}

(e)XMLDataCollected

package com.xyz.xmlpar1;

public class XMLDataCollected {

    String datac=null;

    public void setData(String c){
        datac=c;
    }

    public String datatoString(){
        return datac;
    }

}

获取“第二个”数据项..

我哪里错了???

4

1 回答 1

0

您应该按照我在此问题的副本上发布的教程进行操作:Parsed XML data not retrieving in android

尽管该教程没有显示获取属性。

简而言之,您startElement应该看起来像这样:

@Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {

          if(localName.equals("stuff")){
            String element=attributes.getValue("code");
            info.setData(element); //element will hold "firststuff" on the first execution

        }
    }

您将需要if为您尝试匹配的每个名称单独设置一个。

Xml 文件通常是这样引用的(使用您的命名约定):

<localName attributeName="attributeValue">textValue</localName>所以if上面的语句将处理行<stuff code="firststuff"> <..."secondstuff">等,但你必须添加更多来处理每个不同的行。

我强烈建议您按照教程完成此操作,尽管通过反复试验,您肯定会学到很多东西。

于 2013-01-20T17:45:44.237 回答