您的位置:首页 > 理论基础 > 计算机网络

Android-SAX解析XML文件(http访问取回服务器XML)

2015-01-20 14:07 435 查看
本来打算之后好好写

由于最近Debug消耗了大量意志 所以写下一篇宣泄一下

记住一个坑爹的问题: 访问internet权限一定要加啊!!!我看书说的要注意

前几次敲还记得加,但是这次忘了!坑爹啊!Debug了两个多小时啊!!!



我还以为XML出问题了或HTTP访问返回的对象出问题了 一直抛出异常XML文件无可找到的元素 no element found!有木有!!!??

<uses-permission android:name="android.permission.INTERNET"/>



开始正题

DOM PULL 都可以解析 但是SAX可以边加载边解析

package com.john.xmlreader;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
//设计模式中采用Default接口实现 无需实现接口的所有方法
public class MyContentHandler extends DefaultHandler{
String hisname,address,money,sex,status;
String tagName;
public void startDocument() throws SAXException
{
System.out.println(".....begin.........");
}
public void endDocument() throws SAXException
{
System.out.println("......start........");
}
//开始解析标签    namespace为该标签的命名空间 localname为该标签无前缀的名字 qname为有前缀的名字 attr为其属性
public void startElement(String namespaceURI,String localName,String qName,Attributes attr)throws SAXException
{
//System.out.println("qName------>"+qName);
tagName=localName;

//读取标签信息 遍历attri 将键值对取出打印
if(localName.equals("worker"))
{
for(int i=0;i<attr.getLength();i++)
{ System.out.println(attr.getLocalName(i)+"="+attr.getValue(i));}
}

}
public void endElement(String namespaceURI,String localName,String qName)throws SAXException
{
tagName="";
if(localName.equals("worker"))
{
this.printoutXML();
}
}
public void characters(char[] ch,int start ,int length)throws SAXException{
if(tagName.equals("name"))
{
hisname=new String(ch,start,length);
}
else if(tagName.equals("sex"))
{
sex=new String(ch,start,length);
}
else if(tagName.equals("status"))
{
status=new String(ch,start,length);
}
else if (tagName.equals("address"))
{
address=new String(ch,start,length);
}
else if (tagName.equals("money"))
{
money=new String(ch,start,length);
}

}
private void printoutXML()
{
System.out.println("name:"+hisname);
System.out.println("sex:"+sex);
System.out.println("status:"+status);
System.out.println("address:"+address);
System.out.println("money:"+money);
}

}
http读取器

ackage com.john.utils_download;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.util.Log;

//定义HttpDownLoader类 返回下载文件内容
public class HttpDownLoader {
//定义URL负责传入Http地址
private URL url=null;
//构造函数不多说
public String download(String urlStr)
{
//StringBuffer处理字符的容器
StringBuffer sb= new StringBuffer();
String line=null;
//BufferedReader处理流对象的容器
BufferedReader buffer=null;

try
{
//通用HttpURL处理过程
//定义一个URL
url =new URL(urlStr);
//创建HttpURLConnection,使用url的.openConnection()方法建立连接
HttpURLConnection urlConn =(HttpURLConnection)url.openConnection();
Log.d("UrlConn", "NoBug");
//BufferedReader很屌,装饰者修饰方式 getInputStream()获得字节流,InputStreamReader()获得字符流,最后BufferedReader对象容器进行操作
buffer=new BufferedReader (new InputStreamReader(urlConn.getInputStream()));
//通过bufferedReader的readLine()方法 进行朱行读取 放到StringBuffer容器中进行操作
while ((line=buffer.readLine())!=null)
{
sb.append(line);
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally {
//记得擦屁股哦
try{
buffer.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
//StringBuffer.toString()返回String
return sb.toString();

}

//下载文件
public int DownFile (String urlStr,String path,String fileName)
{
//创造输入流对象
InputStream  inputStream =null;
try{
//用文件工具类对象
FileUtils fileUtils=new FileUtils();
//如果存在返回1
if(fileUtils.isFileExist(path+fileName))
{return 1;}
else{
inputStream =getInputStreamFromUrl(urlStr);
File resultFile =fileUtils.write2SDFromInput(path, fileName, inputStream);
if(resultFile==null)
{return -1;}
}
}
catch (Exception e)
{
e.printStackTrace();
return -1;
}
finally{
try
{inputStream.close();}
catch (Exception e)
{ e.printStackTrace();}
}
return 0;
}
//处理URL
public InputStream getInputStreamFromUrl(String UrlStr)
throws MalformedURLException ,IOException
{
url=new URL(UrlStr);
HttpURLConnection urlConn=(HttpURLConnection)url.openConnection();
InputStream inputStream =urlConn.getInputStream();
return inputStream;

}

}


下载工具包

package com.john.utils_download;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.os.Environment;
//文件存储类
public class FileUtils {
//成员变量定义文件存储路径
private String SDPATH;
//获取文件存储路径
public String getSDPATH()
{
return SDPATH;
}
//构造函数生成文件路径
public FileUtils(){
SDPATH=Environment.getExternalStorageDirectory()+"/";

}
//生成文件
public File createSDFile(String fileName) throws IOException
{
File file =new File(SDPATH+fileName);
file.createNewFile();
return file;
}
//生成文件目录
public File createSDDir(String dirName)
{
File dir=new File(SDPATH+dirName);
dir.mkdirs();
return dir;
}
//判断文件是否已存在
public boolean isFileExist(String fileName)
{
File file =new File(SDPATH+fileName);
return file.exists();

}
//将生成的文件写进SDCARD
public File write2SDFromInput(String path,String fileName,InputStream input)
{
File file =null;
OutputStream output =null;
try
{
createSDDir(path);
file =createSDFile(path + fileName);
//生成输出流对象
output =new FileOutputStream(file);
//以4M写入
byte buffer[]=new byte [4*1024];

while((input.read(buffer))!=-1)
{
output.write(buffer);
}
output.flush();

}//记得擦屁股哦!
catch(Exception e)
{
e.printStackTrace();
}
finally{
try{
output.close();
}
catch(Exception e)
{ e.printStackTrace();}

}
return file;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: