java讀取xml/.properties設定檔

來源:互聯網
上載者:User
package org.ifocus.crawler.main;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.util.Enumeration;import java.util.Iterator;import java.util.List;import java.util.Properties;import org.jdom.Document;import org.jdom.Element;import org.jdom.JDOMException;import org.jdom.Text;import org.jdom.input.SAXBuilder;public class ReadConfiguration {//讀取xml的設定檔public void readXml(String path) throws JDOMException, IOException {SAXBuilder builder = new SAXBuilder(false);Document document = builder.build(path);Element site = document.getRootElement();Element bd = site.getChild("bd");System.out.println(bd.getChildText("name"));System.out.println(bd.getChildText("pages"));}//讀取.properties的設定檔public void readProp(String path) throws IOException {InputStream in = new BufferedInputStream(new FileInputStream(path));Properties prop = new Properties();prop.load(in);Enumeration<String> en = (Enumeration<String>) prop.propertyNames();while(en.hasMoreElements()) {System.out.println(prop.get(en.nextElement()));}}public static void main(String args[]) throws IOException, JDOMException {ReadConfiguration rc = new ReadConfiguration();//rc.readProp("./src/baseUrl.properties");rc.readXml("./src/baseUrl.xml");}}

另外兩個設定檔的內容

######.properties#百度site.bd.name=baidusite.bd.pages=5sie.bd.url=http://video.baidu.com/music/?order=hot&pn=1#音樂台yyt.name=yinyuetaiyyt.pages=5yyt.url=http://www.yinyuetai.com/mv/all?page=1#####.xml<?xml version="1.0" encoding="UTF-8"?><site><bd><name>百度</name><pages>5</pages><!--<url>http://video.baidu.com/music/?order=hot&pn=1</url>--></bd><yyt><name>音樂台</name><pages>5</pages><!--<url>http://www.yinyuetai.com/mv/all?page=1</url>--></yyt></site>

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.