Unmarshal XML with JAXB

letharion
letharion used Ask the Experts™
on
I'm trying to parse XML in Java.
JAXB sounded like it was the way to go for me, and I'm looking at:
https://jaxb.dev.java.net/tutorial/section_3_1-Unmarshalling-and-Using-the-Data.html
I'm a bit uncertain how to call the unmarshal function in the example.
I thought the function wanted the class representing the xml's root node and an inputstream from which to read the xml data. I can't compile my code however.
//Relevant code
    private ObjectFactory of;
    private static GhSettings ghs;

    public JAXB(){
        of = new ObjectFactory();
        ghs = of.createGhSettings();
        FileInputStream is = new FileInputStream("schema.xsd");
        unmarshal(ghs, is);
    }

//Error message:
JAXB.java:15: <T>unmarshal(java.lang.Class<T>,java.io.InputStream) in JAXB cannot be applied to (jaxb.GhSettings,java.io.FileInputStream)
                unmarshal(ghs, is);

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
why are you passing jaxb.GhSettings as an argument to unmarshaller?
please check if you can try without it.
Commented:
Assuming the file 'test.xml' contains an XML representation of the class X:

X instance = unmarshal(X.class, new FileInputStream(new File("test.xml")))
ioanton:
That seems to work really well, I get a JAXBException during parse, I'm just gonna sort that out to see that nothing else related to this turns up, but I think that was it.

Is there any chance you could elaborate a bit for a java newbie so I can better understand what's going on next time?
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Commented:
For more details on this subject you can take a look at:

https://jaxb.dev.java.net/

section Documentation

Author

Commented:
I updated my xsd and ran the program again, now I get this run time failure:
# java JAXB
Exception in thread "main" java.lang.ClassCastException: jaxb.GhSettings cannot be cast to javax.xml.bind.JAXBElement
        at JAXB.unmarshal(JAXB.java:52)
        at JAXB.<init>(JAXB.java:16)
        at JAXB.main(JAXB.java:43)

On the understanding front, I was more thinking about the generic class parameter, rather than JAXB itself :)
//Relevant code, I think:
Line 16: GhSettings settings = unmarshal(GhSettings.class, is);
Line 52: JAXBElement<T> doc = (JAXBElement<T>)u.unmarshal( inputStream );

Open in new window

Commented:
As I said, you must provide an InputStream built using the xml representation of an instance of the class GhSettings as the second argument. If your class has the following structure:

@XmlRootElement
public class GhSettings {
  String name;
  String value;
  ......
}

then the xml file should look like this:

<?xml version="1.0" encoding="UTF-8">
<GhSettings>
  <name>...</name>
  <value>...</value>
</GhSettings>

Author

Commented:
Unfortunately I still don't understand a lot about the problem.

I did notice however that the poster on suns forum used a simpler code than mine to achieve the same result (AFAICT). I tried that, and then the remarks about casting further down makes sense, and also works.

Having spent quite a bit of time with this I'm going to pick the easy way out and go with what works.
Maybe I'll work up some understanding later on, particularly if problems arise.

Thanks for you're time.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial