Home » Java » Generate Java classes from a XML file

Generate Java classes from a XML file

Posted by: admin October 22, 2018 Leave a comment

Questions:

I’m doing a program to convert XML to Java. However I have encountered issues with the code portion that follows, especially with what’s inside the “Trias” and with the “siri:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Trias xmlns:siri="http://www.siri.org.uk/siri" xmlns="http://www.vdv.de/trias" xmlns:ns3="http://www.ifopt.org.uk/acsb" xmlns:ns4="http://www.ifopt.org.uk/ifopt" xmlns:ns5="http://datex2.eu/schema/1_0/1_0" version="1.2">
     <SubscriptionRequest>
          <siri:RequestorRef>Test-TO</siri:RequestorRef>
          ...
     </SubscriptionRequest>
</Trias>

When I test the console sends:

javax.xml.bind.UnmarshalException: unexpected element
(uri:”http://www.vdv.de/trias“, local:”Trias”). Expected elements are
<{}Trias>

and

rg.apache.catalina.core.StandardWrapperValve invoke SEVERE:
Servlet.service() for servlet
[pt.thales.attracktive.communication.MobilepTT] in context with path
[/mobileptt] threw exception java.lang.NullPointerException

I’m using JAXB. If I remove what’s inside the “Trias” and the “siri:” it works fine thanks to:

    public Trias testXmlToObject(InputStream line)  {
        try {

            File("C:\\Users\\file1.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(Trias.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            Trias mobilepTT = (Trias) jaxbUnmarshaller.unmarshal(line);
            System.out.println(mobilepTT);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return null;
    }

Do you know what’s the issue?

When this problem is solved my goal is to generate Java objects from the XML. I started doing it starting by converting the XML to the XSD and using the command line xjc but I wish to program that.

Answers: