标签:
web工程目录
xml文件
<?xml version="1.0" encoding="UTF-8"?> <persons> <person id="1"> <name>张三</name> <age>23</age> </person> <person id="2"> <name>李四</name> <age>25</age> </person> </persons>
java源程序
MyHandler类,存放数据
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class MyHandler extends DefaultHandler { private HashMap<String,String> map = null;//存储单个解析的完整对象 private List<HashMap<String,String>> list = null;//存储所有的解析对象 private String currentTag = null;//正在解析的元素的标签 private String currentValue = null;//解析当前元素的值 private String nodeName = null;//解析当前结点名称 public MyHandler(String nodeName) { // TODO Auto-generated constructor stub this.nodeName = nodeName; } public List<HashMap<String, String>> getList() { return list; } public void setList(List<HashMap<String, String>> list) { this.list = list; } @Override public void startDocument() throws SAXException { // TODO Auto-generated method stub //当读到第一个开始标签的时候,会触发这个方法 list = new ArrayList<HashMap<String,String>>(); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { // TODO Auto-generated method stub //当遇到文档的开头的时候调用这个方法 if(qName.equals(nodeName)){ map = new HashMap<String, String>(); } if(attributes!=null && map!=null){ for(int i=0;i<attributes.getLength();i++){ map.put(attributes.getQName(i), attributes.getValue(i)); } } currentTag = qName; } @Override public void characters(char[] ch, int start, int length) throws SAXException { // TODO Auto-generated method stub //这个方法是用来处理xml文件所读取到的内容 if(currentTag!=null && map!=null){ currentValue = new String(ch,start,length); if(currentValue!=null && !currentValue.trim().equals("")&&!currentValue.trim().equals("\n")){ map.put(currentTag, currentValue); } } currentTag = null;//把当前节点的对应的值和标签设置为空 currentValue = null; } @Override public void endElement(String uri, String localName, String qName) throws SAXException { // TODO Auto-generated method stub //遇到结束标记的时候 if(qName.equals(nodeName)){ list.add(map); map=null; } super.endElement(uri, localName, qName); } }
SaxService类,用于解析xml
import java.io.InputStream; import java.util.HashMap; import java.util.List; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import com.sax.handler.MyHandler; public class SaxService { public SaxService() { // TODO Auto-generated constructor stub } public static List<HashMap<String,String>> readXML(InputStream inputStream,String nodeName){ try { //创建一个解析xml的工厂对象,使应用程序能够配置和获取基于 SAX 的解析器以解析 XML文档 SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser parser = spf.newSAXParser();//解析xml MyHandler handler = new MyHandler(nodeName); parser.parse(inputStream, handler); inputStream.close(); return handler.getList(); } catch (Exception e) { // TODO: handle exception } return null; } }
工具类,用于获取url对象的输入流(xml)
import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class HttpUtils { public HttpUtils() { // TODO Auto-generated constructor stub } public static InputStream getXML(String path){ InputStream inputStream = null; try { URL url = new URL(path); if(url!=null){ HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(3000); connection.setDoInput(true); connection.setRequestMethod("GET"); int code = connection.getResponseCode(); if(code == 200){ inputStream = connection.getInputStream(); } } } catch (Exception e) { // TODO: handle exception } return inputStream; } }
测试类
import java.io.InputStream; import java.util.HashMap; import java.util.List; import com.sax.http.HttpUtils; import com.sax.sercice.SaxService; public class Test { public Test() { // TODO Auto-generated constructor stub } public static void main(String[] args) { String path = "http://169.254.32.172:8080/myhttp/person.xml"; InputStream inputStream = HttpUtils.getXML(path); try { List<HashMap<String,String>> list = SaxService.readXML(inputStream, "person"); for(HashMap<String,String> map : list){ System.out.println(map.toString()); } } catch (Exception e) { // TODO: handle exception } } }
结果:
{id=1, age=23, name=张三}
{id=2, age=25, name=李四}
标签:
原文地址:http://www.cnblogs.com/zyxiaohuihui/p/4537951.html