JDK reads XML files

Source: Internet
Author: User

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

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.