The XML of Java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.