从URL读取xml文件属性

时间:2014-12-19 09:12:28

标签: java xml url

我正在尝试从URL中的文件中读取一些属性,当我这样做时,我将这些属性设为null,我不知道为什么。这是我的代码:

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 个答案:

答案 0 :(得分: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();