2

Important: I want to advance that I already read javax.xml.bind.UnmarshalException: unexpected element. Expected elements are (none) and tried the suggested solutions without success.


I tried to deserialize an XML into a JAXB class generated with xjc. This is the code:

final GeocodPlugin plugin;

final JAXBContext xmlContext = JAXBContext.newInstance(GeocodPlugin.class);
final Unmarshaller xmlDeserializer = xmlContext.createUnmarshaller();

try (final Reader reader = new StringReader(xml)) {
    plugin = (GeocodPlugin) xmlDeserializer.unmarshal(reader);
}

This is the error:

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"id"). Expected elements are (none)

This is the XML, created by another program (YAWL). I put [newline] only to signal there's a newline at the start and the end of the XML string:

[newline]
  <id>myid</id>
  <jsonrpc>2.0</jsonrpc>
  <method>geocod</method>
  <params>
    <userId>user</userId>
    <imagePathList>a16274073a714a20b434895e94e8c333</imagePathList>
    <imageIdList>a16274073a714a20b434895e94e8c333</imageIdList>
    <outputPathRoot>/home/database/share/CODELET_20190816144726</outputPathRoot>
    <preserveDirectoryMinDepth>-1</preserveDirectoryMinDepth>
    <preserveDirectoryMaxDepth>-1</preserveDirectoryMaxDepth>
    <polarisation>ANY</polarisation>
    [...other params...]
  </params>
[newline]

This is the JAXB class generated by xjc:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(
    name = "GeocodPlugin", propOrder = {
        "id",
        "jsonrpc",
        "method",
        "params"
    }
)

public class GeocodPlugin {
    @XmlElement(required = true)
    protected String id;
    @XmlElement(required = true)
    protected String jsonrpc;
    @XmlElement(required = true, defaultValue = "geocod")
    protected String method;
    @XmlElement(required = true)
    protected GeocodParameters params;

    [getters and setters]
}

ObjectFactory is not useful, since it's simply a factory with no additional annotations. It seems it continues to be generated only for backward compatibility. Anyway I tried also with it and I get the same problem.

I also tried to surround the XML with a <root> element and add @XmlRootElement(name = "root") to the class GeocodePlugin, but it gives me an absurd error:

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"root"). Expected elements are ({parameters}root)

where parameters is the namespace of the objects used as type of the params element, declared in the original XSD...

This is the original XSD:

<?xml version="1.0" ?>
<xs:schema jxb:version="2.0" targetNamespace="parameters" xmlns:enumerations="enumerations" xmlns:parameters="parameters" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:annotation>
    <xs:appinfo>
      <jxb:schemaBindings>
        <jxb:package name="my.plugin.package"/>
      </jxb:schemaBindings>
    </xs:appinfo>
  </xs:annotation>
  <xs:import namespace="enumerations" schemaLocation="total_enumerations_schema.xsd"/>
  <!-- @author Marco Sulla -->
  <xs:complexType name="GeocodParameters">
    <xs:sequence>
      <xs:element name="userId" nillable="true" type="xs:string"/>
      <xs:element maxOccurs="1000" minOccurs="1" name="imagePathList" nillable="true" type="xs:string"/>
      <xs:element maxOccurs="1000" minOccurs="0" name="imageIdList" nillable="false" type="xs:string"/>
      <xs:element default="-1" name="preserveDirectoryMinDepth" nillable="false" type="xs:integer"/>
      <xs:element default="-1" name="preserveDirectoryMaxDepth" nillable="false" type="xs:integer"/>
      <xs:element default="ANY" name="polarisation" nillable="false" type="enumerations:PolarisationEnum"/>
      [other params]
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="GeocodPlugin">
    <xs:sequence>
      <xs:element name="id" type="xs:string"/>
      <xs:element name="jsonrpc" type="xs:string"/>
      <xs:element default="geocod" name="method" type="xs:string"/>
      <xs:element name="params" type="parameters:GeocodParameters"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

I'm using Java 8 (Azul ZuluFx 8.0.202) and xjc 2.2.8

Marco Sulla
  • 15,299
  • 14
  • 65
  • 100

1 Answers1

1

I make it working. I had to add a <root> or whatever enclosing tag to the XML, add @XmlRootElement to the Java class (I don't know why xjc does not add it by default) and remove package-info.java generated by xjc, that contains:

@javax.xml.bind.annotation.XmlSchema(namespace = "parameters")
package my.plugin.package;

that I don't understand well its usefulness in the Java code. I also tried to add <parameters:root> and I get the error namespace not defined.

Marco Sulla
  • 15,299
  • 14
  • 65
  • 100