android development basics tutorial three ways to implement xml file parsing

  • 2020-05-07 20:29:31
  • OfStack

1. sax way
 
/** 
*  use sax parsing  
*/ 
public class SaxParse{ 
/** 
* sax The parser  
*/ 
private SAXParser parser; 
public SaxParse(){ 
try { 
SAXParserFactory f = SAXParserFactory.newInstance(); 
parser = f.newSAXParser(); 
} catch (ParserConfigurationException e) { 
e.printStackTrace(); 
} catch (Exception e) { 
e.printStackTrace(); 
} 
} 
public List<Person> doParse(InputStream is) { 
try { 
XmlHandler h = new XmlHandler(); 
parser.parse(is,h); 
return h.getpersons(); 
} catch (Exception e) { 
e.printStackTrace(); 
} 
return null; 
} 
/** 
*  The processor  
*/ 
class XmlHandler extends DefaultHandler{ 
List<Person> persons = null ; 
Person person = null ; 
// Current element name  
private String currEleName; 
/** 
*  The text node fires the method  
*/ 
public void characters(char[] ch, int start, int length)throws SAXException { 
String str = new String(ch,start,length); 
//name 
if("name".equals(currEleName)){ 
person.name = str ; 
} 
else if("age".equals(currEleName)){ 
person.age = Integer.parseInt(str); 
} 
} 
public void endDocument() throws SAXException { 
} 
/** 
*  End of the element  
*/ 
public void endElement(String uri, String localName, String qName) 
throws SAXException { 
if("person".equals(localName)){ 
persons.add(person); 
} 
// Empty the current element  
else if(("name".equals(currEleName)) || ("age".equals(currEleName))){ 
this.currEleName = "" ; 
} 
} 
/** 
*  Document start event  
*/ 
public void startDocument() throws SAXException { 
persons = new ArrayList<Person>(); 
} 
/** 
*  Element start event  
* localName: Local name  
* uri: Name space  
* qName: Qualified name , The prefix  +  Local name  
*/ 
public void startElement(String uri, String localName, String qName, 
Attributes attributes) throws SAXException { 
// instantiation person object  
if("person".equals(localName)){ 
person = new Person(); 
person.id = Integer.parseInt(attributes.getValue(0)); 
} 
//name The element  
else if("name".equals(localName)){ 
this.currEleName = "name" ; 
} 
//name The element  
else if("age".equals(localName)){ 
this.currEleName = "age" ; 
} 
} 
public List<Person> getpersons(){ 
return persons ; 
} 
} 
} 

2. dom way
 
/** 
* DOM parsing  
*/ 
public class DomParse{ 
// 
private DocumentBuilder builder; 
public DomParse(){ 
try { 
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance(); 
this.builder = f.newDocumentBuilder(); 
} catch (Exception e) { 
e.printStackTrace(); 
} 
} 
public List<Person> doParse(InputStream is) { 
List<Person> persons = new ArrayList<Person>(); 
Person person = null ; 
try { 
Document doc = builder.parse(is); 
NodeList list = doc.getElementsByTagName("person"); 
Element ele = null ; 
for(int i = 0 ; i < list.getLength() ; i ++){ 
ele = (Element) list.item(i); 
person = new Person(); 
person.id = Integer.parseInt(ele.getAttribute("id")); 
person.name = getSubElementTextContent(ele,"name"); 
person.age = Integer.parseInt(getSubElementTextContent(ele,"age")); 
persons.add(person); 
} 
} catch (Exception e) { 
e.printStackTrace(); 
} 
return persons; 
} 
/** 
*  Gets the intermediate text content in the specified resource  
*/ 
private String getSubElementTextContent(Element ele, String tagName) { 
NodeList list = ele.getElementsByTagName(tagName); 
Element e = (Element) list.item(0); 
// I get the middle text node  
return e.getTextContent(); 
} 
} 

3. pull way
 
/** 
* pull parsing , Pull mode , It can be controlled manually 1 Whether or not an event is triggered . 
*/ 
public class PullParse{ 
public List<Person> doParse(InputStream is) { 
List<Person> persons = null ; 
Person person = null ; 
try { 
XmlPullParser parser = Xml.newPullParser(); 
// Set the parse data source  
parser.setInput(is, "utf-8"); 
// Gets the type of the event  
int eventType = parser.getEventType(); 
String eleName = null ; 
while(eventType != XmlPullParser.END_DOCUMENT){ 
switch(eventType){ 
// The beginning of the document  
case XmlPullParser.START_DOCUMENT: 
persons = new ArrayList<Person>(); 
break ; 
// element  
case XmlPullParser.START_TAG: 
eleName = parser.getName(); 
if("person".equals(eleName)){ 
person = new Person(); 
person.id = Integer.parseInt(parser.getAttributeValue(0)); 
} 
else if("name".equals(eleName)){ 
person.name = parser.nextText(); 
} 
else if("age".equals(eleName)){ 
person.age = Integer.parseInt(parser.nextText()); 
} 
break ; 
// End of the tag  
case XmlPullParser.END_TAG: 
eleName = parser.getName(); 
if("person".equals(eleName)){ 
persons.add(person); 
} 
break ; 
} 
// Manually activate the trigger for the next event  
eventType = parser.next(); 
} 
} catch (Exception e) { 
e.printStackTrace(); 
} 
return persons; 
} 
} 

Related articles: