Android memo, for your own use, do not laugh: (3) XML

Source: Internet
Author: User

Parse XML. Android has two mage types: the SAX method (stream and event, using a relatively large XML, which requires performance) and the DOM method (the entire file is loaded and parsed, provides interface operations, using small XML, without performance requirements ).

 

Code snippet:

SAX:

Import javax. xml. parsers. SAXParserFactory
Import javax. xml. parsers. SAXParser

//

SAXParserFactory factory = SAXParserFactory. newInstance ();
SAXParser parser = factory. newSAXParser ();
ParseXML parX = new ParseXML ();
Parser. parse ("F: \ emps. xml", parX );

 

DOM:

Import javax. xml. parsers. DocumentBuilderFactory
Import javax. xml. parsers. DocumentBuilder
//
DocumentBuilderFactory dbf = DocumentBuilderFactory. newInstance ();
DocumentBuilder db = dbf. newDocumentBuilder ();
Doc = db. parse (docFile); // get the document.

 

I often use DOM. The following example code:

 1 InputStream kana = this. _ activity. getResources (). getAssets (). open (_ fileName );
2 Document doc = DocumentBuilderFactory. newInstance (). newDocumentBuilder (). parse (kana );
3 Element root = doc. getDocumentElement (); // get the root node
4 NodeList lstNodes = root. getElementsByTagName (yin). item (0). getChildNodes ();
5 //
6 for (int I = 0; I <lstNodes. getLength (); I ++)
7 {// because there are still empty nodes, pay attention to the node type before conversion.
8 if (lstNodes. item (I). getNodeType () = Node. ELEMENT_NODE)
9 {
10 Element el = (Element) lstNodes. item (I );
11 KanaXml temp = new KanaXml ();
12 temp. Hiragana = el. getAttribute (_ hiragana );
13 temp. Katakana = el. getAttribute (_ katakana );
14 temp. Romaji = el. getAttribute (_ romaji );
15 temp. Sound = el. getAttribute (_ sound );
16 lstXmls. add (temp );
17}
18}

 

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.