2014-12-19 196 views
1

我想從URL中的文件中讀取一些屬性,當我這樣做時,我將這些屬性設置爲null,但我不知道爲什麼。這是我的代碼:從URL中讀取xml文件屬性

Properties propiedades = new Properties(); 

URL url2= new URL("http://localhost:3624/web/applets/paqProperties/configuration.xml"); 
InputStream in =url2.openStream(); 
Reader reader = new InputStreamReader(in); 
propiedades.load(reader); 
//propiedades.loadFromXML(new FileInputStream("paqProperties/configuration.xml")); 
//propiedades.load(new FileInputStream("paqProperties/configuracion.properties")); 

soapAction=propiedades.getProperty("soapAction"); 
servidor=propiedades.getProperty("servidor"); 
url=propiedades.getProperty("urlWS"); 

爲什麼會發生這種情況?我只是在最後一行,這是XML的輸出:

<properties> 
    <entry key="soapAction">http://localhost:3624/</entry> 
    <entry key="servidor">http://localhost:3624/web/soa/</entry> 
    <entry key="urlWS">http://localhost:3624/web/soa/soa.asmx</entry> 
    <entry key="servidor2">http://localhost:3624/web/soa/</entry> 
    <entry key="soapAction2">http://localhost:3624/web/soa/getURL</entry> 
    </properties> 
+1

什麼是'propiedades'? – 2014-12-19 09:13:43

+0

如果您通過捲髮或網頁瀏覽器訪問您的網址,會發生什麼情況?你得到的XML文件? – Simon 2014-12-19 09:15:13

+0

是的,我知道了。所以我不知道 – zoit 2014-12-19 09:16:25

回答

0

你可以使用HttpURLConnection,讓您的InputStream

String uri = YOUR_URL 
URL url = new URL(uri); 
HttpURLConnection connection = 
    (HttpURLConnection) url.openConnection(); 
connection.setRequestMethod("GET"); 
connection.setRequestProperty("Accept", "application/xml"); 

InputStream xml = connection.getInputStream(); 

... 

connection.disconnect();