Leon Roy
Leon Roy

Reputation: 580

Representing multiple element names in JAXB

I have the below XML which represents a call event and needs to be unmarshalled to a Java object. I'm using JAXB and able to handle most of it except the block which represents a collection of values as element names.

Short of creating a class for every possible element (ie. as given here) is there a way to represent this in JAXB more dynamically?

<message xmlns='jabber:client' from='pubsub.server.example.com' to='leon@server.example.com'
id='leon_office_vmstsp_default__leon@server.example.com__uz8mQ'>
<event xmlns='http://jabber.org/protocol/pubsub#event'>
    <items node='leon_office_vmstsp_default'>
        <item id='leon_office_vmstsp_default'>
            <callstatus xmlns='http://xmpp.org/protocol/openlink:01:00:00#call-status'
                busy='false'>
                <call>
                    <id>1411743786867</id>
                    <profile>leon_office</profile>
                    <interest>leon_office_vmstsp_default</interest>
                    <changed>State</changed>
                    <state>CallEstablished</state>
                    <direction>Outgoing</direction>
                    <duration>3</duration>
                    <caller>
                        <number>6001</number>
                        <name>leon_office</name>
                    </caller>
                    <called>
                        <number>3807</number>
                        <name>3807</name>
                    </called>
                    <actions>
                        <ClearCall />
                        <RemoveThirdParty />
                        <StopVoiceDrop />
                        <MuteParty />
                        <AddThirdParty />
                        <SendDigit />
                        <ClearConnection />
                        <UnMuteParty />
                        <StartVoiceDrop />
                    </actions>
                    <participants>
                        <participant exten='3807' />
                        <participant exten='6001' />
                    </participants>
                    <features>
                        <feature id='Conference'>true</feature>
                    </features>
                </call>
            </callstatus>
        </item>
    </items>
</event>
<headers xmlns='http://jabber.org/protocol/shim'>
    <header name='pubsub#subid'>aLfatHpG059mNcAe5ta11YNOAjA02l486sE1p4hK</header>
</headers>

Upvotes: 2

Views: 99

Answers (1)

Xstian
Xstian

Reputation: 8282

JAXB java classes generation

  1. Generate an XSD from your XML. You can use also this link or another tool works fine.
  2. Generate classes from XSD by XJC, Maven plugin see here

Below en example of maven-jaxb2-plugin

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.8.1</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <args>
            <arg>-Xannotate</arg>
            <arg>-Xnamespace-prefix</arg>
            <arg>-nv</arg>
        </args>
        <extension>true</extension>
        <forceRegenerate>true</forceRegenerate>
        <bindingDirectory>${basedir}/src/main/resources/xjb</bindingDirectory>
        <bindingIncludes>
            <include>*.xjb</include>
        </bindingIncludes>
        <schemas>
            <schema>
                <fileset>
                    <directory>${basedir}/src/main/resources/xsd</directory>
                    <includes>
                        <include>*.xsd</include>
                    </includes>
                </fileset>
            </schema>
        </schemas>
        <debug>true</debug>
        <verbose>true</verbose>
        <plugins>
            <plugin>
                <groupId>org.jvnet.jaxb2_commons</groupId>
                <artifactId>jaxb2-basics</artifactId>
                <version>0.6.2</version>
            </plugin>
            <plugin>
                <groupId>org.jvnet.jaxb2_commons</groupId>
                <artifactId>jaxb2-basics-annotate</artifactId>
                <version>0.6.2</version>
            </plugin>
            <plugin>
                <groupId>org.jvnet.jaxb2_commons</groupId>
                <artifactId>jaxb2-namespace-prefix</artifactId>
                <version>1.1</version>
            </plugin>
        </plugins>
    </configuration>
</plugin>

I hope it can help you.

Upvotes: 2

Related Questions