Android XML数据解析简单示例

所属分类: 软件编程 / Android 阅读数: 828
收藏 0 赞 0 分享

1、创建XML数据

在android工程目录中res/目录下创建raw文件夹,在raw文件夹内创建data.xml。

复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<data>
    <class>
        <c studentNum="1">Android</c>
        <c studentNum="2">IPhone</c>
    </class>
</data>

2、XML解析代码

复制代码 代码如下:

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.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  InputStream is = getResources().openRawResource(R.raw.data);
  try {
   byte[] bytes = new byte[is.available()];
   is.read();
   String XMLStr = new String(bytes,"utf-8");
   is.reset();
   System.out.println(XMLStr);
   
   DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
   Document doc = builder.parse(is);
   
   Node node = doc.getFirstChild();
   System.out.println("第一个子节点"+node.getNodeName());
   NodeList list = doc.getElementsByTagName("c");
   NamedNodeMap map;
   for(int i = 0; i < list.getLength(); i++)
   {
    node = list.item(i);
    map = node.getAttributes();
    System.out.println(node.getTextContent()+"  studentNum  "+map.getNamedItem("studentNum").getNodeValue());
   }
   
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (ParserConfigurationException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (SAXException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

更多精彩内容其他人还在看

Android开发之设置开机自动启动的几种方法

这篇文章主要介绍了Android开发之设置开机自动启动的几种方法的相关资料,这里提供三种方法帮助大家实现这样的功能,需要的朋友可以参考下
收藏 0 赞 0 分享

Android绘制圆形百分比加载圈效果

这篇文章主要为大家详细介绍了Android绘制圆形百分比加载圈效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android 两种启动模式的实例详解

这篇文章主要介绍了Android 两种启动模式的实例详解的相关资料,Activity的两种启动模式:FLAG_ACTIVITY_CLEAR_TOP和FLAG_ACTIVITY_REORDER_TO_FRONT ,需要的朋友可以参考下
收藏 0 赞 0 分享

Android下载进度监听和通知的处理详解

这篇文章主要为大家详细介绍了Android下载进度监听和通知的处理,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android读取资源文件的方法

这篇文章主要介绍了Android读取资源文件的方法的相关资料,这里提供两种方法及实例,需要的朋友可以参考下
收藏 0 赞 0 分享

Android EasyBarrage实现轻量级弹幕效果

本篇文章主要介绍了Android EasyBarrage实现轻量级弹幕效果,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android仿微信朋友圈全文、收起功能的实例代码

本篇文章主要介绍了Android仿微信朋友圈全文、收起功能的实例代码,具有一定的参考价值,有兴趣的可以了解一下
收藏 0 赞 0 分享

详解Android中图片的三级缓存及实例

这篇文章主要介绍了详解Android中图片的三级缓存及实例的相关资料,通过网络、本地、内存三级缓存图片,来减少不必要的网络交互,避免浪费流量,需要的朋友可以参考下
收藏 0 赞 0 分享

TabLayout使用方法详解

这篇文章主要为大家详细介绍了Android中TabLayout的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

ToolBar使用方法详解

这篇文章主要为大家详细介绍了Android中ToolBar的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多