android有一些dom / sax解析器吗?
E.g。 xml文件:
<A>
<B>
</B>
<C>
</C>
</A>
我的文件比较复杂,但这是一个简单的例子 android有一些dom / sax解析器吗?
答案 0 :(得分:1)
是的,Android还支持 SAXParser ,代码与普通的java程序没有区别。
SAX解析器使用回调函数(org.xml.sax.helpers.DefaultHandler)来通知客户端XML文档结构。您应该扩展DefaultHandler并覆盖几个方法来实现xml解析。 要覆盖的方法是
答案 1 :(得分:1)
we can parse xml files in android is very simple. In market there are many xml parsers are available to parse xml data in android. But simplexml is one of the best xml parser in android.
<Employees>
<Employee>
<id>01</id>
<name>jagadeesh</name>
<salary>00000</salary>
</Employee>
<Employee>
<id>02</id>
<name>jaggubai</name>
<salary>00000</salary>
</Employee>
<Employee>
<id>03</id>
<name>jaggudada</name>
<salary>00000</salary>
</Employee>
<Employees>
public class Employee{
@Element
public String id;
@Element
public String name;
@Element
public String salary;
}
@Root
public class Employees{
@ElementList(inline=true, entry="Employee")
public List<Employee> listOfEmployees;
}
答案 2 :(得分:0)
public class SAXParserExample extends DefaultHandler{
List myEmpls;
private String tempVal;
//to maintain context
private Employee tempEmp;
public SAXParserExample(){
myEmpls = new ArrayList();
}
public void runExample() {
parseDocument();
printData();
}
private void parseDocument() {
//get a factory
SAXParserFactory spf = SAXParserFactory.newInstance();
try {
//get a new instance of parser
SAXParser sp = spf.newSAXParser();
//parse the file and also register this class for call backs
sp.parse("employees.xml", this);
}catch(SAXException se) {
se.printStackTrace();
}catch(ParserConfigurationException pce) {
pce.printStackTrace();
}catch (IOException ie) {
ie.printStackTrace();
}
}
/**
* Iterate through the list and print
* the contents
*/
private void printData(){
System.out.println("No of Employees '" + myEmpls.size() + "'.");
Iterator it = myEmpls.iterator();
while(it.hasNext()) {
System.out.println(it.next().toString());
}
}
//Event Handlers
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
//reset
tempVal = "";
if(qName.equalsIgnoreCase("Employee")) {
//create a new instance of employee
tempEmp = new Employee();
tempEmp.setType(attributes.getValue("type"));
}
}
public void characters(char[] ch, int start, int length) throws SAXException {
tempVal = new String(ch,start,length);
}
public void endElement(String uri, String localName, String qName) throws SAXException {
if(qName.equalsIgnoreCase("Employee")) {
//add it to the list
myEmpls.add(tempEmp);
}else if (qName.equalsIgnoreCase("Name")) {
tempEmp.setName(tempVal);
}else if (qName.equalsIgnoreCase("Id")) {
tempEmp.setId(Integer.parseInt(tempVal));
}else if (qName.equalsIgnoreCase("Age")) {
tempEmp.setAge(Integer.parseInt(tempVal));
}
}
public static void main(String[] args){
SAXParserExample spe = new SAXParserExample();
spe.runExample();
}
}
for xml
<Personnel>
<Employee type="permanent">
<Name>Seagull</Name>
<Id>3674</Id>
<Age>34</Age>
</Employee>
<Employee type="contract">
<Name>Robin</Name>
<Id>3675</Id>
<Age>25</Age>
</Employee>
<Employee type="permanent">
<Name>Crow</Name>
<Id>3676</Id>
<Age>28</Age>
</Employee>
</Personnel>
点击此链接:http://www.java-samples.com/showtutorial.php?tutorialid=152