XML解析Android自定义解析器

时间:2014-04-03 12:57:46

标签: android xml xml-parsing android-xml

我试图在android中解析我的XML,我遇到了一些问题。

我需要什么来自XML 我需要得到每个问题

 question id
 question text
 options id
 options text

这是我的xml:

<question id="1" text="Zodiac Sign" >
  <option id="1" >Aries</option>
  <option id="2" >Taurus</option>
  <option id="3" >Gemini</option>
  <option id="4" >Cancer</option>
  <option id="5" >Leo</option>
  <option id="6" >Virgo</option>
  <option id="7" >Libra</option>
  <option id="8" >Scarpio</option>
  <option id="9" >Sagitarius</option>
  <option id="10" >Capricorn</option>
  <option id="11" >Acqauarius</option>
</question><question id="2" text="Home Town" >
</question><question id="3" text="Current Locaion" >
</question><question id="4" text="Nationality" ></question>
<question id="5" text="Height" ></question>
<question id="6" text="Build(optional)" >
  <option id="13" >Slim</option>
  <option id="14" >Athletic</option>
  <option id="15" >Few Pounds Extra</option>
  <option id="16" >Obese</option>
</question><question id="7" text="Hair Color" >
</question><question id="8" text="Color Of Eyes" >
  <option id="23" >Black</option>
  <option id="24" >Brown</option>
  <option id="25" >Blue</option>
</question><question id="9" text="Smoking Habits" >
</question>

这是我的解析代码:

public class ProfileHome extends Activity {

    public String id="";
    public String text="";
    public ArrayList<String>option = new ArrayList<String>();
    public String singleoption="";

    List<Questions> q;
    private Questions question = new Questions();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profile_home);
        String payload = "<question id=\"1\" text=\"Zodiac Sign\" ><option id=\"1\" >Aries</option><option id=\"2\" >Taurus</option><option id=\"3\" >Gemini</option><option id=\"4\" >Cancer</option><option id=\"5\" >Leo</option><option id=\"6\" >Virgo</option><option id=\"7\" >Libra</option><option id=\"8\" >Scarpio</option><option id=\"9\" >Sagitarius</option><option id=\"10\" >Capricorn</option><option id=\"11\" >Acqauarius</option></question><question id=\"2\" text=\"Home Town\" ></question><question id=\"3\" text=\"Current Locaion\" ></question><question id=\"4\" text=\"Nationality\" ></question><question id=\"5\" text=\"Height\" ></question><question id=\"6\" text=\"Build(optional)\" ><option id=\"13\" >Slim</option><option id=\"14\" >Athletic</option><option id=\"15\" >Few Pounds Extra</option><option id=\"16\" >Obese</option></question><question id=\"7\" text=\"Hair Color\" ></question><question id=\"8\" text=\"Color Of Eyes\" ><option id=\"23\" >Black</option><option id=\"24\" >Brown</option><option id=\"25\" >Blue</option></question><question id=\"9\" text=\"Smoking Habits\" ></question>";
        q = new ArrayList<Questions>();

        if(payload.contains("question"))
            {

                for(int i=0;i<payload.length();i++)
                    {
                    Questions ques = new Questions();
                    // check if question has come
                        if(payload.substring(i, i+8).equals("question"))
                        {
                            i=i+9;
                            // check for the id, text and options
                            for(int k=i;k<payload.length();k++)
                            {
                                if(payload.substring(k, k+9).equals("/question"))
                                {
                                    q.add(ques);

                                    k=k+9;
                                    break;
                                }else if(payload.substring(k, k+4).equals("id=\""))
                                {
                                    //get id
                                    for(int j=k+4;j<payload.length();j++)
                                    {
                                        if(payload.substring(j, j+1).equals("\""))
                                        {
                                            ques.setId(id);
                                            id="";
                                            k=j+1;
                                            break;
                                        }else
                                        {
                                            id= id + payload.substring(j, j+1);
                                        }
                                    }
                                    //check for text
                                }else if(payload.substring(k, k+6).equals("text=\""))
                                {
                                    for(int j=k+6;j<payload.length();j++)
                                    {
                                        if(payload.substring(j, j+1).equals("\""))
                                        {
                                            ques.setText(text);
                                            id="";
                                            k=j+1;
                                            text="";
                                            break;
                                        }else
                                        {
                                            text= text + payload.substring(j, j+1);
                                        }
                                    }
                                    //check for options                                 
                                }else if(payload.substring(k, k+11).equals("option id=\""))
                                        {
                                            for(int l=k;l<payload.length();l++)
                                            {
                                                if(payload.substring(k, k+11).equals("</question>"))
                                                {
                                                    ques.setOptions(option);
                                                    q.add(ques);
                                                    k=k+11;
                                                    break;
                                                }
                                            //now get all options and their id's
                                            for(int j=k+11;j<payload.length();j++)
                                            {
                                                //get id and text
                                                if(payload.substring(j, j+9).equals("</option>"))
                                                {
                                                    option.add(singleoption);
                                                    singleoption="";
                                                    k=j+9;
                                                    break;
                                                }else{
                                                    singleoption = singleoption +payload.substring(j,j+1);
                                                }

                                            }
                                            }
                                        }

                            }
                        }
                    }
            }


        for(int i=0;i<q.size();i++)
        {
            q.get(i).getText();
            q.get(i).getId();
            q.get(i).getOptions().toString();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.profile_home, menu);
        return true;
    }

}

