android xml解析

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36385296/article/details/79196756
1,要解析的xml文件文件

xml="<apps>\n" +
                "  <app>\n" +
                "    <id>1</id>\n" +
                "    <name>burn</name>\n" +
                "    <version>1.0</version>\n" +
                "    <info>\n" +
                "      <size>10.5</size>\n" +
                "      <message>hhhhhhhh</message>\n" +
                "    </info>\n" +
                "    <info>\n" +
                "      <size>10.5</size>\n" +
                "      <message>hhhhhhhh</message>\n" +
                "    </info>\n" +
                "  </app>\n" +
                "  <app>\n" +
                "    <id>2</id>\n" +
                "    <name>burn2</name>\n" +
                "    <version>2.1</version>\n" +
                "    <info>\n" +
                "      <size>10.5</size>\n" +
                "      <message>hhhhhhhh</message>\n" +
                "    </info>\n" +
                "    <info>\n" +
                "      <size>10.5</size>\n" +
                "      <message>hhhhhhhh</message>\n" +
                "    </info>\n" +
                "  </app>\n" +
                "  <app>\n" +
                "    <id>3</id>\n" +
                "    <name>burn3</name>\n" +
                "    <version>3.2</version>\n" +
                "    <info>\n" +
                "      <size>10.5</size>\n" +
                "      <message>hhhhhhhh</message>\n" +
                "    </info>\n" +
                "    <info>\n" +
                "      <size>10.5</size>\n" +
                "      <message>hhhhhhhh</message>\n" +
                "    </info>\n" +
                "  </app>\n" +
                "</apps>";

2,使用pull方式解析xml文件

private void parseXmlWithPull() {
try {
XmlPullParserFactory factory=XmlPullParserFactory.newInstance();
XmlPullParser parser=factory.newPullParser();
parser.setInput(new StringReader(xml));
int eventType=parser.getEventType();
String id="";
String name="";
String version="";
String size="";
String message="";
while (eventType!=parser.END_DOCUMENT){
String nodeName=parser.getName();
switch (eventType){
case XmlPullParser.START_TAG:
switch (nodeName){
case "id":
id=parser.nextText();
break;
case "name":
name=parser.nextText();
break;
case "version":
version=parser.nextText();
break;
case "size":
size=parser.nextText();
break;
case "message":
message=parser.nextText();
break;
default:
break;
}
break;
case XmlPullParser.END_TAG:
if ("app".equals(nodeName)){
Log.e("tag","id:"+id);
Log.e("tag","name:"+name);
Log.e("tag","version:"+version);
}else if ("info".equals(nodeName)){
Log.e("tag","size:"+size);
Log.e("tag","message:"+message);
}
break;
default:
break;
}
eventType=parser.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

3,使用sax方式解析xml文件

private void parseXmlWithSax(){
try {
SAXParserFactory factory=SAXParserFactory.newInstance();
SAXParser parser=factory.newSAXParser();
XMLReader xmlReader=parser.getXMLReader();
ContentHandler ch=new ContentHandler();
xmlReader.setContentHandler(ch);
xmlReader.parse(new InputSource(new StringReader(xml)));
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public class ContentHandler extends DefaultHandler {

//characters方法不是一次获取全部数据,所有要将数据拼接
private StringBuilder id;
private StringBuilder name;
private StringBuilder version;
private StringBuilder size;
private StringBuilder message;
private String nodeName;

@Override
public void startDocument() throws SAXException {
id=new StringBuilder();
name=new StringBuilder();
version=new StringBuilder();
size=new StringBuilder();
message=new StringBuilder();
}

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
nodeName=localName;
}

@Override
public void characters(char[] ch, int start, int length) throws SAXException {
switch (nodeName){
case "id":
id.append(ch,start,length);
break;
case "name":
name.append(ch,start,length);
break;
case "version":
version.append(ch,start,length);
break;
case "size":
size.append(ch,start,length);
break;
case "message":
message.append(ch,start,length);
break;
default:
break;
}
}

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if ("app".equals(localName)){
//获取的数据中含有多余的空格和换行需要去掉
Log.e("tag","id:"+id.toString().trim());
Log.e("tag","name:"+name.toString().trim());
Log.e("tag","version:"+version.toString().trim());
//清空
id.setLength(0);
name.setLength(0);
version.setLength(0);
}else if ("info".equals(localName)){
Log.e("tag","size:"+size.toString().trim());
Log.e("tag","message:"+message.toString().trim());
size.setLength(0);
message.setLength(0);
}
}

@Override
public void endDocument() throws SAXException {
}
}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页