Myhandler.java
PackageCom.sax.handler;Importjava.util.ArrayList;ImportJava.util.HashMap;Importjava.util.List;Importorg.xml.sax.Attributes;Importorg.xml.sax.SAXException;ImportOrg.xml.sax.helpers.DefaultHandler; Public classMyHandlerextendsDefaultHandler {PrivateHashmap<string,string> map=NULL;//Store a single parsed full object PrivateListNULL;//store all resolved objects PrivateString currenttag=NULL;//the element label being parsed PrivateString currentvalue=NULL;//parse the current element value PrivateString nodename=NULL;//to solve the current node name PublicListgetList () {returnlist; } PublicMyHandler (String nodeName) { This. nodename=NodeName; } @Override Public voidStartdocument ()throwssaxexception {//read the first tag to trigger this methodlist=NewArraylist(); } @Override Public voidstartelement (String uri, String localname, String qName, Attributes Attributes)throwssaxexception {//read to the beginning of the document, triggering this method if(Qname.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=QName; } @Override Public voidCharacters (Char[] ch,intStartintlength)throwssaxexception {//This method is to process the contents of the read to XML file if(currenttag!=NULL&& map!=NULL) {CurrentValue=NewString (CH, start, length); if(currentvalue!=NULL&&!currentvalue.trim (). Equals ("") &&!currentvalue.equals ("\ n")); Map.put (Currenttag,currentvalue); } Currenttag=NULL;//sets the value and label of the current node to nullCurrentvalue=NULL; } @Override Public voidendElement (String uri, String localname, string qName)throwssaxexception {//This method is called when the closing tag is encountered if(Qname.equals (nodeName)) {list.add (map); Map=NULL; } }}
Saxservers.java
PackageCom.sax.handler;ImportJava.io.InputStream;ImportJava.util.HashMap;Importjava.util.List;ImportJavax.xml.parsers.SAXParser;Importjavax.xml.parsers.SAXParserFactory; Public classSaxservers { Publicsaxservers () {//TODO auto-generated Constructor stub } Public StaticListreadXml (InputStream inputstream,string nodeName) {List<HashMap<String,String>> list=NULL; Try { //Create a factory object that parses the XMLSAXParserFactory spf=saxparserfactory.newinstance (); SAXParser Parser=spf.newsaxparser ();//Parsing XMLMyHandler handler=NewMyHandler (nodeName); Parser.parse (Inputstream,handler); Inputstream.close (); returnhandler.getlist (); } Catch(Exception e) {//Todo:handle Exception returnlist; } }}
Httputils.java
PackageCom.sax.handler;ImportJava.io.InputStream;Importjava.net.HttpURLConnection;ImportJava.net.URL; Public classHttputils { Publichttputils () {//TODO auto-generated Constructor stub } Public Staticinputstream GetXML (String path) {InputStream InputStream=NULL; Try{URL URL=NewURL (path); if(url!=NULL) {httpurlconnection connection=(HttpURLConnection) url.openconnection (); Connection.setconnecttimeout (3000); Connection.setdoinput (true); Connection.setrequestmethod ("GET"); intCode=Connection.getresponsecode (); if(code==200) {InputStream=Connection.getinputstream (); } } } Catch(Exception e) {//Todo:handle Exception } returnInputStream; }}
Text.java
PackageCom.sax.handler;ImportJava.io.InputStream;ImportJava.util.HashMap;Importjava.util.List; Public classText { PublicText () {//TODO auto-generated Constructor stub } /** * @paramargs*/ Public Static voidMain (string[] args) {//TODO auto-generated Method StubString path= "Http://weikew.gotoip55.com/person.xml"; InputStream InputStream=httputils.getxml (path); Try{List<HashMap<String,String>> List=saxservers.readxml (InputStream, "person"); for(hashmap<string,string>map:list) {System.out.println (map.tostring ()); } } Catch(Exception e) {//Todo:handle Exception } }}
Sax parsing xml