2014-12-19 195 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();