users@jaxb.java.net

UnmarshallException "Unexpected element {}:xxx"

From: Irv Thomae <ithomae_at_ists.dartmouth.edu>
Date: Tue, 03 Dec 2002 17:38:42 -0500

In this, my first attempt to use the jaxb-1.0-beta package for a real
application, I have been battling the
UnmarshallException quoted above for two full days, bouncing back and
forth between the "JAXB Users' Guide", another on-line tutorial, and
countless variations on my code - until I'm cross-eyed. It's probably
time to ask for help <g>.

  The XML files from which I need to read data look like this
  (I will use "..." to represent omitted repetition):<br>
==========================================================
<?xml version="1.0" encoding="UTF-8"?>

<bgpstats>
<date>2002-11-21 15:59</date>

<router name="63.209.255.229 (ATT-SD)">
<adds>6</adds>
<drops>1</drops>
</router>

... [many more "router" blocks] ...

<as number="701">
<routearounds>26</routearounds>
<disconnects>2</disconnects>
</as>
.....[Many more "as" blocks]...


</bgpstats>
====================================================

Here is the entire schema file, called "BgpXml.xsd":
======================================================
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name="bgpstats" type="BgpStatsType"/>

    <xsd:complexType name="BgpStatsType">
        <xsd:sequence>
            <xsd:element name="date" type="xsd:dateTime" minOccurs="1"
maxOccurs="1"/>
            <xsd:element name="router" type="routerReport" minOccurs="1"/>
            <xsd:element name="AS" type="ASreport" minOccurs="0"/>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:complexType name="routerReport">
        <xsd:attribute name="name" type="xsd:string" use="required"/>
        <xsd:attribute name="adds" type="xsd:integer"/>
        <xsd:attribute name="drops" type="xsd:integer"/>
    </xsd:complexType>

    <xsd:complexType name="ASreport">
        <xsd:attribute name="number" type="xsd:string" use="required"/>
        <xsd:attribute name="routearounds" type="xsd:integer"/>
        <xsd:attribute name="disconnects" type="xsd:integer"/>
    </xsd:complexType>

</xsd:schema>
=====================================================================


This was compiled on a Linux system using the "xjc.sh" provided in the
jaxb-1.0-beta package:

  xjc.sh -p BgpXml BgpXml.xsd


As expected from reading the Users' Guide, this produced a group of .java
files in directories BgpXml and BgpXml.impl , each declared a member of
package
"BgpXml".. They compiled without errors, using SDK1.3.1 (IBM's, actually.)
The test program (heavily influenced by the Users' Guide samples) just tries
to read a sample file (whose hard-coded name is "teststats.xml"):

====================BgpXmlReader.java===============================================

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.util.ValidationEventCollector;

// import java content classes generated by the binding compiler
import BgpXml.*;
import BgpXml.impl.*;

public class BgpXmlReader
{
  public static void main( String[] args )
  {
    try {
         // create a JAXBContext capable of handling classes generated into
         // the BgpXml package
          JAXBContext jc = JAXBContext.newInstance( "BgpXml" );

          ValidationEventCollector vce = new ValidationEventCollector();

            // create an Unmarshaller
          Unmarshaller u = jc.createUnmarshaller();
          u.setEventHandler(vce);

            // unmarshal a BgpXml instance document into a tree of Java
content
            // objects composed of classes from the primer.po package.
          Bgpstats stats =
                (Bgpstats)u.unmarshal( new FileInputStream(
"teststats.xml" ) );

            // examine some of the content
          java.util.Calendar timestamp = stats.getDate();
          System.out.println( "Contents of Test File dated " +
timestamp.toString());

          RouterReport report;
          ASReport ASr;
          int nRouters = 0;
          int nASrs = 0;
          while ( (report = stats.getRouter()) != null)
          {
            displayRouter(report);
            nRouters++;
          }
          System.out.println("Activity reported for " + nRouters + "
routers.");
          /*
          while ( (ASr = stats.getAS()) != null)
          {
            displayAS(ASr);
            nASrs++;
          }
          System.out.println("Some avoidance activity reported for "
+nASrs + " AS's.");
          */
        } catch( JAXBException je ) {
            je.printStackTrace();
        } catch( IOException ioe ) {
            ioe.printStackTrace();
        }
    }

    private static void displayRouter(RouterReport rpt)
    {
      System.out.println("\tFrom " + rpt.getName() + ':');
      System.out.println("\t\t" + (int)rpt.getAdds() + " adds, "
               + (int)rpt.getDrops() + " drops\n");
    }

    private static void displayAS(ASReport asr)
    {
      System.out.println("\tAS " + asr.getRoutearounds() + ",
"+asr.getDisconnects() + " disconnects.");
    }
}
========================================================================

But this program doesn't get very far. Instead of the first System.out
message, it throws an exception
and quits:

    javax.xml.bind.UnmarshallException: Unexpected element {}:adds

  (I've omitted the stack trace here ...)

Since the "adds" attribute is the first element defined for the complex
element "router", this error message
is bewildering to say the least. The example schema and code as
presented here have both been "boiled down" considerably since the
UnmarshallException was first seen, and many other variations have been
tried too, but nothing I've tried has worked. **What** am I missing???

Thanks,
   Irv Thomae

(PS: I am aware that the method used to retrieve successive
"RouterReport" objects will need corrections, but obviously we need to
get past the unmarshalling problem first.)