[Resolvido] File, ler XML remoto

7 respostas
paulofernandesjr

Opa!

galera, como faço para acessar e ler um arquivo XML na minha aplicação WEB.

para manipular o XML pensei em usar o XStream, porém estou com dificuldade para capturar o arquivo

tenho a seguinte estrutura

src
web
web>sistema>arquivo.xml

obrigado

7 Respostas

P

http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletContext.html#getResourceAsStream%28java.lang.String%29

paulofernandesjr

Tem como pegar o ServletContext a partir do request?

como posso fazer

P

ServletContext servletContext = request.getSession().getServletContext()

paulofernandesjr

por algum motivo assim não deu certo, ai passei como parametro para o metodo e aparentemente deu certo

mas agora o problema é que o

InputStream input = sc.getResourceAsStream(path);

está retornando null

P

Dá uma uma lida no javadoc:

getResource


Returns a URL to the resource that is mapped to a specified path. The path must begin with a “/” and is interpreted as relative to the current context root.

This method allows the servlet container to make a resource available to servlets from any source. Resources can be located on a local or remote file system, in a database, or in a .war file.

The servlet container must implement the URL handlers and URLConnection objects that are necessary to access the resource.

This method returns null if no resource is mapped to the pathname.

Some containers may allow writing to the URL returned by this method using the methods of the URL class.

The resource content is returned directly, so be aware that requesting a .jsp page returns the JSP source code. Use a RequestDispatcher instead to include results of an execution.

This method has a different purpose than java.lang.Class.getResource, which looks up resources based on a class loader. This method does not use class loaders.

paulofernandesjr

Não entendi muito bem…

se quiser me ajudar!

o meu código está abaixo:

StringBuffer url = request.getRequestURL();
		int limitador = url.indexOf("financeiro/");
		String path = url.substring(0, limitador)+"emailCartaFornecedor.xml";
		URL u = new URL(path);
		URLConnection connection = u.openConnection();
		System.out.println(sc);
		InputStream input = sc.getResourceAsStream("/sistema/financeiro/emailCartaFornecedor.xml");
		System.out.println(input);

obrigado

paulofernandesjr

consegui:

private InputStream getArquivo(HttpServletRequest request) throws IOException{ StringBuffer url = request.getRequestURL(); int limitador = url.indexOf("financeiro/"); String path = url.substring(0, limitador)+"emailCartaFornecedor.xml"; URL u = new URL(path); URLConnection connection = u.openConnection(); connection.connect(); return connection.getInputStream(); }

Criado 15 de setembro de 2009
Ultima resposta 15 de set. de 2009
Respostas 7
Participantes 2