1 //convert to XML format2 Public StaticString Arraytoxml (map<string, string>arr) {3String XML = "<xml>";4Iterator<entry<string, string>> iter =Arr.entryset (). iterator ();5 while(Iter.hasnext ()) {6entry<string, string> Entry =Iter.next ();7String key =Entry.getkey ();8String val =Entry.getvalue ();9 if(IsNumeric (val)) {TenXML + = "<" + key + ">" + val + "</" + key + ">"; One}Else AXML + = "<" + key + "><! [cdata["+ val +"]]></"+ key +" > "; - } -XML + = "</xml>"; the returnXML; - } - - Public Static Booleanisnumeric (String str) { + if(Str.matches ("\\d *")) { - return true; +}Else { A return false; at } -}
1 //Parsing XML2 PrivateMap<string, string>doxmlparse (String XML)3 throwsxmlpullparserexception, IOException {4 5InputStream InputStream =NewBytearrayinputstream (Xml.getbytes ()); 6 7map<string, string> map =NULL; 8 9Xmlpullparser Pullparser =xmlpullparserfactory.newinstance ()Ten . Newpullparser (); One APullparser.setinput (InputStream, "UTF-8");//To set the XML data to parse for XML - - intEventType =Pullparser.geteventtype (); the - while(EventType! =xmlpullparser.end_document) { - Switch(eventtype) { - Casexmlpullparser.start_document: +Map =NewHashmap<string, string>(); - Break; + A CaseXmlpullparser.start_tag: atString key =Pullparser.getname (); - if(Key.equals ("xml")) - Break; -String value =Pullparser.nexttext (); - map.put (key, value); - Break; in CaseXmlpullparser.end_tag: - Break; to } +EventType =Pullparser.next (); - } the returnmap; *}
The XML of Java