Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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 尝试读取XML文件后发生UnknownHostException_Java_Xml_Exception_Xom - Fatal编程技术网

Java 尝试读取XML文件后发生UnknownHostException

Java 尝试读取XML文件后发生UnknownHostException,java,xml,exception,xom,Java,Xml,Exception,Xom,我必须将映射的键/值序列化到XML文件,然后反序列化它们 Map<String,Integer> map = new HashMap<>(); // ... LinkedList<Element> l = new LinkedList<Element>(); Element root = new Element("root"); for (String str : map.keySet()) { l.add(new Element(str

我必须将映射的键/值序列化到XML文件,然后反序列化它们

Map<String,Integer> map = new HashMap<>();
// ...
LinkedList<Element> l = new LinkedList<Element>();
Element root = new Element("root");
for (String str : map.keySet()) {
     l.add(new Element(str)); // key
     l.getLast().appendChild(map.get(str).toString()); // value
     root.appendChild(l.getLast());
}
Document d = new Document(root);

BufferedWriter out = new BufferedWriter(new FileWriter("data.xml"));
out.write(d.toXML());
out.close();

d = new nu.xom.Builder().build("data.xml"); // !
Elements e = d.getRootElement().getChildElements();
但是XML文件创建成功。格式化的版本如下所示:

<?xml version="1.0"?>
<root>
    <through>1</through>
    <don>1</don>
    <backed>1</backed>
    <I>2</I>
    <asList>1</asList>
// ....
</root>

1.
1.
1.
2.
1.
// ....

您能解释一下问题出在哪里吗?

生成方法需要一个URL:

生成方法需要一个URL:

您需要将正确的URL传递到
build()
,这包括本地文件

您可以使用以下方法获取本地文件的URL:

new File(path).toURI().toURL();

您需要将正确的URL传递到
build()
,这包括本地文件

您可以使用以下方法获取本地文件的URL:

new File(path).toURI().toURL();

根据@delephin指出的文档,最好使用
build(File-in)
version的
build
方法,将与
data.xml
关联的
File
实例传递给
build()
方法,如下所示

 d = new nu.xom.Builder().build(new File("data.xml"));

根据@delephin指出的文档,最好使用
build(File-in)
version的
build
方法,将与
data.xml
关联的
File
实例传递给
build()
方法,如下所示

 d = new nu.xom.Builder().build(new File("data.xml"));

你知道什么是
build()
作为参数,如果它的文件名提供文件的完整路径。你应该遵循。你知道什么是
build()
作为参数,如果它的文件名提供文件的完整路径。你应该遵循。这样更好!)@阿拉斯泰尔马克谢谢。这样更好!:)@谢谢。