I've been trying to convert XML file to java objects efficiently but I haven't succeeded yet. I have seen JAXB notation, and a few others but they havent looked efficient to me and I need to use json. I need help with efficient code example.
-
1Good to hear that you have attempted this. What have you tried?Reimeus– Reimeus2012-08-27 11:54:15 +00:00Commented Aug 27, 2012 at 11:54
-
1Use JAXB is the better option to proceed further for marshalling and unmarshalling in javathar45– thar452012-08-27 11:54:26 +00:00Commented Aug 27, 2012 at 11:54
-
You could have at the very least provided an XML snippet you're trying to convert and the corresponding java class.Strelok– Strelok2012-08-27 11:56:51 +00:00Commented Aug 27, 2012 at 11:56
-
i tried unmarshalling by using JAXB but i had an exception about resource finding. it means i locate the xml file to wrong place. i located it webcontent in eclipse project but resuorce can't be founderdysson– erdysson2012-08-27 12:05:09 +00:00Commented Aug 27, 2012 at 12:05
-
I handled the problem. Thanks a lot to that tries t help. I used JAXB and with a little difference; importing the other version of the same named libray. thankserdysson– erdysson2012-08-27 12:48:55 +00:00Commented Aug 27, 2012 at 12:48
Add a comment
|
1 Answer
Do not invent the wheel. These libraries (GSON, Jackson...) are pretty fast, tested and have huge community. If it was easy to write things better, it would have been already done.
And this is not really a question ;-)
7 Comments
erdysson
i tried unmarshalling by using JAXB but i had an exception about resource finding. it means i locate the xml file to wrong place. i located it webcontent in eclipse project but resuorce can't be found
malejpavouk
update your question (exception, file hierarchy etc.). From your current post it is really hard to guess what went wrong and what you expect.
erdysson
JAXBContext context1 = JAXBContext.newInstance(JavaObject.class);final Unmarshaller unmarshaller = context1.createUnmarshaller(); File file = new File("sample-xml.xml"); final JavaObject object = (JavaObject) unmarshaller.unmarshal(new FileInputStream(file));
erdysson
in this case, i get file not found exception
malejpavouk
and where is the file, if in src directory, tham 1) make sure, that you have maven resources polugin configured 2) you should load it using classloader.getResource(). Also file has method getAbsolutePath, so printout the path and make sure, that the file is there.
|