I am having an issue with exporting a custom hashmap function to xml. The layout of my project is as follows. The package adt contains the following classes:
class HashMap implements Map<K,V>
A custom hashmap implementation, pretty much identical to java.util.HashMap. (Note, same errors exist when I use java.util.HashMap)
class Database extends HashMap <String,Table>
class Table extends HashMap <Object, Row>
(Note: The key of type Object is always a string in my implementation)
class Row extends HashMap <String, Object>
(Object is of type String, boolean, or int)
I am trying to export a table to an xml file, and eventually import it again but I haven't gotten that far. Here is the function which I use to export it:
private Response xml(Table table, FileWriter file) {
JAXBContext jaxbContext;
Marshaller jaxbMarshaller;
try {
jaxbContext = JAXBContext.newInstance(HashMap.class);
jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(table, file);
}catch (JAXBException e) {
e.printStackTrace(System.out);
return new Response(false,"Export - JAXBException.",null);
}//catch
return new Response(true,"Export - Successfully wrote table to file.",null);
}//xml
Also note... - I can export flawlessly using JsonObjectBuilder from javax.json.* to a json file - It is required to use the javax.xml library - I really don't know what I'm doing
It returns the code:
?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<hashMap/>
The table which I am trying to export does have data which is not represented in the XML output. Right now, the HashMap class has @XmlRootElement at the top. That is the only XML annotation in any class.
I also receive a NullPointerException when I change this line
jaxbContext = JAXBContext.newInstance(HashMap.class);
to this:
jaxbContext = JAXBContext.newInstance(Table.class);
The exception is as follows:
Exception in thread "main" java.lang.NullPointerException
at com.sun.xml.internal.bind.v2.runtime.property.SingleMapNodeProperty.serializeBody(SingleMapNodeProperty.java:252)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:345)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsSoleContent(XMLSerializer.java:578)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:326)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:479)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:308)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:236)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:95)
at driver.DExport.xml(DExport.java:99)
at driver.DExport.execute(DExport.java:59)
at core.Server.interpret(Server.java:57)
at core.Console.prompt(Console.java:56)
at core.Console.main(Console.java:37)
After much trial and error, it so turns out that I needed to create a list of Row extends HashMap<String,Object>
types. I guess the fact that there was nested hashmapping ( Table extends Hashmap<Object,Row extends Hashmap<String,Object>>
screwed it up. Now, my Row class contains @XmlRootElement, and I made my Tuple(known as Entry in java.util.Hashmap) static, with the get methods for key
and val
having the @XmlElement annotation. Now I must export in a loop. Pretty sure this is the wrong way to do it but it works(kinda)...
jaxbContext = JAXBContext.newInstance(Row.class);
jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
for (int i = 0; i<rows.size(); i++) {
jaxbMarshaller.marshal(rows.get(i), file);
}//for
and the output...
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<row>
<Tuple>
<key xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">str</key>
<val xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">goodbye</val>
</Tuple>
<Tuple>
<key xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">bool</key>
<val xsi:type="xs:boolean" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">false</val>
</Tuple>
</row>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<row>
<Tuple>
<key xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">str</key>
<val xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">hello</val>
</Tuple>
<Tuple>
<key xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">bool</key>
<val xsi:type="xs:boolean" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">true</val>
</Tuple>
</row>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.