SAX parser vs XMLPull parser

后端 未结 7 1286
长情又很酷
长情又很酷 2020-12-12 16:38

I understand the difference between how the SAX parser works vs the XMLPull parser. In fact there\'s a pretty good explanation here:

http://www.firstobject.com/xml-r

7条回答
  •  南笙
    南笙 (楼主)
    2020-12-12 17:02

    I found better and more efficient output while using SAX rather than XMLPullParser... My scenario is to parse the attributes under a XML tag, i could do it easily and insert it into Database smoothly... I think it depends on situations, when i need to write on a XML file i prefer DOM Parser...

    public void startElement(String uri, String localName, String qName,
                Attributes attributes) throws SAXException {
            currentElement = true;
            db = new DatabaseHelper(thecontext);
            if (qName.equals("Asa.Amms.Data.Entity.User")) {
                int length = attributes.getLength();
                for (int i = 0; i < length; i++) {
                    String name = attributes.getQName(i);
                    if (name.equals("Id")) {
                        id = Integer.parseInt(attributes.getValue(i));
                    }
                    if (name.equals("Login")) {
                        LoginID = attributes.getValue(i).toString();
                    }
                    if (name.equals("Name")) {
                        Name = attributes.getValue(i).toString();
                    }
                    if (name.equals("Password")) {
                        Password = attributes.getValue(i).toString();
                    }
                    if (name.equals("ProgramOfficerId")) {
                        user_ProgramOfficerId = Integer.parseInt(attributes.getValue(i).toString());
                    }
                }
                Log.i("Baal dhukbe", id + LoginID + Name + Password);
    
                db.insertUser(id, LoginID, Name, Password, user_ProgramOfficerId);
            }
    }
    

提交回复
热议问题