当前位置: 代码迷 >> XML/SOAP >> java中的dom,XmlPullParser 解析xml
  详细解决方案

java中的dom,XmlPullParser 解析xml

热度:133   发布时间:2016-05-23 01:56:09.0
java中的dom,XmlPullParser 解析xml,

新建xml文件:

<?xml version="1.0" encoding="UTF-8"?>  
<contacts>  
    <contact id="1">  
        <name>zhangsan</name>  
        <image src="http://192.168.1.103:8080/mymyweb/images/1.gif"/>  
    </contact>  
<contact id="2">  
        <name>zhangsan</name>  
        <image src="http://192.168.1.103:8080/mymyweb/images/1.gif"/>  
    </contact> 
</contacts> 

安卓版本dom解析:

private void createXmlFile(){

File linceseFile=new File(BOOKS_PATH);
try {
linceseFile.createNewFile();
} catch (Exception e) {
Log.e("IOException", "exception in createNewFile() method");
}
FileOutputStream fileos=null;
try {
fileos=new FileOutputStream(linceseFile);
} catch (Exception e) {
Log.e("FileNotFoundException","can't create FileOutputStream");
}
XmlSerializer serializer=Xml.newSerializer();
try {
serializer.setOutput(fileos,"UTF-8");
serializer.startDocument(null,true);
serializer.startTag(null,"books");
for(int i=0;i<3;i++){
serializer.startTag(null, "book");
serializer.startTag(null, "bookname");
serializer.text("Android教程"+i);
serializer.endTag(null, "bookname");
serializer.startTag(null, "bookauthor");
serializer.text("Frankie"+i);
serializer.endTag(null, "bookauthor");
serializer.endTag(null, "book");
}
serializer.endTag(null, "books");
serializer.endDocument();
serializer.flush();
fileos.close();
} catch (Exception e) {
// TODO: handle exception
}
Toast.makeText(getApplicationContext(), "创建XML文件成功!", Toast.LENGTH_LONG).show();
}
private void saxParserXML(){

}
private void domParserXML() {
File file=new File(BOOKS_PATH);
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=null;
try {
db=dbf.newDocumentBuilder();
} catch (Exception e) {
e.printStackTrace();
}
Document doc=null;
try {
doc=db.parse(file);
} catch (Exception e) {
e.printStackTrace();
}
Element root=doc.getDocumentElement();
NodeList books=root.getElementsByTagName("book");
String res="本结果是通过dom 解析的:/n";
for(int i=0;i<books.getLength();i++){
Element book=(Element) books.item(i);
Element bookname=(Element)book.getElementsByTagName("bookname").item(0);
Element bookauthor=(Element)book.getElementsByTagName("bookauthor").item(0);
res+="书名:"+bookname.getFirstChild().getNodeValue()+" "+"作者"+bookauthor.getFirstChild().getNodeValue()+"/n";

}

tv.setText(res);

其中注意设定权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

安卓版本pullparser解析:

try {
XmlPullParserFactory factory=XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp=factory.newPullParser();
xpp.setInput(new StringReader("<contacts><contact id='1'>  <name>zhangsan</name>   <image src='http://192.168.1.103:8080/mymyweb/images/1.gif'/></contact></contacts>"));
int eventType=xpp.getEventType();
while(eventType!=XmlPullParser.END_DOCUMENT){
switch(eventType){
case XmlPullParser.START_DOCUMENT:

System.out.println("Start document");
break;
case XmlPullParser.START_TAG:
System.out.println("Start tag"+xpp.getName());
if(xpp.getName().equals("name")){
Toast.makeText(getApplicationContext(), xpp.nextText(), Toast.LENGTH_SHORT).show();
}else if(xpp.getName().equals("image")){
Toast.makeText(getApplicationContext(), xpp.getAttributeValue(0), Toast.LENGTH_SHORT).show();
}
break;
case XmlPullParser.TEXT:
break;
case XmlPullParser.END_DOCUMENT:
break;
default  :
break;


}
eventType=xpp.next();
}

  相关解决方案
本站暂不开放注册!
内测阶段只得通过邀请码进行注册!
 
  • 最近登录:Wed Jul 26 22:41:10 CST 2017
  • 最近登录:Wed Jul 26 22:41:10 CST 2017
  • 最近登录:Wed Jul 26 22:41:10 CST 2017
  • 最近登录:Wed Jul 26 22:41:10 CST 2017
  • 最近登录:Wed Jul 26 22:41:10 CST 2017