Preface
When writing a common component, you need to read the XML file. However, because the component distribution is more convenient, third-party jar packages are rarely referenced. Therefore, you can directly use the built-in XML Parsing Method of JDK, of course, the functions and efficiency may not be as powerful as the third-party component.
Imported file:
import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NodeList;
Parsing Code:
Public map loadurlrange () {map = new hashmap (); try {java.net. URL url = getclass (). getresource ("/"); documentbuilderfactory DBF = documentbuilderfactory. newinstance (); // obtain a documentbuilderfactory instance documentbuilder DB = DBF. newdocumentbuilder (); // generate a documentbuilderfile file = new file (URL. getFile () + "/orgclientres/urlrange. XML "); // open the file and obtain the handle document DOC = dB. parse (File); // use Dom to parse XML files Nodelist urllist = Doc. getelementsbytagname ("url"); // retrieve the element productelement from all nodes named "product". // The Element Object declares for (INT I = 0; I <urllist. getlength (); I ++) // cyclically processed object {productelement = (element) urllist. item (I); string dourl = productelement. getattribute ("dourl"); // system. out. println ("link:" + dourl); If (! Map. containskey (dourl) {map. Put (dourl, "") ;}} catch (exception e) {e. printstacktrace () ;}return map ;}
JDK reads XML files