问题是:

我能够解析完整的xml,但问题是我尝试使用

在数组列表中保存问题属性
q.add(question)

我之前的参赛作品都是新作品。

有什么办法可以解析这个xml并得到我想要的数据吗?

1 个答案:

答案 0 :(得分:0)

使用SimpleXML(http://simple.sourceforge.net)。

private void doParse()
{
    // static body here, but you can pass a Stream to the serializer too
    private static final String XML_SAMPLE = "<questions><question id=\"1\" text=\"Zodiac Sign\" >  <option id=\"1\" >Aries</option>  <option id=\"2\" >Taurus</option>  <option id=\"3\" >Gemini</option>  <option id=\"4\" >Cancer</option>  <option id=\"5\" >Leo</option>  <option id=\"6\" >Virgo</option>  <option id=\"7\" >Libra</option>  <option id=\"8\" >Scarpio</option>  <option id=\"9\" >Sagitarius</option>  <option id=\"10\" >Capricorn</option>  <option id=\"11\" >Acqauarius</option></question><question id=\"2\" text=\"Home Town\" ></question><question id=\"3\" text=\"Current Locaion\" ></question><question id=\"4\" text=\"Nationality\" ></question><question id=\"5\" text=\"Height\" ></question><question id=\"6\" text=\"Build(optional)\" >  <option id=\"13\" >Slim</option>  <option id=\"14\" >Athletic</option>  <option id=\"15\" >Few Pounds Extra</option>  <option id=\"16\" >Obese</option></question><question id=\"7\" text=\"Hair Color\" ></question><question id=\"8\" text=\"Color Of Eyes\" >  <option id=\"23\" >Black</option>  <option id=\"24\" >Brown</option>  <option id=\"25\" >Blue</option></question><question id=\"9\" text=\"Smoking Habits\" ></question></questions>";


    Serializer serializer = new Persister();
    QuestionList response = null;
    try {
        response = serializer.read(QuestionList.class, XML_SAMPLE);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // something interesting with the classes now
}

<强> QuestionList.java

package com.dshaw.questionparser;

import java.util.List;

import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

@Root(name="questions")
public class QuestionList {

    @ElementList(name="question",inline=true)
    List<Question> questions;

}

<强> Question.java:

package com.dshaw.questionparser;

import java.util.Map;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.ElementMap;
import org.simpleframework.xml.Root;

@Root(name="question")
public class Question {

    @Attribute(required=false)
    Integer id;
    @Attribute(required=false)
    String text;

    @ElementMap(entry="option", key="id", attribute=true, inline=true, required=false)
    private Map<String, String> options;
}

最后,调试器中的输出使您可以看到它如何解析:

Parsed objects in the result