重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章给大家分享的是有关android使用DOM和SAXParserFactory来解析XML文件的方法的内容。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。
鲤城ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!
对于以下的xml文件:
rjzjh jjjjjj bbbb ccc
这样来解析:
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节点 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; }
采用SAXParserFactory:
@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 ArrayListlist = new ArrayList (); private class Book { String email; String name; String price; @Override public String toString() { return "name=" + name + "price=" + price; } }
感谢各位的阅读!关于android使用DOM和SAXParserFactory来解析XML文件的方法就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到吧!