java


UnknownHostException after the attempt of reading a XML file


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"));
The build method expects an URL: Builder.build(String)
You need to pass a proper URL to build(), this includes local files.
You can get the URL for a local file using:
new File(path).toURI().toURL();

Related Links

capture JVM snapshot at every error and exception
Server Not Working when trying to accees on another oc or placed client on other computer
how to store array values into array list and also need to print the reverse of resultant array list
Infinite while loop and counting for loop errors in java
getBaseContext() error for parsing data
Best way to manage my own paths in Google Maps Android API
How i can create button as shown in picture?
XStream custom converter that can generate flat XML structure from List?
Set timer for ambient mode (android wear)
Intent is not getting passed to AppCompactActivity
can please explain the piece of code in j2me program ..! [duplicate]
Active rendering on a JPanel with ImageIcon tiles? #Java
dynamically added items to recycler view not showing up when the application is restarted
gradle exclude transient dependency from project
Passing function in Scala
MD5 hash not changed after the file has been altered

Categories

HOME
maven
java
wso2-am
vbscript
drivers
relative-path
syntax
react-router
read-eval-print-loop
android-4.4-kitkat
amazon-ecs
cakephp-2.5
jpeg
binary-tree
fsm
google-project-tango
callback
multiple-records
autotools
append
android-youtube-api
synchronization
kibana-4
qt-creator
windows-azure-storage
hex-editors
msp430
windows-7-x64
normalizr
karma-jasmine
jasonette
facebook-instant-articles
opennlp
buildbot
wpfdatagrid
virtualdub
vision
objectanimator
iframe-resizer
ioio
catel
caret
vsts-build-task
geopositioning
wpf-controls
space-complexity
vapor
sqlite2
document.write
keydown
checkboxlist
wptoolkit
long-polling
jquery-nestable
libusb-win32
dtexec
sqldf
mcafee
jspdf-autotable
spring-mongodb
eventkit
watchconnectivity
gridpane
home-directory
merge-conflict-resolution
angstrom-linux
dlna
wapiti
cyclomatic-complexity
wireshark-dissector
citrus-pay
angular-local-storage
angularjs-ng-click
mesa
fluentautomation
expected-exception
resty-gwt
oam
ora-00911
jquery-mobile-dialog
http-unit
trusted
manchester-syntax
newtonscript
uiviewanimation-curve
windows-phone-7.1.1
transactionscope
eclipse-templates
mercurial-server
telerik-scheduler
yslow
sudzc
subviews
h.323
gamequery
nhibernate.search
zend-tool
anti-piracy
project-hosting
grid-system

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App