XML parsing Tool class

Source: Internet
Author: User


Public class xmlutil { /*
* Parse XML file contents using DOM4J and return map data form
* path is where the. xml file is located
*/
     Public StaticMap<string,string> paserxmlbydom4j (String path)throwsexception{Path=Servletactioncontext.getservletcontext (). Getrealpath (path); Saxreader Reader=NewSaxreader (); Document Doc= Reader.read (NewFileInputStream (NewFile (path)); Map<String,String> xml=Paserxml (DOC); returnXML; }            //A string passed into an XML format, converted to an XML type, then parsed for its contents, returning the Map data form
/*
* Strxml is a string in XML format
*/ Public StaticMap<string,string> strtoxmlandpaserxml (String strxml)throwsexception{Saxreader Reader=NewSaxreader (); Document Doc= Reader.read (NewBytearrayinputstream (Strxml.getbytes ("UTF-8"))); Map<String,String> xml=Paserxml (DOC); returnXML; } //traverse parsing XML data Public StaticMap<string,string> Paserxml (Document doc)throwsexception{Map<String,String> xml=NewHashmap<string,string>(); Element Root=doc.getrootelement (); Iterator it=Root.elementiterator (); element element; while(It.hasnext ()) {element=(Element) it.next (); Xml.put (Element.getname (), Element.gettext ()); } returnXML; } }

XML parsing Tool class

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.