0

I really need your help. I've got this XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<transaction xmlns="http://www.w3schools.com" transactionName="KYGGT003" application="KYGG" version="01" country="ES" language="EN" type="S" enable="1" pre_event="0" pre_log="0"post_channel="0" post_event="0" post_event_functional="0" post_journal="0" post_log="0" post_rop="0" synchronizedJournal="0" priority="0" timeout="30000" jpa="true">
  <paramsIn>
    <parameter order="1" name="BANK-CODE" type="String" size="4"mandatory="1" />
    <parameter order="2" name="COUNTRY-CODE" type="String" size="2"mandatory="1" />
  </paramsIn>
  <paramsOut>
    <parameter order="9" name="ENTRY-BRANCH-CODE" type="String"size="4" mandatory="0" />
    <parameter order="10" name="ENTRY-EMPLOYEE-USER-CODE" type="String"size="8" mandatory="0" />
    <parameter order="11" name="ENTRY-EMPLOYEE-CODE" type="String"size="20" mandatory="0" />
    <group name="AUTHENTICATION-TYPE" order="12">
      <parameter order="1" name="AUTHENTICATION-TYPE- CODE" type="String" size="2" mandatory="0" />
      <group name="USERS" order="2">
        <parameter order="1" name="USER" type="String" size="80" mandatory="0" />
        <group name="PASS" order="3">
          <parameter order="1" name="PASS" type="String" size="80" mandatory="0"/>
        </group>
      </group>
    </group>
  </paramsOut>
  <description>-</description>
</transaction>

And this is my java of GroupType:

@XmlRootElement(name = "group")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "",  propOrder = {"parameter","group"})
public class GroupType {

    @XmlElement(required = true)
    protected List<ParamType> parameter;
    @XmlElement(name = "group", required=true)
    protected List<GroupType> group;
    @XmlAttribute
    protected String name;
    @XmlAttribute
    protected String order;

    public List<ParamType> getParameter() {
        if (parameter == null) {
            parameter = new ArrayList<ParamType>();
        }
        return this.parameter;
    }

    public String getName() {
        return name;
    }

    public void setName(String value) {
        this.name = value;
    }

    public String getOrder() {
        return order;
    }

    public void setOrder(String value) {
        this.order = value;
    }

    public List<GroupType> getGroup() {
        if (group == null) {
            group = new ArrayList<GroupType>();
        }
        return this.group;
    }
}

How can I get the nested groups into the GroupType Object? I've been tried it for so long, testing several possibilities and I didn't find a solution.

Would you help me?

Thank you in advance, Regards. Pablo.

P. Solar
  • 339
  • 3
  • 10
  • What you have looks good, what does the XML look like when you populate the object and marshal it to XML? – bdoughan Oct 09 '14 at 14:31
  • It only processes til first parameter: . The Object 'GroupType' only contains name="AUTHENTICATION-TYPE" order="12" and the ParameterType Object but no the next GroupType nested. – P. Solar Oct 09 '14 at 14:38

1 Answers1

0

What you have should work. Below is what I did to try it out.

Java Model

GroupType.java

Same as you have in your question.

package-info.java

You need to ensure that your namespaces are mapped correctly. If your package was com.example then you would need the following package-info class.

@XmlSchema(
        namespace = "http://www.w3schools.com",
        elementFormDefault = XmlNsForm.QUALIFIED)
package com.example;

import javax.xml.bind.annotation.*;

Demo Code

input.xml

Below is the part of the XML document from your question that corresponds to the GroupType class that you are trying to unmarshal. Note I added the xmlns declaration from root element of the document from your question down to the new root element group.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<group name="AUTHENTICATION-TYPE" order="12" xmlns="http://www.w3schools.com" >
    <parameter order="1" name="AUTHENTICATION-TYPE- CODE" type="String" size="2" mandatory="0" />
    <group name="USERS" order="2">
        <parameter order="1" name="USER" type="String" size="80" mandatory="0" />
        <group name="PASS" order="3">
            <parameter order="1" name="PASS" type="String" size="80" mandatory="0"/>
        </group>
    </group>
</group>

Demo

Below is some code that unmarshals the XML to objects, and then marshals it back to XML.

import javax.xml.bind.*;
import java.io.File;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(GroupType.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("input.xml");
        GroupType group = (GroupType) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(group, System.out);
    }

}

Output

Below is the output from running the demo code. Note the parameter element is empty because I didn't map any of the properties in my recreation of your model.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<group xmlns="http://www.w3schools.com" name="AUTHENTICATION-TYPE" order="12">
    <parameter/>
    <group name="USERS" order="2">
        <parameter/>
        <group name="PASS" order="3">
            <parameter/>
        </group>
    </group>
</group>
bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • 1
    Let me try and I give you the feedback! Thanks all for your time! – P. Solar Oct 09 '14 at 15:22
  • It doesn't work... What I'm doing is.... Jaxb2Marshaller deserializer = new Jaxb2Marshaller(); Class>[] clases = { Transaction.class, ParamType.class, ObjectFactory.class, GroupType.class }; deserializer.setClassesToBeBound(clases); Transaction transaccion = null; ParamsIn paramsIn = null; ParamsOut paramsOut = null; try { transaccion = (Transaction) deserializer.unmarshal(new StreamSource(flujo)); paramsIn = transaccion.getParamsIn(); paramsOut = transaccion.getParamsOut(); } catch (Exception e) {} – P. Solar Oct 09 '14 at 15:35
  • @user1068980 - Did you try my code using straight JAXB APIs? Your comment indicates that you are interacting with JAXB via the Spring wrapper. It appears the problem is related to the Spring layer. – bdoughan Oct 09 '14 at 15:49