Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
Below is how you could handle this use case by leveraging MOXy's @XmlPath
extension.
XML Schema (schema.xsd)
Here is an XML schema based on the fragment you have provided in your question.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:forum17408865" xmlns="urn:forum17408865"
elementFormDefault="qualified">
<xs:element name="foo">
<xs:complexType>
<xs:sequence>
<xs:element name="bar" type="Activity"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="Activity">
<xs:sequence>
<xs:element name="log" type="DiagnosticLogMessage"
minOccurs="0" maxOccurs="1" />
<xs:element name="inputs" type="Mappings" minOccurs="0"
maxOccurs="1" />
<xs:element name="outputs" type="Mappings" minOccurs="0"
maxOccurs="1" />
<xs:element name="log" type="DiagnosticLogMessage"
minOccurs="0" maxOccurs="1" />
<xs:element name="to" type="DirectConnection" minOccurs="0"
maxOccurs="1" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="DiagnosticLogMessage" />
<xs:complexType name="Mappings" />
<xs:complexType name="DirectConnection" />
</xs:schema>
Activity
MOXy has an @XmlPath
extension that allows you to map to an XML element based on its position. This class can't be generated from the XML schema so we will create it ourselves.
package forum17408865;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder={"log1", "inputs", "outputs", "log2", "to"})
public class Activity {
@XmlPath("log[1]")
private DiagnosticLogMessage log1;
private Mappings inputs;
private Mappings outputs;
@XmlPath("log[2]")
private DiagnosticLogMessage log2;
private DirectConnection to;
}
binding.xml
To have JAXB use the class we created manually we will leverage a JAXB bindings file.
<jxb:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb" version="2.1">
<jxb:bindings schemaLocation="schema.xsd">
<jxb:bindings node="//xs:complexType[@name='Activity']">
<jxb:class ref="forum17408865.Activity" />
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
XJC Call
Below is the XJC call that leverages the bindings file. Note how we also needed to use the -nv
flag to disable schema validation.
xjc -nv -b binding.xml schema.xs
For More Information