Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Is recursive XML data possible with JAXB?

843834Dec 3 2002 — edited Dec 23 2002
Hello all,

Seems like recursive XML data should be simple enough to accomplish, but I'm seeing JAXB have some problems with it.

The complete example is listed below. I'm basically trying to construct a simple "menu", which has a recursive datatype called a "menuItem" (i.e. - each menuItem can have child menuItems). The source files ARE GENERATED SUCCESSFULLY, but I get the vague error below when trying to access the data.

Somebody PLEASE tell me that I don't need to write a custom binding for this!

Much thanks in advance!!!

Joel Wiegman


SCHEMA:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name="applicationContext" type="ApplicationContextType"/>

<xsd:complexType name="ApplicationContextType">
  <xsd:sequence>
    <xsd:element name="menu" type="Menu" maxOccurs="1"/>
  </xsd:sequence>
  <xsd:attribute name="id" type="xsd:string" use="required"/>
</xsd:complexType>

<xsd:complexType name="Menu">
  <xsd:sequence>
    <xsd:element name="menuItem" type="MenuItem" maxOccurs="unbounded"/>
  </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="MenuItem">
  <xsd:sequence>
    <xsd:element name="link" type="xsd:string"/>
    <xsd:element name="menuItem" type="MenuItem" maxOccurs="unbounded"/>
  </xsd:sequence>
  <xsd:attribute name="id" type="xsd:string" use="required"/>
  <xsd:attribute name="text" type="xsd:string" use="required"/>
</xsd:complexType>

</xsd:schema>
DATA:
<?xml version="1.0"?>
<applicationContext id="PPMS">
  <menu>
    <menuItem id="PA" text="Parameter Assignment">
      <link>javascript:window.showMenu(window.pgMenu);</link>
      <menuItem id="PASEARCH" text="Search">
        <link>paSearch.do</link>
      </menuItem>
      <menuItem id="PACREATE" text="Create">
        <link>paCreate.do</link>
      </menuItem>
    </menuItem>
  </menu>
</applicationContext>
HARNESS:
package foo;

import java.io.FileInputStream;
import java.util.Iterator;
import java.util.List;
import java.net.URL;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

public class ResourceManager {

  private static ResourceManager manager;
  private static ApplicationContext applicationContext;

  private ResourceManager() throws Exception {
    init();
  }

  public static ResourceManager getManager() throws Exception {
    if(manager == null)
      return new ResourceManager();

    return manager;
  }

  private void init() throws Exception {

      JAXBContext jc = JAXBContext.newInstance("foo");

      Unmarshaller u = jc.createUnmarshaller();

      URL url = this.getClass().getClassLoader().
                getResource("menu.xml");

      applicationContext =
          (ApplicationContext)u.unmarshal(url.openStream());

  }

  public ApplicationContext getApplicationContext() {
    return applicationContext;
  }

  public static void main (String[] args) {
    try {
      ApplicationContext context =
          ResourceManager.getManager().getApplicationContext();

      Menu menu = context.getMenu();
      MenuItem menuItem;

      Iterator menuItems = menu.getMenuItem().iterator();
      while(menuItems.hasNext()) {
        menuItem = (MenuItem)menuItems.next();
        System.out.println(menuItem.getText());
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

}
ERROR:
javax.xml.bind.UnmarshalException: Unexpected end of element {}:menuItem

	at com.sun.xml.bind.unmarshaller.UnreportedException.createUnmarshalException(UnreportedException.java:59)

	at com.sun.xml.bind.unmarshaller.SAXUnmarshallerHandlerImpl.reportAndThrow(SAXUnmarshallerHandlerImpl.java:406)

	at com.sun.xml.bind.unmarshaller.SAXUnmarshallerHandlerImpl.endElement(SAXUnmarshallerHandlerImpl.java:108)

	at org.apache.xerces.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:585)

	at org.apache.xerces.impl.XMLNamespaceBinder.handleEndElement(XMLNamespaceBinder.java:898)DefaultValidationEventHandler: [ERROR]: Unexpected end of element {}:menuItem



	at org.apache.xerces.impl.XMLNamespaceBinder.endElement(XMLNamespaceBinder.java:644)

	at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1008)

	at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(XMLDocumentFragmentScannerImpl.java:1469)

	at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:329)

	at org.apache.xerces.parsers.DTDConfiguration.parse(DTDConfiguration.java:525)

	at org.apache.xerces.parsers.DTDConfiguration.parse(DTDConfiguration.java:581)

	at org.apache.xerces.parsers.XMLParser.parse(XMLParser.java:152)

	at org.apache.xerces.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1175)

	at com.sun.xml.bind.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:139)

	at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:129)

	at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:166)

	at com.limited.storesystems.ppms.system.ResourceManager.init(ResourceManager.java:46)

	at com.limited.storesystems.ppms.system.ResourceManager.<init>(ResourceManager.java:17)

	at com.limited.storesystems.ppms.system.ResourceManager.getManager(ResourceManager.java:22)

	at com.limited.storesystems.ppms.system.ResourceManager.main(ResourceManager.java:57)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 20 2003
Added on Dec 3 2002
6 comments
1,277 views