How does android parse XML files using DOM and SAXParserFactory?

Source: Internet
Author: User
Tags svm
Opencv provides several classifiers, which are described by character recognition in routines. 1. support vector machine (SVM): Given training samples, the support vector machine creates a hyperplane as the decision plane, this maximizes the isolation edge between the positive and negative examples. Function prototype: training prototype cv2.SVM. train (trainData, responses [, varIdx [, sampleIdx [, params]). trainD has the following xml files:


 
       
            
   
    rjzjh
             
   
    jjjjjj
         
         
            
   
    bbbb
             
   
    ccc
         
    
 


In this way, we will analyze:



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 {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); XmlReader (); for (int I = 0; I
 
  
Node 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
  
   
List = new ArrayList
   
    
(); Private class Book {String email; String name; String price ;}
   
  
 


Using SAXParserFactory:


@Overrideprotected 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;}@Overridepublic 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;}}@Overridepublic void characters(char[] chars, int start, int length) throws SAXException {super.characters(chars, start, length);getBuffer().append(chars, start, length);}@Overridepublic 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
 
   list = new ArrayList
  
   ();private class Book {String email;String name;String price;@Overridepublic String toString() {return "name=" + name + "price=" + price;}}
  
 

The preceding figure shows how android uses DOM and SAXParserFactory to parse the content of the XML file. For more information, see PHP Chinese website (www.php1.cn )!

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.