0

My goal is to convert the XML to JavaObject and have the JavaObject be able to handle one or more "Tasks".

I would also like to be able to convert the JavaObject to JSON as well.

How can this be done? At it's current state, I am able to convert if only one task exists, anymore results in an error.

I have an XML file that follows this structure:

<Plan>
    <ID></ID>
    <Comment></Comment>
    <CreateTime></CreateTime>
    <Task>...</Task>
    <Task>...</Task>
    <Task>...</Task>
</Plan>

Each Task element is structured like so:

    <Task>
      <Type></Type>
      <Time></Time>
      <Target>
         <Code></Code>
         <TargetId></TargetId>
         <Content>
            <Stuff1></Stuff1>
            <Stuff2></Stuff2>
            <Stuff3></Stuff3>
         </Content>
      </Target>
    </Task>

I am able to deserialize the XML when there is only one Task involved. Anymore gives me errors.

My Classes (simplified) are as follows:

@Root(name = "Plan")
public class Plan{
   @Element(name = "ID")
   private String id;
   
   @Element(name = "Comment")
   private String comment;

   @Element(name = "CreateTime")
   private String createTime;

   @Element(name = "Task")
   private Task task; 
}

@Root(name = "Task")
public class Task{

   @Element(name = "Type")
   private String type;
   
   @Element(name = "Time")
   private String time;

   @Element(name = "Target")
   private Target target;
}

@Root(name = "Target")
public class Target{

   @Element(name = "Code")
   private String code;
   
   @Element(name = "TargetId")
   private String targetId;

   @Element(name = "Content")
   private Content content;
}

@Root(name = "Content")
public class Content{

   @Element(name = "Stuff1")
   private String stuff1;
   
   @Element(name = "Stuff2")
   private String stuff2;

   @Element(name = "Stuff3")
   private String stuff3;
}

In the schema file, I set this but doesn't seem to work:

<xs:element name="Task" type="Task" minOccurs="1" maxOccurs="unbounded">

1 Answer 1

1

At it's current state, I am able to convert if only one task exists, anymore results in an error.

This happens because your element annotation interprets the task tag as a property, so if there is more than one it raises an error. To avoid this behaviour you can use the JacksonXmlElementWrapper annotation you can apply to your task list:

public class Plan {

    @JacksonXmlProperty(localName = "Task")
    @JacksonXmlElementWrapper(useWrapping = false)
    List<Task> tasks;
}

public class Task {}

This above is a starting basic example you can use to define more complicated xmls.

Sign up to request clarification or add additional context in comments.

1 Comment

@Gamblers You have to create a Target class and put inside the Task class the Target field with @JacksonXmlProperty(localName = "Target"), after you begin to put inside the Target class the other fields and so on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.