91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

android使用DOM和SAXParserFactory來解析XML文件的方法

發布時間:2020-08-21 11:21:41 來源:億速云 閱讀:150 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關android使用DOM和SAXParserFactory來解析XML文件的方法的內容。小編覺得挺實用的,因此分享給大家做個參考。一起跟隨小編過來看看吧。

 對于以下的xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<books>  
    <book email="zhoujunhui">  
        <name>rjzjh</name>  
        <price>jjjjjj</price>  
    </book>  
     <book email="aaaaaaaaa">  
        <name>bbbb</name>  
        <price>ccc</price>  
    </book>  
</books>


這樣來解析:

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<list.size();i++) {
			Book book = list.get(i);
			Log.d("TAG", "name="+book.name+"email="+book.email);
		}
	}


	public void XmlReader() {
		Document doc = null;
		AssetManager assetManager = this.getAssets();
		DocumentBuilder docBuilder = null;
		DocumentBuilderFactory docBuilderFactory = null;
		try {
			docBuilderFactory = DocumentBuilderFactory.newInstance();
			docBuilder = docBuilderFactory.newDocumentBuilder();
			doc = docBuilder.parse(assetManager.open("test1.xml"));
			Element root = doc.getDocumentElement();
			NodeList books = root.getChildNodes();
			if (books != null) {
				for (int i = 0; i < books.getLength(); i++) {
					Node book = books.item(i);
					Book mybook = null;
					if (book.getNodeType() == Node.ELEMENT_NODE) {
						//這里才表示這個是<book>節點
						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<Book> list = new ArrayList<Book>();
	
	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 ArrayList<Book> list = new ArrayList<Book>();

	private class Book {
		String email;
		String name;
		String price;

		@Override
		public String toString() {
			return "name=" + name + "price=" + price;
		}

	}

感謝各位的閱讀!關于android使用DOM和SAXParserFactory來解析XML文件的方法就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

绩溪县| 鹤庆县| 九龙城区| 横峰县| 靖安县| 阳新县| 远安县| 鱼台县| 合山市| 临邑县| 馆陶县| 龙胜| 静宁县| 金沙县| 临江市| 芦山县| 奉新县| 宣化县| 胶州市| 平凉市| 新源县| 余江县| 芦山县| 麻栗坡县| 沙雅县| 泰州市| 云南省| 靖边县| 永新县| 瓮安县| 木兰县| 福建省| 运城市| 杭锦旗| 家居| 马公市| 大荔县| 常宁市| 白城市| 鹿泉市| 忻城县|