Java xml is used as the configuration file to read

Source: Internet
Author: User

Each project requires a configuration file to extract variable items for program development and maintenance:

You have compiled an xml file reading class and can call the attributes and values of the xml file in the form of strings. In fact, you can directly obtain the attributes and values of a static function. However, I did not. It is estimated that this will be better. Not to mention, it is easy to change. Provide source first:

/* * Property.java * * Created on 2008.7.11, am8:56 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */package mail;import java.io.File;import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList;/** * * @author user */public class Property {        private Element _rootElement = null;    /** Creates a new instance of Property */    public Property() {        //get the current directory        File directory = new File("");        String pathStr = directory.getAbsolutePath();        try {             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();            DocumentBuilder builder = factory.newDocumentBuilder();            Document document = builder.parse(new File(pathStr + "//" + CONSTANT.xmlFileName));            _rootElement = document.getDocumentElement();        } catch (Exception e) {            Logger.println("exception:" + e.getMessage());        }    }        /* get property with the specified name     * param: property name     * return: the value of the property     */    public String getProperty(String propertyName){        try{            NodeList list = _rootElement.getElementsByTagName(propertyName);            if(list.getLength() == 0){throw new Exception("xml error: there is no such property /"" + propertyName + "/"");}            Element element = (Element) list.item(0);            return element.getChildNodes().item(0).getNodeValue();        }catch(Exception ex){            Logger.println(ex.getMessage());            return "";        }    }        /* factory of this class     * return the object of this class     */    public static Property getInstance(){        Property _property = new Property();        return _property;    }    }

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.