这是我的xml,
<root>
<child>
<Lunchmenu>
<id>2</id>
<lunch_date>2013-10-24</lunch_date>
<break_name>Lunch</break_name>
<class_id/>
<school_id>1</school_id>
<batch_id/>
</Lunchmenu>
<Eatable>
<id>2</id>
<eatable_name>Apples</eatable_name>
</Eatable>
</child>
<child>
<Lunchmenu>
<id>2</id>
<lunch_date>2013-10-24</lunch_date>
<break_name>Lunch</break_name>
<class_id/>
<school_id>1</school_id>
<batch_id/>
</Lunchmenu>
<Eatable>
<id>3</id>
<eatable_name>Orange</eatable_name>
</Eatable>
</child>
我需要知道,有没有办法解析上面xml的孩子,因为还有两个单独的标签,如Lunchmenu和eatable。
谢谢
我的解析器类是: SchoolParser.java
公共类SchoolParser扩展了DefaultHandler {
private List<School> schoolListData ;
private boolean isSuccess;
private School school;
StringBuilder tempData;
@Override
public void startDocument() throws SAXException {
super.startDocument();
Log.e("StudentListParser","startDocument");
schoolListData = new ArrayList<School>();
}
public List<School> getSchoolListData(){
return schoolListData;
}
@Override
public void startElement(String uri, String localName, String qName,
org.xml.sax.Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
if (localName.equals("School")) {
school = new School();
Log.e("SchoolParser", "-----START----");
}
tempData = new StringBuilder();
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
super.characters(ch, start, length);
tempData.append(new String(ch, start, length));
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
super.endElement(uri, localName, qName);
if(localName.equals("School")){
schoolListData.add(school);
Log.d("localName",localName);
}
else if (localName.equals("id")) {
Log.e("id", localName);
school.id = Integer.parseInt(tempData.toString());
} else if (localName.equals("school_name")) {
school.schoolName = tempData.toString();
Log.e("name", localName);
} else if (localName.equals("logo")) {
school.logo = tempData.toString().getBytes();
Log.e("logo", localName);
}else if (localName.equals("phone")) {
school.phn_no = tempData.toString();
Log.e("phn no", localName);
}
else if (localName.equals("School")) {
Log.e("SchoolParser", "----END---");
}
// int size = buffer.length();
// buffer.delete(0, size);
// Log.i("buffer is empty", ""+buffer.toString());
}
@Override
public void endDocument() throws SAXException {
super.endDocument();
isSuccess = false;
Log.e("StudentListParser", "endDocument");
}
}
答案 0 :(得分:0)
可能是您可以使用序列化逻辑来解析xml以指导java类对象。
你可以使用以下课程..
package com.lib.android.util;
import java.io.InputStream;
import org.apache.log4j.Logger;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import com.lib.android.util.logger.PLogger;
public class XmlParser
{
private static Logger log = new PLogger(XmlParser.class).getLogger();
public static Object Parse(Class<? extends Object> parseInto, InputStream is)
{
Serializer serializer = new Persister();
try
{
Object obj = serializer.read(parseInto, is);
return obj;
} catch (Exception e)
{
log.error(e.getMessage());
}
return null;
}
}
希望它会帮助你..