在线读取 XML 并存储它(使用 Java)

Reading XML online and Storing It (Using Java)(在线读取 XML 并存储它(使用 Java))
本文介绍了在线读取 XML 并存储它(使用 Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我发现并遵循了 Stackoverflow (http://stackoverflow.com/questions/2310139/how-to-read-xml-response-from-a-url-in-java) 中关于如何读取 XML 的示例来自 URL 的文件(如您在下面粘贴的代码中所见).我唯一的麻烦是,既然我得到了读取 XML 的程序,我该如何让它来存储它呢?例如,我可以让它将信息保存到项目中内置的 XML 文件中吗(如果可能的话,这对我来说是最好的解决方案)?例如,例如,我在项目中内置了一个空白 XML 文件.程序运行,从 URL 中读取 XML 代码,并将其全部存储到预先构建的空白 XML 文件中.我可以这样做吗?

I found and followed an example from Stackoverflow (http://stackoverflow.com/questions/2310139/how-to-read-xml-response-from-a-url-in-java) of how to read an XML file from a URL (as you can see in my code pasted below). My only trouble is that now that I got the program to read the XML, how do I get it to store it? For example, could I make it save the information to a XML file built into the project (this would be the best solution for me, if it's possible)? Such as, take for example, I have a blank XML file built into the project. The program runs, reads the XML code off of the URL, and stores it all into the pre-built blank XML file. Could I do this?

如果我对任何事情感到困惑或不清楚,请让我澄清我在寻找什么.

If I sound confusing or un-clear about anything, just ask me to clarify what I'm looking for.

这是我的代码,如果你想看看我目前的代码:

And here is my code, if you'd like to look at what I have so far:

package xml.parsing.example;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class XmlParser {
    public static void main (String[] args) throws IOException, ParserConfigurationException, SAXException, TransformerException {
        URL url = new URL("http://totheriver.com/learn/xml/code/employees.xml");
        URLConnection conn = url.openConnection();

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(conn.getInputStream());

        TransformerFactory tfactory = TransformerFactory.newInstance();
        Transformer xform = tfactory.newTransformer();

        // that’s the default xform; use a stylesheet to get a real one
        xform.transform(new DOMSource(doc), new StreamResult(System.out));
    }
}

推荐答案

很简单:

File myOutput = new File("c:\myDirectory\myOutput.xml");
xform.transform(new DOMSource(doc), new StreamResult(myOutput));

这篇关于在线读取 XML 并存储它(使用 Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Reliable implementation of PBKDF2-HMAC-SHA256 for JAVA(PBKDF2-HMAC-SHA256 for JAVA 的可靠实现)
Correct way to sign and verify signature using bouncycastle(使用 bouncycastle 签名和验证签名的正确方法)
Creating RSA Public Key From String(从字符串创建 RSA 公钥)
Why java.security.NoSuchProviderException No such provider: BC?(为什么 java.security.NoSuchProviderException 没有这样的提供者:BC?)
Generating X509 Certificate using Bouncy Castle Java(使用 Bouncy Castle Java 生成 X509 证书)
How can I get a PublicKey object from EC public key bytes?(如何从 EC 公钥字节中获取 PublicKey 对象?)