Parse XML files on Android platforms: Sax and pull

Source: Internet
Author: User

Using the event parsing mechanism, Sax occupies a very small amount of memory and is very suitable for mobile devices such as mobile phones. Sax is an event

The event is defined in the contenthandler interface. The common methods of contenthandler include:

// Process text nodes

• Characters (char [] CH, int start, int length)

// Triggered when the node is terminated

• Endelement (string Uri, string localname, string QNAME)

// Triggered when the document is started

• Startdocument ()

// Triggered when parsing a node (QNAME: name of a tag with a namespace prefix)

• Startelement (string Uri, string localname, string QNAME, attributes)

// Triggered when the document is ended

• Enddocument () parsing tool class
public class XmlTool {
public static List<User> readXmlSAX(InputStream inputStream) throws Exception{
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
MyDefaultHandler defaultHandler = new XmlTool().init();
parser.parse(inputStream, defaultHandler);
inputStream.close();
return defaultHandler.getUsers();
}
public MyDefaultHandler init(){
return new MyDefaultHandler();
}

public class MyDefaultHandler extends DefaultHandler{
private List<User> users;
private User user;
private String content;

public List<User> getUsers() {
return users;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
super.characters(ch, start, length);
content = new String(ch,start,length);
}
@Override
public void startDocument() throws SAXException {
users = new ArrayList<User>();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
if("user".equals(localName)){
user = new User();
user.setId(Integer.parseInt(attributes.getValue("id")));
}
}

@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
super.endElement(uri, localName, qName);
if("name".equals(localName)){
user.setName(content);
}else if("password".equals(localName)){
user.setPassword(content);
}else if("user".equals(localName)){
users.add(user);
}
}
}
}

In addition to using Sax to parse XML files, we can also use the pull parser. Android system

Built-in pull parser. The pull parser also uses event parsing.

Main events and methods of the pull parser

Xmlpullparser parser = xml. newpullparser (); // obtain the pull parser.

Parser. setinput (fileinputstream, "UTF-8"); // transmits the XML document as a stream to the parser.

Int eventcode = parser. geteventtype (); // get the code returned by the parsing event

Xmlpullparser. start_document // start document

Xmlpullparser. end_document // end document

Xmlpullparser. start_tag // Start Node

Xmlpullparser. end_tag // End Node

Eventcode = parser. Next (); // parse the next node

Parser. getname () // obtain the node name

Parser. getattributevalue (null, "ID") // obtain node attributes

Parser. nexttext () // obtain the test Text Value

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.