xml的sax解析

前端之家收集整理的这篇文章主要介绍了xml的sax解析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. public void parseXml04(){
  2. String xmlPath = "C:\\Users\\asus\\Desktop\\map.osm.xml";
  3. try {
  4. //获取SAX分析器的工厂实例,专门负责创建SAXParser分析器
  5. SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
  6. //获取SAXParser分析器的实例
  7. SAXParser saxParser = saxParserFactory.newSAXParser();
  8. InputStream inputStream = new FileInputStream(new File(xmlPath));
  9. //解析xml文档
  10. saxParser.parse(inputStream,new XmlSAXHandler04());
  11. //迭代list
  12. System.out.println(nodes.size()+","+ways.size());
  13. if (XmlRead.ways.size()>0) {
  14. for (Way way:XmlRead.ways) {
  15. System.out.println("-----------------------------------------");
  16. System.out.println("【Id】"+way.getId());
  17. for (String string:way.getList()) {
  18. System.out.println("【Nd】"+string);
  19. }
  20. }
  21. }
  22. } catch (ParserConfigurationException e) {
  23. e.printStackTrace();
  24. } catch (SAXException e) {
  25. e.printStackTrace();
  26. } catch (FileNotFoundException e) {
  27. e.printStackTrace();
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. }
  31. }
  1. class XmlSAXHandler04 extends DefaultHandler {
  2. @Override
  3. public void startDocument() throws SAXException {
  4. XmlRead.nodes=new ArrayList<Node>();
  5. XmlRead.ways=new ArrayList<Way>();
  6. XmlRead.list=new ArrayList<String>();
  7. }
  8. @Override
  9. public void endDocument() throws SAXException {
  10. }
  11. @Override
  12. public void startElement(String uri,String localName,String qName,Attributes attributes) throws SAXException {
  13. if (qName.equals("node")) {
  14. XmlRead.node=new Node();
  15. if (attributes.getLength()>0) {
  16. XmlRead.node.setIdString(attributes.getValue("id").toString());
  17. XmlRead.node.setLat(attributes.getValue("lat").toString());
  18. XmlRead.node.setLon(attributes.getValue("lon").toString());
  19. }
  20. }
  21. if (qName.equals("way")) {
  22. XmlRead.way=new Way();
  23. if (attributes.getLength()>0) {
  24. XmlRead.way.setId(attributes.getValue("id").toString());
  25. }
  26. }
  27. if (qName.equals("nd")) {
  28. XmlRead.list=new ArrayList<String>();
  29. if (attributes.getLength()>0) {
  30. XmlRead.string=attributes.getValue("ref").toString();
  31. // System.out.println(XmlRead.list.size());
  32. }
  33. }
  34. }
  35. @Override
  36. public void endElement(String uri,String qName) throws SAXException {
  37. //需要说明的是,因为每一个非空标签都有characters(),那么无法知道user子标签循环完了
  38. //但是可以这样,如果不考虑子标签顺序可以判断是否解析到了最后一个子标签来判断
  39. //或者直接在user标签的endElement()中添加即可。
  40. if (qName.equals("node")) {
  41. XmlRead.nodes.add(XmlRead.node);
  42. XmlRead.node=null;
  43. }
  44. if (qName.equals("way")) {
  45. XmlRead.way.setList(XmlRead.list);
  46. XmlRead.ways.add(XmlRead.way);
  47. System.out.println(XmlRead.list.size());
  48. XmlRead.way=null;
  49. XmlRead.list=null;
  50. }
  51. if (qName.equals("nd")) {
  52. XmlRead.list.add(XmlRead.string);
  53. XmlRead.string=null;
  54. }
  55. }
  56. @Override
  57. public void characters(char[] ch,int start,int length) throws SAXException {
  58.  
  59. }
  60. }

猜你在找的XML相关文章