Java parses XML in two ways:
** Dom
** Sax
Example:
<?xml version="1.0" encoding="UTF-8"?><users><description>information of users</description><user><name>LiLei</name><sex>1</sex><age>19</age></user><user><name>Han Meimei</name><sex>0</sex><age>18</age></user></users>
Dom mode:
Load XML, and create a DOM tree corresponding to the XML file in the memory.
Obtain the required information based on node and nodelist In the DOM tree.
public boolean parserXml(InputStream is) throws Exception {DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.newDocumentBuilder();Document doc = builder.parse(is);NodeList descNodes = doc.getElementsByTagName("description");NodeList userNodes = doc.getElementsByTagName("user");if (descNodes.getLength() > 0) {// ignore following descriptionsdocument.setDescription(descNodes.item(0).getFirstChild().getNodeValue());}Node node;NodeList nodes;String name;User user;for (int i = 0; i < userNodes.getLength(); i++) {user = new User();nodes = userNodes.item(i).getChildNodes();for (int j = 0; j < nodes.getLength(); j++) {node = nodes.item(j);name = node.getNodeName();if ("name".equals(name)) {user.setName(node.getFirstChild().getNodeValue());} else if ("sex".equals(name)) {user.setSex(Integer.parseInt(node.getFirstChild().getNodeValue()));} else if ("age".equals(name)) {user.setAge(Integer.parseInt(node.getFirstChild().getNodeValue()));}}document.addUser(user);}return true;}
** Getchildnodes () contains white spaces and line breaks. Therefore, the return value of nodes. getlength () is 7. In addition to three subnodes, there are 4 line breaks and tabs. So the first childnode is \ n \ t
** The Name node has one subnode, that is, the # text node of lilei.
Sax method:
Event-driven mode.
Read the XML file stream and trigger corresponding events based on the read node. Such as startdocument (), enddocument (), startelement (), endelement (), and characters ().....
/** * event driven parse xml as an input stream, with a handler as a callback while * meeting each element tag startElement will contain lots of if/else * * @author xuefeng * */public class SaxXmlDemo implements IXmlDocument {private UserDocument document;public SaxXmlDemo() {document = new UserDocument();}public boolean parserXml(InputStream is) throws Exception {SAXParserFactory saxfac = SAXParserFactory.newInstance();SAXParser saxparser = saxfac.newSAXParser();saxparser.parse(is, new MySAXHandler(document));return true;}public static void main(String[] args) throws Exception {SaxXmlDemo demo = new SaxXmlDemo();FileInputStream fis = new FileInputStream("data/test.xml");demo.parserXml(fis);System.out.println();}}class MySAXHandler extends DefaultHandler {private UserDocument document;private User user; // userd for startElement and endElementprivate String curTag; // userd for startElement and endElementpublic MySAXHandler(UserDocument document) {this.document = document;}public void startDocument() throws SAXException {}public void endDocument() throws SAXException {}public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {curTag = qName;if ("user".equals(qName)) {user = new User();document.addUser(user);}}public void endElement(String uri, String localName, String qName)throws SAXException {curTag = null;}public void characters(char[] ch, int start, int length)throws SAXException {if (curTag == null)return;String value = new String(ch, start, length);if ("description".equals(curTag)) {document.setDescription(value);} else if ("name".equals(curTag)) {user.setName(value);} else if ("sex".equals(curTag)) {user.setSex(Integer.parseInt(value));} else if ("age".equals(curTag)) {user.setAge(Integer.parseInt(value));}}}
Where,
public void startElement(String uri, String localName, String qName, Attributes attributes)
Uri: If the parser supports namespaces, The URI is the namespace. If the parser does not support or does not have a namespace, The uri = ""
Localname: If the parser supports namespace, It is the name of the tag; otherwise, it is null.
QNAME: If the tag has a namespace prefix, It is the prefix + tag name. Otherwise, it is the tag name.
Attributes: the attribute list of a tag, which can be accessed through getlength () and getvalue.
Code: SVN: http://hsuehfeng.googlecode.com/svn/trunk/xml/XMLParse/