jaxb
  1. jaxb
  2. JAXB-975

Example wrong on http://jaxb.java.net/tutorial/section_3_1-Unmarshalling-and-Using-the-Data.html#Unmarshalling

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: 2.2.7
    • Fix Version/s: None
    • Component/s: docs
    • Labels:
      None
    • Environment:

      Online examples

      Description

      The example code on the following page are incorrect:

      It should be something like:

      public <T> T unmarshal( Class<T> docClass, InputStream inputStream )
      throws JAXBException {
      JAXBContext jc = JAXBContext.newInstance( docClass );
      Unmarshaller u = jc.createUnmarshaller();
      JAXBElement<T> doc = (JAXBElement<T>)u.unmarshal( inputStream );
      return doc.getValue();
      }

      Another issue with this example is that it doesn't demonstrate caching the JAXBContext (which is thread safe). If the intent is not to show reusing the JAXBContext, then the example could be further simplified to be:

      public <T> T unmarshal( Class<T> docClass, InputStream inputStream )
      throws JAXBException

      { return JAXB.unmarshal(inputStream, docClass); }

        Activity

        Hide
        blaise_doughan added a comment -

        There also appears to be some confusion as to what gets returned from the Unmarshal call. The code makes it appear as though it is always JAXBElement, when in reality it is only JAXBElement when the root element is mapped with @XmlElementDecl. Perhaps the code should be:

        public <T> T unmarshal( Class<T> docClass, InputStream inputStream )
        throws JAXBException

        { JAXBContext jc = JAXBContext.newInstance( docClass ); Unmarshaller u = jc.createUnmarshaller(); JAXBElement<T> doc = u.unmarshal( new StreamSource(inputStream) docClass ); return doc.getValue(); }
        Show
        blaise_doughan added a comment - There also appears to be some confusion as to what gets returned from the Unmarshal call. The code makes it appear as though it is always JAXBElement, when in reality it is only JAXBElement when the root element is mapped with @XmlElementDecl. Perhaps the code should be: public <T> T unmarshal( Class<T> docClass, InputStream inputStream ) throws JAXBException { JAXBContext jc = JAXBContext.newInstance( docClass ); Unmarshaller u = jc.createUnmarshaller(); JAXBElement<T> doc = u.unmarshal( new StreamSource(inputStream) docClass ); return doc.getValue(); }

          People

          • Assignee:
            Iaroslav Savytskyi
            Reporter:
            blaise_doughan
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated: