ReadXmlUtil.java
packageorg.apache.list.util; importjava.io.File; importjava.io.FileInputStream; importjava.io.FileNotFoundException; importjava.io.IOException; importjava.util.HashMap; importjava.util.List; importjavax.xml.parsers.ParserConfigurationException; importjavax.xml.parsers.SAXParser; importjavax.xml.parsers.SAXParserFactory; importorg.xml.sax.SAXException; publicclassReadXmlUtil{ /** *匹配文件 */ privatestaticfinalStringFILE_TYPE="xml"; /** *用Sax读取xml文件 * *@paramfileName *文件名 *@paramnodeName *标示符 *@return *读取的内容 *@throwsException */ publicstaticList<HashMap<String,String>>readXmlInfo(StringfileName,StringnodeName) throwsException{ //文件为空的情况下 if(fileName==null||fileName.length()==0){ returnnull; } //不是XML文件的情况下 if(!fileName.endsWith(FILE_TYPE)){ returnnull; } //文件是否存在和文件夹判断 FilefileInfo=newFile(fileName); if(!fileInfo.exists()){ returnnull; } if(fileInfo.isDirectory()){ returnnull; } //获得文件输入流 FileInputStreamfs=newFileInputStream(newFile(fileName)); try{ SAXParserFactoryspf=SAXParserFactory.newInstance(); SAXParserparser=spf.newSAXParser(); SaxHandlerhandler=newSaxHandler(nodeName); parser.parse(fs,handler); fs.close(); returnhandler.getList(); }catch(ParserConfigurationExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); }catch(SAXExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); }catch(IOExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } returnnull; } publicstaticvoidmain(String[]args)throwsException{ try{ List<HashMap<String,String>>list=readXmlInfo("src/itcast.xml","person"); for(HashMap<String,String>p:list){ System.out.println(p.toString()); } }catch(FileNotFoundExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } }
SaxHandler.java
packageorg.apache.list.util; importjava.util.ArrayList; importjava.util.HashMap; importjava.util.List; importorg.xml.sax.Attributes; importorg.xml.sax.SAXException; importorg.xml.sax.helpers.DefaultHandler; publicclassSaxHandlerextendsDefaultHandler{ /** *三级标签 */ privateHashMap<String,String>map=null; /** *二级标签 */ privateList<HashMap<String,String>>list=null; /** *正在解析的元素的标签 */ privateStringcurrentTag=null; /** *正在解析的元素的值 */ privateStringcurrentValue=null; privateStringnodeName=null; publicList<HashMap<String,String>>getList(){ returnlist; } publicSaxHandler(StringnodeName){ this.nodeName=nodeName; } /** *当读到一个开始标签的时候,会触发这个方法 */ publicvoidstartDocument()throwsSAXException{ list=newArrayList<HashMap<String,String>>(); } /** *当遇到文档的开头的时候,调用这个方法 */ publicvoidstartElement(Stringuri,StringlocalName,Stringname,Attributesattributes) throwsSAXException{ if(name.equals(nodeName)){ map=newHashMap<String,String>(); } if(attributes!=null&&map!=null){ for(inti=0;i<attributes.getLength();i++){ map.put(attributes.getQName(i),attributes.getValue(i)); } } currentTag=name; } /** *这个方法用来处理在XML文件中读到的内容 */ publicvoidcharacters(char[]ch,intstart,intlength)throwsSAXException{ if(currentTag!=null&&map!=null){ currentValue=newString(ch,start,length); if(currentValue!=null&&!currentValue.trim().equals("") &&!currentValue.trim().equals("\n")){ map.put(currentTag,currentValue); } } currentTag=null; currentValue=null; } /** *在遇到结束标签的时候,调用这个方法 */ publicvoidendElement(Stringuri,Stringname)throwsSAXException{ if(name.equals(nodeName)){ list.add(map); map=null; } super.endElement(uri,localName,name); } }
itcast.xml
<?xmlversion="1.0"encoding="UTF-8"?> <persons> <personid="23"> <name>zhangsan</name> <age>30</age> </person> <personid="20"> <name>lisi</name> <age>25</age> </person> </persons>原文链接:https://www.f2er.com/xml/297424.html