简体   繁体   English

Java / Android:将xml转换为可循环对象的最简单方法? (地图?)

[英]Java/Android: Easiest way to convert xml to a loopable object? (Map?)

I have a string containing XML content with the following structure. 我有一个包含具有以下结构的XML内容的字符串。 My question is, how do i easily convert it to an object of some sort, and then loop through all of the items within ? 我的问题是,我如何轻松地将其转换为某种对象,然后遍历其中的所有项?

Are there any easy to use libraries that does this? 是否有任何易于使用的库可以做到这一点? If so, examples would be great. 如果是这样,例子将是很好的。 I recently got in to Java developing and i'm still learning. 我最近开始从事Java开发,现在仍在学习。

I just read about XStream ( http://x-stream.github.io/tutorial.html ), and it seems very promising. 我刚刚读到有关XStream的信息( http://x-stream.github.io/tutorial.html ),这似乎很有希望。 I just don't know how to properly apply it. 我只是不知道如何正确应用它。

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

Any suggestions, ideas or examples are very appreciated. 任何建议,想法或例子都非常感谢。

Create a class that defines what the object is: 创建一个定义对象是什么的类:

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

} }

Then use an XML parser to parse through your XML: 然后使用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;
}

This has been wonderful for me. 这对我来说真是太好了。 it's a lot of code, but it works, pretty quickly too actually. 它有很多代码,但是实际上也很快。 I notice no lag time, and there are nearly 30 "terms" in my XML, each with 5 attributes. 我注意到没有滞后时间,并且我的XML中有近30个“术语”,每个术语都有5个属性。 just store your XML under res/xml (you will likely have to create the XML folder). 只需将XML存储在res / xml下(您可能必须创建XML文件夹)。

Not quite an answer, but you could also consider using Simple: http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#deserialize 答案不尽相同,但您也可以考虑使用简单: http : //simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#deserialize

It has a pretty comprehensize set of tutorials and if I remember correctly, it's quite small, so it shouldn't add much size to your application. 它有一套相当全面的教程,如果我没记错的话,它很小,所以它不应为应用程序增加太多大小。

As with most of these XML parsing libraries, you'll need to create a data object of some sort to hold your deserialized data and you'll need to add some tags in that class to let the deserializer know what data you want to capture from the XML file and where to store it. 与大多数此类XML解析库一样,您需要创建某种数据对象来保存反序列化的数据,并且需要在该类中添加一些标签,以使反序列化器知道要从中捕获哪些数据XML文件及其存储位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM