Java JAXB exporting hashmaps to xml | بلاگ

Java JAXB exporting hashmaps to xml

تعرفه تبلیغات در سایت

آخرین مطالب

امکانات وب

Vote count: 0

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 - 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 class Table extends HashMap (Note: The key of type Object is always a string in my implementation) class Row extends HashMap (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

The error I receive is as follows:

javax.xml.bind.JAXBException: class adt.Table nor any of its super class is known to this context.
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:567)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:467)
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)

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)

Like I said before, I really don't know what I'm doing with regards to this xml library.

plz halp.

asked 1 min ago
spencerktm30

...
نویسنده : استخدام کار بازدید : 9 تاريخ : پنجشنبه 23 فروردين 1397 ساعت: 1:14