


How android uses DOM and SAXParserFactory to parse XML files
For the following xml file:
##
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <books> <book email="zhoujunhui"> <name>rjzjh</name> <price>jjjjjj</price> </book> <book email="aaaaaaaaa"> <name>bbbb</name> <price>ccc</price> </book> </books>
parse like this:
import java.util.ArrayList; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.res.AssetManager; import android.os.Bundle; import android.util.Log; public class TestActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); XmlReader(); for(int i=0;i<list.size();i++) { Book book = list.get(i); Log.d("TAG", "name="+book.name+"email="+book.email); } } public void XmlReader() { Document doc = null; AssetManager assetManager = this.getAssets(); DocumentBuilder docBuilder = null; DocumentBuilderFactory docBuilderFactory = null; try { docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilder = docBuilderFactory.newDocumentBuilder(); doc = docBuilder.parse(assetManager.open("test1.xml")); Element root = doc.getDocumentElement(); NodeList books = root.getChildNodes(); if (books != null) { for (int i = 0; i < books.getLength(); i++) { Node book = books.item(i); Book mybook = null; if (book.getNodeType() == Node.ELEMENT_NODE) { //这里才表示这个是<book>节点 mybook = new Book(); String email = book.getAttributes() .getNamedItem("email").getNodeValue(); mybook.email = email; for (Node node = book.getFirstChild(); node != null; node = node .getNextSibling()) { if (node.getNodeType() == Node.ELEMENT_NODE) { if (node.getNodeName().equals("name")) { String name1 = node.getFirstChild() .getNodeValue(); mybook.name = name1; } if (node.getNodeName().equals("price")) { String price = node.getFirstChild() .getNodeValue(); mybook.price = price; } } } list.add(mybook); } } } } catch (Exception e) { e.printStackTrace(); } } private ArrayList<Book> list = new ArrayList<Book>(); private class Book{ String email; String name; String price; }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SAXParserFactory factory = SAXParserFactory.newInstance(); try { SAXParser saxParser = factory.newSAXParser(); InputStream is = this.getAssets().open("test1.xml", Context.MODE_PRIVATE); saxParser.parse(is, new MyDefaultHandler()); } catch (Exception ex) { } System.out.println("===size=" + list.size()); for (int i = 0; i < list.size(); i++) { Book book = list.get(i); Log.d("TAG", "name=" + book.name + "email=" + book.email); } } protected String getElementName(String name, String qName) { if ("".equals(name)) { return qName; } else { return name; } } class MyDefaultHandler extends DefaultHandler { private StringBuffer buf; private Book mBook; public MyDefaultHandler() { super(); mBook = new Book(); } protected StringBuffer getBuffer() { return this.buf; } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { super.startElement(uri, localName, qName, attributes); buf = new StringBuffer(); for (int i = 0; i < attributes.getLength(); i++) { String value = attributes.getValue(i); mBook.email = value; } } @Override public void characters(char[] chars, int start, int length) throws SAXException { super.characters(chars, start, length); getBuffer().append(chars, start, length); } @Override public void endElement(String uri, String localName, String qName) throws SAXException { super.endElement(uri, localName, qName); String elementName = getElementName(localName, qName); if (elementName.equals("book")) { list.add(mBook); mBook = new Book(); } else if (elementName.equals("name")) { mBook.name = getBuffer().toString(); } else if (elementName.equals("price")) { mBook.price = getBuffer().toString(); } } } private ArrayList<Book> list = new ArrayList<Book>(); private class Book { String email; String name; String price; @Override public String toString() { return "name=" + name + "price=" + price; } }

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

OnLeaks has now partnered with Android Headlines to provide a first look at the Galaxy S25 Ultra, a few days after a failed attempt to generate upwards of $4,000 from his X (formerly Twitter) followers. For context, the render images embedded below h

Alongside announcing two new smartphones, TCL has also announced a new Android tablet called the NXTPAPER 14, and its massive screen size is one of its selling points. The NXTPAPER 14 features version 3.0 of TCL's signature brand of matte LCD panels

The Vivo Y300 Pro just got fully revealed, and it's one of the slimmest mid-range Android phones with a large battery. To be exact, the smartphone is only 7.69 mm thick but features a 6,500 mAh battery. This is the same capacity as the recently launc

Samsung has not offered any hints yet about when it will update its Fan Edition (FE) smartphone series. As it stands, the Galaxy S23 FE remains the company's most recent edition, having been presented at the start of October 2023. However, plenty of

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

The Redmi Note 14 Pro Plus is now official as a direct successor to last year'sRedmi Note 13 Pro Plus(curr. $375 on Amazon). As expected, the Redmi Note 14 Pro Plus heads up the Redmi Note 14 series alongside theRedmi Note 14and Redmi Note 14 Pro. Li

OnePlus'sister brand iQOO has a 2023-4 product cycle that might be nearlyover; nevertheless, the brand has declared that it is not done with itsZ9series just yet. Its final, and possibly highest-end,Turbo+variant has just beenannouncedas predicted. T
