SAXParser |通过向量解析xml数据到db

时间:2012-01-17 21:50:23

标签: java android xml saxparser

编辑:尚未完全解决!
如何将每个条目添加到db 。我想将Data1,Data2,Data3等添加到新列。

在我的实际代码中,在下面,解析res / raw中的XML数据就像一个魅力。 但是我无法通过网址获取它。哪个我做错了?




我想从网上解析XML中的数据。 XML看起来像:

<entrys>
  <entry>
    <data1>a number</data1>
    <data2>a name</data2>
    <data3>a webUrl</data3>
    <data4>a streamUrl</data4>
    <data5>a logoUrl</data5>
  </entry>
  <entry>
    <data1>other number</data1>
    <data2>other name</data2>
    <data3>other webUrl</data3>
    <data4>other streamUrl</data4>
    <data5>other logoUrl</data5>
  </entry>

<-- more entrys like this, up to one number defined in another xml -->
</entrys>

如果我在途中解析它(下面的代码),我只会得到最后一个输入数据。
问题是,我希望我的数据库中的每个条目(及其数据)。我怎样才能实现呢?

class ParsedExampleDataSet {
    private String data1;
    private String data2;
    private String data3;
    private String data4;
    private String data5;

    public String toString() {
        return "entry ID: " + data1 + "entry Name: "
                + data2 + "entry webUrl: " + data3
                + "entry streamUrl: " + data4
                + "entry Logo: " + data5;
    }

    public String getdata1() { return data1; }
    public void setdata1(String data1) { this.data1 = data1; }
    public String getdata2() { return data2; }
    public void setdata2(String data2) { this.data2 = data2; }
    public String getdata3() { return data3; }
    public void setdata3(String data3) { this.data3 = data3; }
    public String getdata4() { return data4; }
    public String getdata5() { return data5; }
    public void setdata4(String data4) {
        this.data4 = data4;
    }
    public void setdata5(String data5) {
        this.data5 = data5;
    }
}

class ContentHandler extends DefaultHandler {
    private enum Tags {
        entry, data1, data2, data3, data4, data5, entrys
    }

    private boolean in_entryTag = false;
    private boolean in_entrys = false; //opened at document start, closed at document end
    private boolean in_entry = false; //opened at new entry, closed after entry completet
    private boolean in_data1 = false; //the entry id
    private boolean in_data2 = false; //entry Name
    private boolean in_data3 = false; //entry weburl
    private boolean in_data4 = false; //entry streamUrl
    private boolean in_data5 = false; // Url to entry logo

    private ParsedExampleDataSet DataSet;
    private Vector<ParsedExampleDataSet> MyParsedExampleDataSets;


    public ContentHandler() {
        super();
        this.MyParsedExampleDataSets = new Vector<ParsedExampleDataSet>();
    }

    public Vector<ParsedExampleDataSet> getParsedExampleDataSets() {
        return this.MyParsedExampleDataSets;
    }

    public void startDocument() throws SAXException { }
    public void endDocument() throws SAXException { }

    public void startElement(String n, String l, String q, Attributes a) {
         switch(Tags.valueOf(l)) {
             case entry:
                 in_entryTag = true;
                 DataSet = new ParsedExampleDataSet();
                 break;
             case data1:
                 in_data1 = true;
                 break;
             case data2:
                 in_data2 = true;
                 break;
             case data3:
                 in_data3 = true;
                 break;
             case data4:
                 in_data4 = true;
                 break;
             case data5:
                 in_data5 = true;
                 break;
             case entrys:
                 break;
         }
    }

    public void endElement(String n, String l, String q) {
        switch(Tags.valueOf(l)) {
            case entry:
                in_entryTag = false;
                MyParsedExampleDataSets.add(DataSet);
                break;             
            case data1:
             in_data1 = false;
             break;      
            case data2:
             in_data2 = false;
             break;              
            case data3:
             in_data3 = false;
             break;              
            case data4:
             in_data4 = false;
             break;              
            case data5:
             in_data5 = false;
             break;              
            case entrys:
                break;
        }
    }

    public void characters(char ch[], int start, int length) {
        if(this.in_data1){
            DataSet.setdata1(new String(ch, start, length));
        }else if(this.in_data2) {
            DataSet.setdata2(new String(ch, start, length));
        }else if(this.in_data3) {
            DataSet.setdata3(new String(ch, start, length));
        }else if(this.in_data4) {
            DataSet.setdata4(new String(ch, start, length));
        }else if(this.in_data5) {
            DataSet.setdata5(new String(ch, start, length));
        }

    }
}

public class XMLparserSample extends Activity {
    private TextView myTextView;
    private static final String TAG = XMLparserSample.class.getSimpleName();

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

        setContentView(R.layout.main);

        Vector<ParsedExampleDataSet> test = read(getResources().openRawResource(R.raw.sample));

        String text = "";
        for(int i=0; i < test.size(); i++)
            text += test.get(i).toString();

        myTextView = (TextView) findViewById(R.id.tv);        
        myTextView.setText(text);
        Log.d(TAG, "vector = " + test);
    }

    public Vector<ParsedExampleDataSet> read(InputStream in) {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp;
        try {
            sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();
            ContentHandler ch = new ContentHandler();
            xr.setContentHandler(ch);
            xr.parse(new InputSource(in));
            return ch.getParsedExampleDataSets();                
        } catch (ParserConfigurationException e) {
        } catch (SAXException e) {
        } catch (IOException e) {}
        return null;
    }    
}

2 个答案:

答案 0 :(得分:1)

请查看您的代码到应用程序

case entrys:
                MyParsedExampleDataSets.add(DataSet);
                break;

这里的entrys标签只会在完整文档中结束一次。所以将此代码放入条目标记中,如下所示:

public void endElement(String n, String l, String q) {
        switch(Tags.valueOf(l)) {
            case entry:
                in_entryTag = false;
                MyParsedExampleDataSets.add(DataSet);

                break;             
            case data1:
             in_data1 = false;
             break;      
            case data2:
             in_data2 = false;
             break;              
            case data3:
             in_data3 = false;
             break;              
            case data4:
             in_data4 = false;
             break;              
            case data5:
             in_data5 = false;
             break;              
            case entrys:
                break;
        }
    }

答案 1 :(得分:0)

尝试像这样解析,这里doc只是你解析的xml文档

    NodeList entrysnodeList = doc.getElementsByTagName("entrys");   
        for (int i = 0; i < ccnodeList.getLength(); i++) {
//loop through the results

// Again take the nodelist for entry like above in the same loop
// then read one by one
    }