android sax解析

sax解析分为两部分:

sax解析器,defaultHandler


过程:

1、从assert中读入xml文件 

InputStream inStream = this.getResources().getAssets().open("person.xml"); 

2、创建解析器

 1、获取sax解析工厂  
       SAXParserFactory  sf = SAXParserFactory.newInstance();  
 2、获取解析器  
           SAXParser sp = sf.newSAXParser();

3、编写defaultHandler

public class MyDefaulthandler extends DefaultHandler {
private HashMap<String, String> mMap = null;
private List<HashMap<String, String>> mList = null;
private String mNodeName = null;
private String currentTag = null;
private String currentValue=null;
private boolean isHasChar = true;

/**
* sax事件处理
* @param context
* @param nodeName
*/
public MyDefaulthandler(String nodeName) {
mNodeName = nodeName;
}

/**
* startDocument
*/
@Override
public void startDocument() throws SAXException {
mList = new ArrayList<HashMap<String,String>>();
}

/**
* startElement,每次读到开始标签都会执行这里
*/
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
currentTag = qName;

/**
* 当读到开始标签时,如果有属性则遍历放入mMap中
*/
if(qName.equals(mNodeName)) {
mMap = new HashMap<String, String>();
isHasChar = true;
/*获取标签内的属性*/
if(attributes.getLength() > 0){
for(int i=0; i<attributes.getLength(); i++) {
mMap.put(attributes.getQName(i), attributes.getValue(i));
}
}
}
}

/**
* 读取到内容时
*/
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
String currentValue = String.valueOf(ch, start, length);
if(currentTag != null ) {
/*将获取到的\n \t剔除*/
if(currentTag.equals("name") ) {
for(int i=0; i<currentValue.length();i++) {
if(currentValue.charAt(i)=='\n' || currentValue.charAt(i)=='\t') 
isHasChar = false;
}
if(isHasChar)
mMap.put(currentTag, currentValue);
}

if(currentTag.equals("age")) {
for(int i=0; i<currentValue.length();i++) {
if(currentValue.charAt(i)=='\n' || currentValue.charAt(i)=='\t') 
isHasChar = false;
}
if(isHasChar){
mMap.put(currentTag, currentValue);
}
}
}
}

/**
* endElement
*/
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if(qName.equals(mNodeName)) {
mList.add(mMap);
currentTag = null;
currentValue = null;
mMap = null;
}
}

/**
* feedback mList
* @return
*/
public List<HashMap<String, String>> getmList() {
return mList;
}




/**
* 结束文档
*/
@Override
public void endDocument() throws SAXException {
}
}


4、将事件器defaulthandler传入

sp.parse(inStream, myHandler); 

5、获取到数组

 myHandler.getmList().


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio 中,可以使用 SAX 解析解析 XML 文件。SAX 解析器是一种基于事件的解析器,它可以将 XML 文件解析为一系列事件,然后通过监听这些事件来获取所需的数据。 以下是使用 SAX 解析解析 XML 文件的示例代码: ```java public class MyHandler extends DefaultHandler { private String currentValue; private boolean parsingName; @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (localName.equals("name")) { parsingName = true; } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (parsingName) { // 处理姓名数据 parsingName = false; } } @Override public void characters(char[] ch, int start, int length) throws SAXException { if (parsingName) { currentValue = new String(ch, start, length); } } } // 在 Activity 中使用 SAX 解析器 try { InputStream inputStream = getResources().openRawResource(R.raw.my_xml_file); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); MyHandler handler = new MyHandler(); saxParser.parse(inputStream, handler); } catch (Exception e) { e.printStackTrace(); } ``` 在上面的示例代码中,MyHandler 类继承了 DefaultHandler 类,并重写了 startElement()、endElement() 和 characters() 方法来监听 XML 文件中的事件。在 startElement() 方法中,可以判断当前解析的标签是否是所需的标签,并进行相应的处理;在 endElement() 方法中,可以完成对标签数据的处理;在 characters() 方法中,可以获取标签文本内容。 在 Activity 中,可以使用 SAXParserFactory 类和 SAXParser 类来创建和使用 SAX 解析器。在调用 saxParser.parse() 方法时,需要传入要解析的 XML 文件的输入流和事件处理器。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值