[英]UnknownHostException after the attempt of reading a XML file
我必須將映射的鍵/值序列化為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文件時,在標記的行上拋出UnknownHostException
。
Exception in thread "main" java.net.UnknownHostException: file
XML文件已成功創建。 格式化的版本如下所示:
<?xml version="1.0"?>
<root>
<through>1</through>
<don>1</don>
<backed>1</backed>
<I>2</I>
<asList>1</asList>
// ....
</root>
你能給我解釋一下是什么問題嗎?
根據@delephin指出的文檔,最好使用build(File in)
版本的build
方法,將與data.xml
關聯的File
實例傳遞給build()
方法,如下所示
d = new nu.xom.Builder().build(new File("data.xml"));
生成方法需要一個URL: Builder.build(String)
您需要傳遞一個正確的URL到build()
,其中包括本地文件。
您可以使用以下方法獲取本地文件的URL:
new File(path).toURI().toURL();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.