sax解析xml案例

前端之家收集整理的这篇文章主要介绍了sax解析xml案例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

有一个xml文档

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?><书架>
  2. <书>
  3. <书名 name="xxxx">java web</书名>
  4. <作者>张大</作者>
  5. <价格>102</价格>
  6. </书>
  7. <书>
  8. <书名>spring</书名>
  9. <作者>呜呜</作者>
  10. <价格>55</价格>
  11. </书>
  12. </书架>

解析所有节点
  1. package cn.itcast.sax;
  2.  
  3. import javax.xml.parsers.SAXParser;
  4. import javax.xml.parsers.SAXParserFactory;
  5.  
  6. import org.xml.sax.Attributes;
  7. import org.xml.sax.ContentHandler;
  8. import org.xml.sax.Locator;
  9. import org.xml.sax.SAXException;
  10. import org.xml.sax.XMLReader;
  11.  
  12. public class Demo1 {
  13. public static void main(String[] args) throws Exception {
  14. //1.创建工厂
  15. SAXParserFactory saxP = SAXParserFactory.newInstance();
  16. //2.得到解析器
  17. SAXParser parser = saxP.newSAXParser();
  18. //3.得到读取器
  19. XMLReader xmlR = parser.getXMLReader();
  20. //4.设置内容处理器
  21. xmlR.setContentHandler(new ParseS());
  22. //5.读取xml内容
  23. xmlR.parse("src/book.xml");
  24. }
  25. }
  26.  
  27. //得到xml文档的内容
  28. class ParseS implements ContentHandler{
  29.  
  30. @Override
  31. public void setDocumentLocator(Locator locator) {
  32. // TODO Auto-generated method stub
  33. }
  34.  
  35. @Override
  36. public void startDocument() throws SAXException {
  37. // TODO Auto-generated method stub
  38. }
  39.  
  40. @Override
  41. public void endDocument() throws SAXException {
  42. // TODO Auto-generated method stub
  43. }
  44.  
  45. @Override
  46. public void startPrefixMapping(String prefix,String uri)
  47. throws SAXException {
  48. // TODO Auto-generated method stub
  49. }
  50.  
  51. @Override
  52. public void endPrefixMapping(String prefix) throws SAXException {
  53. // TODO Auto-generated method stub
  54. }
  55.  
  56. @Override
  57. public void startElement(String uri,String localName,String qName,Attributes atts) throws SAXException {
  58. System.out.println("<"+qName+">");
  59. for(int i=0; i<atts.getLength(); i++){
  60. String name = atts.getQName(i);
  61. String value = atts.getValue(i);
  62. System.out.println(name+": "+value);
  63. }
  64. }
  65.  
  66. @Override
  67. public void endElement(String uri,String qName)
  68. throws SAXException {
  69. System.out.println("</"+qName+">");
  70. }
  71.  
  72. @Override
  73. public void characters(char[] ch,int start,int length)
  74. throws SAXException {
  75. System.out.println(new String(ch,start,length));
  76. }
  77.  
  78. @Override
  79. public void ignorableWhitespace(char[] ch,int length)
  80. throws SAXException {
  81. // TODO Auto-generated method stub
  82. }
  83.  
  84. @Override
  85. public void processingInstruction(String target,String data)
  86. throws SAXException {
  87. // TODO Auto-generated method stub
  88. }
  89.  
  90. @Override
  91. public void skippedEntity(String name) throws SAXException {
  92. // TODO Auto-generated method stub
  93. }
  94. }

获取指定标签的值
  1. package cn.itcast.sax;
  2.  
  3. import javax.xml.parsers.SAXParser;
  4. import javax.xml.parsers.SAXParserFactory;
  5.  
  6. import org.xml.sax.Attributes;
  7. import org.xml.sax.SAXException;
  8. import org.xml.sax.XMLReader;
  9. import org.xml.sax.helpers.DefaultHandler;
  10.  
  11. public class Demo2 {
  12.  
  13. public static void main(String[] args) throws Exception{
  14. //1.创建工厂
  15. SAXParserFactory saxP = SAXParserFactory.newInstance();
  16. //2.得到解析器
  17. SAXParser parser = saxP.newSAXParser();
  18. //3.得到读取器
  19. XMLReader xmlR = parser.getXMLReader();
  20. //4.设置内容处理器
  21. xmlR.setContentHandler(new TagHandler());
  22. //5.读取xml内容
  23. xmlR.parse("src/book.xml");
  24. }
  25.  
  26. }
  27.  
  28. //获取指定标签的值
  29. class TagHandler extends DefaultHandler{
  30. private String currentTag; //记录当前解析到的标签
  31. private int needNum = 2; //记录想获取第几个标签
  32. private int currentNum;
  33. @Override
  34. public void startElement(String uri,Attributes attributes) throws SAXException {
  35. currentTag = qName;
  36. if("作者".equals(currentTag)){
  37. currentNum++;
  38. }
  39. }
  40.  
  41. @Override
  42. public void endElement(String uri,String qName)
  43. throws SAXException {
  44. currentTag = null;
  45. }
  46.  
  47. @Override
  48. public void characters(char[] ch,int length)
  49. throws SAXException {
  50. if("作者".equals(currentTag) && currentNum==needNum){
  51. System.out.println(new String(ch,length));
  52. }
  53. }
  54. }

猜你在找的XML相关文章