1. Now create the XML file in the Resources directory:
-resources
--config
---shipcorp.xml
Shipcorp.xml: The contents of the file are as follows:
<?XML version= "1.0" encoding= "UTF-8"?><Shipcorp> < ShipCode= "TLC"name= "Spring Chau too Hao"></ Ship> < ShipCode= "POS"name= "Po Yang shipping Service"></ Ship> < ShipCode= "COSCO"name= "Cosco"></ Ship> < ShipCode= "CSCO"name= "Middle Sea"></ Ship> < ShipCode= "Nqgs"name= "Nam Qing"></ Ship> < ShipCode= "COH"name= "Jing Han"></ Ship> < ShipCode= "DBR"name= "Big chain Bohai"></ Ship> < ShipCode= "HY"name= "Hong Yun Industry"></ Ship> < ShipCode= "ZIM"name= "With the star steamer"></ Ship></Shipcorp>
2. New Tool class:
PackageCom.portx.util;Importorg.apache.commons.collections.CollectionUtils;Importorg.dom4j.Document;Importorg.dom4j.DocumentException;Importorg.dom4j.Element;ImportOrg.dom4j.io.SAXReader;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;ImportJava.io.*;ImportJava.net.URL;ImportJava.util.*;/*** Product XML file generation and parsing * Created by GMQ on 2016/5/27.*/ Public classXmlreaderutil {Private Static FinalLogger log = Loggerfactory.getlogger (xmlreaderutil.class); Private Staticlist<map<string, object>> result =NewArraylist<>(); Private StaticString Plate_ship_corp = "Shipcorp.xml"; Private Static intLENGTH = Plate_ship_corp.length ()-4; /*** Get the full shipping company * *@return */ Public StaticList<map<string, object>>getallships () {isresultempty (); returnresult; } /*** Get shipping company name according to shipping company Code * *@paramKey *@return */ Public Staticstring Getvaluebykey (String key) {if(Stringutil.isempty (key)) {return""; } isresultempty (); String value= ""; Map<string, object> map =NULL; for(inti = 0; I < result.size (); i++) {Map=Result.get (i); if(Map.containskey (key)) {value=string.valueof (Map.get (key)); Break; } } returnvalue; } Private Static voidIsresultempty () {if(Collectionutils.isempty (Result)) {Assembleshipcorpmap (Plate_ship_corp); } } Private Static voidassembleshipcorpmap (String file) {Document document=reader (file); Set Set=NULL; Element Root=document.getrootelement (); List<Element> childelements =root.elements (); Map<string, object> map =NULL; for(Element child:childelements) {map=NewHashmap<>();//set = new HashSet ();//list<element> childlist = child.elements ();//For (Element element:childlist)// {//Set.add (Element.gettext ());// }Map.put (Child.attributevalue ("code"), Child.attributevalue ("name")); Result.add (map); } } Private StaticDocument Reader (String file) {Document document=NULL; Log.info ("Class Root Directory::" + 111); URL URL= Xmlreaderutil.class. GetResource ("/config/" +file); Log.info ("Plate symbol XML file path::" +file); InputStream InputStream=NULL; Try { Try{InputStream=NewFileInputStream (NewFile (Url.getpath ())); } Catch(FileNotFoundException e) {e.printstacktrace (); } Saxreader Reader=NewSaxreader (); Try{document=Reader.read (InputStream); } Catch(Documentexception de) {Log.error ("Document object Operation Exception"); De.printstacktrace (); } } Catch(Exception e) {e.printstacktrace (); } finally { Try{inputstream.close (); } Catch(IOException e) {log.error ("Stream Close Exception"); E.printstacktrace (); } } returndocument; } Public Static voidMain (string[] args) {//list<map<string, object>> List = Xmlreaderutil.getallships ();//System.out.println (list); }}
The above is possible.
Java Read XML file