1

I'm newbie in XML parsing and try to understand JAXB. Have the following task: Implemented the following method, to got the személy object by id parameter,but it returns null:

    public Személy getSzemélyById(String id) {

        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(Személy.class);
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        XMLReader xmlReader = spf.newSAXParser().getXMLReader();
        InputSource inputSource;
        inputSource = new InputSource(new FileReader("C:\\Users\\zbocskay.TS-EU\\Documents\\NetBeansProjects\\prt2014levzh\\people.xml"));
        SAXSource source = new SAXSource(xmlReader, inputSource);

        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        Személy személy = (Személy) unmarshaller.unmarshal(source);
        System.out.println(személy.toString());

        return new Személy(személy.getId(), személy.getVezetéknév(), személy.getKeresztnév(), személy.getÉletkor(), személy.getCím(), személy.státusz.DIÁK);
    } catch (JAXBException e) {
    } catch (FileNotFoundException | ParserConfigurationException ex) {
        Logger.getLogger(SzemélyDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SAXNotRecognizedException ex) {
        Logger.getLogger(SzemélyDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SAXNotSupportedException ex) {
        Logger.getLogger(SzemélyDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SAXException ex) {
        Logger.getLogger(SzemélyDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

Here is Személy Class:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Személy {

        public enum Státusz {
            FELNŐTT, DIÁK, NYUGDÍJAS, GYERMEK
        }

        @XmlElement
        protected String id;
        @XmlElement
        protected String vezetéknév;
        @XmlElement
        protected String keresztnév;

        protected Integer életkor;
        @XmlElement
        protected String cím;

        protected Státusz státusz;

        public Személy(String id) {
            super();
            this.id = id;
        }

        public void setÉletkor(Integer életkor) {
            this.életkor = életkor;
        }

        public String getVezetéknév() {
            return vezetéknév;
        }

        public void setVezetéknév(String vezetéknév) {
            this.vezetéknév = vezetéknév;
        }

        public String getKeresztnév() {
            return keresztnév;
        }

        public void setKeresztnév(String keresztnév) {
            this.keresztnév = keresztnév;
        }

        public String getCím() {
            return cím;
        }

        public void setCím(String cím) {
            this.cím = cím;
        }

        public String getId() {
            return id;
        }

        public Személy(String id, String vezetéknév, String keresztnév,
                Integer életkor, String cím, Státusz státusz) {
            this(id);
            this.vezetéknév = vezetéknév;
            this.keresztnév = keresztnév;
            this.életkor = életkor;
            this.cím = cím;
            this.státusz = státusz;
        }

        public Személy(String id, String vezetéknév, String keresztnév,
                String születésiDátum, String cím, String diákigazolványszám,
                Státusz státusz) throws ParseException {
            this(id);
            this.vezetéknév = vezetéknév;
            this.keresztnév = keresztnév;
            this.életkor = meghatározÉletkort(születésiDátum);
            this.cím = cím;
            this.státusz = státusz;
        }

        public Integer getÉletkor() {
            return életkor;
        }

        @Override
        public String toString() {
            return "Személy [id=" + id + ", vezetéknév=" + vezetéknév
                    + ", keresztnév=" + keresztnév + ", életkor=" + életkor
                    + ", cím=" + cím + ", státusz=" + státusz + "]";
        }

    }

And people.xml file with data:

    <?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE személyek SYSTEM "people.dtd">

<személyek>
    <személy id="micimacko">
        <vezetéknév>Mici</vezetéknév>
        <keresztnév>Mackó</keresztnév>
        <születésidátum>1921.08.21</születésidátum>
        <cím>Százholdas Pagony</cím>
        <fotó>http://upload.wikimedia.org/wikipedia/en/1/10/Winniethepooh.png
        </fotó>
    </személy>
</személyek>

Running in the main class by the following:

    public static void main(String args[]) {
    SzemélyDAO ddd = new SzemélyDAOImpl();
    System.out.println(ddd.getSzemélyById("micimacko"));
}

Could anybody help me, what am I doing wrong?Thanks.

  • JAXB is quite extended, so in its present form the only way to answer your question would be to describe the entire process and write most of the code. Therefore may I suggest you start by studying a good JAXB tutorial (like [this one](https://jaxb.java.net/tutorial/), or [the official EE tutorial](http://docs.oracle.com/javaee/5/tutorial/doc/bnazf.html)). Most IDEs have extended tooling for JAXB on board that will automate a large part of the work, so also look up what tools exist in or for your IDE. – fvu Jun 11 '14 at 11:02
  • Thanks for your reply @fvu . I'm using Netbeans 8.0, with JDK 1.8, I think the IDE consists all necessary tools for JAXB. The described method implements an interface. Also have no more information or codes I wrote below. – user3729595 Jun 11 '14 at 11:19
  • For Netbeans check out https://netbeans.org/kb/74/websvc/jaxb.html and http://javainxml.blogspot.be/2012/08/using-jaxb-with-netbeans-hello-world.html . The part that you need now is unmarshalling. Try following the tutorial using your example and I'm pretty sure things will be a lot clearer soon. – fvu Jun 11 '14 at 11:25
  • @fvu thanks for the info, it helps, I'm modified the code, and implemented the method, but it's not 100% – user3729595 Jun 11 '14 at 11:54

1 Answers1

0

As the exception states you need to add a no-arg constructor to your Személy class. This means a constructor that doesn't take any parameters. Currently the class has a single constructor that takes a String. Adding the following would work:

private Személy() {
}
bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • got the following error: javax.xml.bind.UnmarshalException - with linked exception: [org.xml.sax.SAXParseException; lineNumber: 3; columnNumber: 41; External DTD: Failed to read external DTD 'people.dtd', because 'file' access is not allowed due to restriction set by the accessExternalDTD property.] – user3729595 Jun 11 '14 at 12:12
  • @user3729595 have a look at http://stackoverflow.com/questions/9909465/how-to-disable-dtd-fetching-using-jaxb2-0 – fvu Jun 11 '14 at 12:28
  • Thanks @fvu but it didn't help. – user3729595 Jun 11 '14 at 14:39