0

次の構造を持つ XML コンテンツを含む文字列があります。私の質問は、それを何らかのオブジェクトに簡単に変換してから、内のすべてのアイテムをループするにはどうすればよいですか?

これを行う使いやすいライブラリはありますか? もしそうなら、例は素晴らしいでしょう。私は最近 Java 開発に取り掛かりましたが、まだ学習中です。

XStream ( http://x-stream.github.io/tutorial.html ) について読んだところですが、非常に有望なようです。それを正しく適用する方法がわかりません。

<requests>
    <0>
        <id>1</id>
        <key>sms_number</key>
        <value>0709601159</value>
    </0>
    <1>
        <id>1</id>
        <key>sms_text</key>
        <value>This is a text message, blablabla.</value>
    </1>
</requests>

提案、アイデア、または例は非常に高く評価されています。

4

2 に答える 2

0

オブジェクトが何であるかを定義するクラスを作成します。

public class Term implements Serializable {


private static final long serialVersionUID = 1L;
private String word;
private String abbr1;
private String abbr2;
private String definition;
private String formula;

public String getWord() {
    return word;
}
public void setWord(String word) {
    this.word = word;
}
public String getAbbr1() {
    return abbr1;
}
public void setAbbr1(String abbr1) {
    this.abbr1 = abbr1;
}
public String getAbbr2() {
    return abbr2;
}
public void setAbbr2(String abbr2) {
    this.abbr2 = abbr2;
}
public String getDefinition() {
    return definition;
}
public void setDefinition(String definition) {
    this.definition = definition;
}
public String getFormula() {
    return formula;
}
public void setFormula(String formula) {
    this.formula = formula;
}

}

次に、XML パーサーを使用して XML を解析します。

public ArrayList<Term> getTerms(){
    ArrayList<Term> termsList = new ArrayList<Term>();

    //create array lists for the four attributes of each term
    ArrayList<String> word = new ArrayList<String>();
    ArrayList<String> abbr1 = new ArrayList<String>();
    ArrayList<String> abbr2 = new ArrayList<String>();
    ArrayList<String> definition = new ArrayList<String>();
    ArrayList<String> formula = new ArrayList<String>();

    //Strings for each of the attributes
    String currentWord = null;
    String currentAbbr1 = null;
    String currentAbbr2 = null;
    String currentDefinition = null;
    String currentFormula = null;



    //try to parse the XML into the four array lists
    try{

        //get the xml document and put it into a reader
        XmlPullParser parser = getResources().getXml(R.xml.terminology);

        //set the eventType and the boolean to test if finished.
        int eventType = parser.getEventType();
        boolean done = false;

        /*
         * while statement that will read through the xml document and includes
         * a switch case block that will go through the tags and put the attributes
         * into the appropriate array lists
         */
        while (eventType !=XmlPullParser.END_DOCUMENT && !done){
            String name = null;
            switch (eventType){
            case XmlPullParser.START_DOCUMENT:
                break;
            case XmlPullParser.START_TAG:
                name = parser.getName();
                if (name.equalsIgnoreCase("terminology")){

                }else if (word != null && abbr1 != null && abbr2 != null && definition != null && formula != null){
                    if (name.equalsIgnoreCase("term")){

                    }else if (name.equalsIgnoreCase("word")){
                        currentWord = new String(parser.getAttributeValue(0));
                    }else if (name.equalsIgnoreCase("abbr1")){
                        currentAbbr1 = new String(parser.getAttributeValue(0));
                    }else if (name.equalsIgnoreCase("abbr2")){
                        currentAbbr2 = new String(parser.getAttributeValue(0));
                    }else if (name.equalsIgnoreCase("definition")){
                        currentDefinition = new String(parser.getAttributeValue(0));
                    }else if (name.equalsIgnoreCase("formula")){
                        currentFormula = new String(parser.getAttributeValue(0));
                    }
                }
                break;
            case XmlPullParser.END_TAG:
                name = parser.getName();
                if (name.equalsIgnoreCase("formula") && currentFormula != null){
                    formula.add(currentFormula);
                }else if (name.equalsIgnoreCase("definition") && currentDefinition != null){
                    definition.add(currentDefinition);
                }else if (name.equalsIgnoreCase("abbr2") && currentAbbr2 != null){
                    abbr2.add(currentAbbr2);
                }else if (name.equalsIgnoreCase("abbr1") && currentAbbr1 != null){
                    abbr1.add(currentAbbr1);
                }else if (name.equalsIgnoreCase("word") && currentWord != null){
                    word.add(currentWord);
                }else if (name.equalsIgnoreCase("term")){
                }else if (name.equalsIgnoreCase("terminology")){
                    done = true;
                }
                break;  
            }
            eventType = parser.next();
        }



    }
    catch (FileNotFoundException e){

    }
    catch (IOException e){

    }
    catch (Exception e){

    }

    for (int i=0; i < word.size(); i++){
        Term term = new Term();
        term.setWord(word.get(i));
        term.setAbbr1(abbr1.get(i));
        term.setAbbr2(abbr2.get(i));
        term.setDefinition(definition.get(i));
        term.setFormula(formula.get(i));
        termsList.add(term);


    }


    return termsList;
}

これは私にとって素晴らしいことです。これは大量のコードですが、実際には非常に迅速に機能します。ラグ タイムはなく、XML には約 30 個の「用語」があり、それぞれに 5 つの属性があります。XML を res/xml に保存するだけです (おそらく XML フォルダーを作成する必要があります)。

于 2012-04-23T17:47:31.130 に答える
0

完全な答えではありませんが、Simple の使用を検討することもできます: http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#deserialize

かなり包括的なチュートリアルのセットがあり、私の記憶が正しければ非常に小さいので、アプリケーションのサイズが大きくなることはありません。

これらの XML 解析ライブラリのほとんどと同様に、逆シリアル化されたデータを保持するために何らかのデータ オブジェクトを作成する必要があります。また、そのクラスにいくつかのタグを追加して、デシリアライザーにキャプチャするデータを知らせる必要があります。 XML ファイルとその保存場所。

于 2012-04-23T17:49:46.330 に答える