下面简单的说明使用DOM如何解析XML格式数据,主要代码如下:
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(inputStream); Element root = document.getDocumentElement(); NodeList nodes = root.getElementsByTagName("person"); for(int i = 0; i < nodes.getLength(); i++) { Element personElement = (Element) nodes.item(i); System.out.println("****************************"); System.out.println("id:" + personElement.getAttribute("id")); stringBuffer.append("*************************\n").append("id:" + personElement.getAttribute("id") + "\n"); NodeList childNodes = personElement.getChildNodes(); for(int y = 0; y < childNodes.getLength(); y++) { Node childNode = childNodes.item(y); if(childNode.getNodeType() == Node.ELEMENT_NODE) { Element childElement = (Element) childNode; if("name".equals(childElement.getNodeName())) { System.out.println("name:" + childElement.getFirstChild().getNodeValue()); stringBuffer.append("name:" + childElement.getFirstChild().getNodeValue() + "\n"); } else if("address".equals(childElement.getNodeName())) { System.out.println("address:" + childElement.getFirstChild().getNodeValue()); stringBuffer.append("address:" + childElement.getFirstChild().getNodeValue() + "\n"); } } } }
被解析的XML文件内容如下:
<persons> <person id="1"> <name>张三</name> <address>安徽</address> </person> <person id="2"> <name>李四</name> <address>上海</address> </person> <person id="3"> <name>王五</name> <address>北京</address> </person> </persons>
解析的结果如下:
Activity代码如下:
import java.io.IOException; import java.io.InputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.widget.TextView; public class AndroidDOMActivity extends Activity { private Handler mHandler = new Handler(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(5 * 000); mHandler.post(new Runnable() { @Override public void run() { try { ((TextView)findViewById(R.id.display)).setText(parserXml(getAssets().open("data.xml"))); } catch (IOException e) { e.printStackTrace(); } } }); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); } private String parserXml(InputStream inputStream) { StringBuffer stringBuffer = new StringBuffer(); try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(inputStream); Element root = document.getDocumentElement(); NodeList nodes = root.getElementsByTagName("person"); for(int i = 0; i < nodes.getLength(); i++) { Element personElement = (Element) nodes.item(i); System.out.println("****************************"); System.out.println("id:" + personElement.getAttribute("id")); stringBuffer.append("*************************\n").append("id:" + personElement.getAttribute("id") + "\n"); NodeList childNodes = personElement.getChildNodes(); for(int y = 0; y < childNodes.getLength(); y++) { Node childNode = childNodes.item(y); if(childNode.getNodeType() == Node.ELEMENT_NODE) { Element childElement = (Element) childNode; if("name".equals(childElement.getNodeName())) { System.out.println("name:" + childElement.getFirstChild().getNodeValue()); stringBuffer.append("name:" + childElement.getFirstChild().getNodeValue() + "\n"); } else if("address".equals(childElement.getNodeName())) { System.out.println("address:" + childElement.getFirstChild().getNodeValue()); stringBuffer.append("address:" + childElement.getFirstChild().getNodeValue() + "\n"); } } } } } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return stringBuffer.toString(); } }
说明:
布局文件如下main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/display" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout>