我有一个包含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>
非常感谢任何建议,想法或例子。
答案 0 :(得分: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文件夹)。
答案 1 :(得分:0)
不是一个答案,但你也可以考虑使用Simple:http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#deserialize
它有一套非常易于理解的教程,如果我没记错的话,它很小,所以它不应该为你的应用程序增加太多的大小。
与大多数这些XML解析库一样,您需要创建某种数据对象来保存反序列化的数据,并且您需要在该类中添加一些标记,以便让反序列化器知道您想要的数据从XML文件中捕获以及存储它的位置。