I have to serialise key/values of a map to a XML file, and then deserialise them.
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();
But when I try to read the XML file, UnknownHostException
is thrown on the marked line.
Exception in thread "main" java.net.UnknownHostException: file
The XML file is created succesfully though. The formatted version looks like:
<?xml version="1.0"?>
<root>
<through>1</through>
<don>1</don>
<backed>1</backed>
<I>2</I>
<asList>1</asList>
// ....
</root>
Could you please explain me what is the problem?
According to the doc pointed by @delephin, its better to use build(File in)
version of build
method, pass the File
instance associated with your data.xml
to your build()
method, as below
d = new nu.xom.Builder().build(new File("data.xml"));