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

不是一个完整的答案,但您也可以考虑使用简单:http ://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#deserialize

它有一套相当全面的教程,如果我没记错的话,它很小,所以它不应该给你的应用程序增加太多的大小。

与大多数这些 XML 解析库一样,您需要创建某种类型的数据对象来保存反序列化的数据,并且您需要在该类中添加一些标签以让反序列化器知道您要从中捕获哪些数据XML 文件及其存储位置。

于 2012-04-23T17:49:46.330 回答