SAX解析器|通过向量解析 xml 数据到数据库

发布于 2024-12-28 01:00:07 字数 6375 浏览 0 评论 0原文

编辑:未完全解决!
如何将每个条目添加到数据库。我想将 Data1、Data2、Data3 等添加到新列中。

在我的实际代码中,在下面,从 res/raw 中的 XML 解析数据就像一个魅力。 但我无法从 URL 获取它。我做错了什么?




我想从网络上的 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;
    }    
}

Edit: Not totally solved!
How I can add every entry to db. I want to add Data1, Data2, Data3 and so on to a new column.

In my actual code, down below, parsing data from the XML in res/raw works like a charm. But I cant get it from a URL. Which I'am doing wrong?

I want to parse data from an XML on the net. The XML looks like:

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

If I parse it on my way (the code below), I only get the last entry data.
The problem is, I want every entry (and its data) in my db. How can I achieve it?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半城柳色半声笛 2025-01-04 01:00:07

请在应用程序中查看您的代码,

case entrys:
                MyParsedExampleDataSets.add(DataSet);
                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;
        }
    }

Please look into your code into application

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

here entrys tag will end only one time in full document. So place this code into entry tag like this:

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;
        }
    }
腹黑女流氓 2025-01-04 01:00:07

尝试像这样解析,这里的 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
    }

Try to parse like this, here doc is nothing but your parsed xml document

    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
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文