Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从web检索和解析XML_Java_Xml_File Io - Fatal编程技术网

Java 从web检索和解析XML

Java 从web检索和解析XML,java,xml,file-io,Java,Xml,File Io,我想使用XPath(Java)解析XML文件。但是,这些XML文件仅在web上可用(手动下载它们不是一个选项(当然,它们必须“下载”才能处理) 因此,基本上我的问题是如何将URI对象转换为文件对象。我是否需要使用SCP或介于两者之间的东西来下载文件。任何代码、教程或一般建议都将不胜感激 我试过这个: URI uri = new URI("http://www.somefiles.com/myfile.xml"); InputStream is = uri.toURL().open

我想使用XPath(Java)解析XML文件。但是,这些XML文件仅在web上可用(手动下载它们不是一个选项(当然,它们必须“下载”才能处理)

因此,基本上我的问题是如何将URI对象转换为文件对象。我是否需要使用SCP或介于两者之间的东西来下载文件。任何代码、教程或一般建议都将不胜感激

我试过这个:

    URI uri = new URI("http://www.somefiles.com/myfile.xml");
    InputStream is = uri.toURL().openStream();
    File xmlDocument = new File(uri);

但这给出了一个
URI方案不是“文件”
错误。

您可以使它更复杂,但这可以像从URL获取一样简单

InputStream in = remoteURI.toURL().openStream();

现在,这不是最初请求的
文件
对象,但我猜您的XPath库可以处理一个通用的
输入流
。否则,您必须将上面的输入流保存到一个临时文件中,并在该文件上创建一个File对象。

您可以将其变得更复杂,但这可以像从URL中执行一样简单

InputStream in = remoteURI.toURL().openStream();

现在,这不是最初请求的
文件
对象,但我猜您的XPath库可以处理通用的
输入流
。否则,您必须将上面的输入流保存到临时文件中,并在该文件上创建文件对象。

尝试先将XML写入磁盘:

File tempDir = new File(System.getProperty("java.io.tmpdir"));
File xmlDocument = new File(tempDir, "theXml.xml");
InputStream in = remoteURI.toURL().openStream();
OutputStream out = new FileOutputStream(xmlDocument);
int read;
while ((read = in.read()) != -1){
  out.write(read);
}
in.close();
out.close();
但是,如果只需要使用XPath从XML中提取一些数据,则无需将任何内容写入磁盘:

InputStream in = remoteURI.toURL().openStream();
StreamSource source = new StreamSource(in);
DOMResult result = new DOMResult();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(source, result);
Document document = (Document)result.getNode();

XPath xpath = XPathFactory.newInstance().newXPath();
xpath.evaluate("...", document);

尝试先将XML写入磁盘:

File tempDir = new File(System.getProperty("java.io.tmpdir"));
File xmlDocument = new File(tempDir, "theXml.xml");
InputStream in = remoteURI.toURL().openStream();
OutputStream out = new FileOutputStream(xmlDocument);
int read;
while ((read = in.read()) != -1){
  out.write(read);
}
in.close();
out.close();
但是,如果只需要使用XPath从XML中提取一些数据,则无需将任何内容写入磁盘:

InputStream in = remoteURI.toURL().openStream();
StreamSource source = new StreamSource(in);
DOMResult result = new DOMResult();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(source, result);
Document document = (Document)result.getNode();

XPath xpath = XPathFactory.newInstance().newXPath();
xpath.evaluate("...", document);

这会给出一个“URI方案不是”文件“”或您仍然将
URI
传递给文件构造函数。您需要使用InputStream
is
生成一个文件。这会给出一个“URI方案不是”文件“”您仍然将
uri
传递给了文件构造函数。您需要使用InputStream
is
生成一个文件。