java解析xml数据---sax解析器
- - ITeye博客下面是handler解析数据的方法. private HashMap map = null;// 存储单个解析的完整对象. private List> list = null;// 存储全部的解析对象. private String currentTag = null; // 正在解析的元素的标签.
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) { this.nodeName = nodeName; } public List<HashMap<String, String>> getList() { return list; } public void startDocument() throws SAXException { // 当读到第一个开始标签的时候,开始触发 list = new ArrayList<HashMap<String, String>>(); } public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { // 当遇到文档的开头的时候,调用这个方法 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; } public void characters(char[] ch, int start, int length) throws SAXException { // 处理从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; } public void endElement(String uri, String localName, String qName) throws SAXException { // 遇到结束标记的时候,调用此方法 if (qName.equals(nodeName)) { list.add(map); map = null; } } }
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 static List<HashMap<String, String>> readXML(InputStream input, String nodeName){ try { //创建一个解析xml的工厂对象 SAXParserFactory apf = SAXParserFactory.newInstance(); SAXParser parser = apf.newSAXParser();//解析xml内容 MyHandler handler = new MyHandler(nodeName); parser.parse(input, handler);//用给定的handler解析input输入流中的内容 input.close(); return handler.getList(); } catch (Exception e) { System.out.println(e); } return null; } }
import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class HttpUtils { public static InputStream getXML(String path) { InputStream input = null; try { URL url = new URL(path); if (url != null) { HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setReadTimeout(3000); connection.setDoInput(true); connection.setRequestMethod("GET"); int code = connection.getResponseCode(); if (code == 200) { input = connection.getInputStream(); } } } catch (Exception e) { // TODO: handle exception } return input; } }
import java.io.InputStream; import java.util.HashMap; import java.util.List; import com.sax.http.HttpUtils; import com.sax.service.SaxService; public class Test { public static void main(String[] args) { String path = "http://127.0.0.1:8080/myhttp/person.xml"; try { InputStream input = HttpUtils.getXML(path); List<HashMap<String, String>> list = SaxService.readXML(input, "person"); for(HashMap<String, String> map :list){ System.out.println("-test->"+map.toString()); } } catch (Exception e) { System.out.println(e); } } }