0

I'm having this issue with parsing XML using JAXB. Here is a simplified layout of the XML in question:

<message>
  <header>
    <network>NET</network>
    <sendTime>0722</sendTime>
  </header>
  <generalInformation>
    <senderReference>1234</senderReference>
    <linkage>
      <externalReference>extRef</externalReference>
    </linkage>
    <linkage>
      <internalReference>intRef</internalReference>
    </linkage>
    <linkage>
      <systemReference>sysRef</externalReference>
    </linkage>
  </generalInformation>
</message>

The problem I'm having is that these references are being sent under the linkage tag which is not unique, and also doesn't have a root like "linkages" which would allow me to easily wrap it in a list in Java, because the generalInformation tag has other tags in it. Here is how I have it set up so far:

@XmlRootElement(name="message")
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Message {
  private Header header;
  private GeneralInformation generalInformation;
}

@XmlRootElement(name="header")
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Header {
  private String network;
  private String sendTime;
}

@XmlRootElement(name="generalInformation")
@NoArgsConstructor
@AllArgsConstructor
@Data
public class GeneralInformation {
  private String senderReference;
  //How to create for linkages??
}

So my question to you is, how can I configure the GeneralInformation class to handle these multiple linkages? I am mostly concerned with unmarshalling from XML to Java at the moment.

  • If you don't have a schema, you should consider reverse-engineer one and compile your classes with `xjc` command. I outlined the entire process to build JAXB classes from scratch on this [Stack Overflow question](https://stackoverflow.com/a/70749964/2851311). Let me know if it works for you or if you have questions. – hfontanez Feb 15 '22 at 16:07

2 Answers2

1

Just define it as a List, for example:

private List<Linkage> linkage;

and define the Linkage class to have single String property:

@XmlRootElement(name="linkage")
...
public class Linkage {
  private String systemReference;
}
Krzysztof Majewski
  • 2,494
  • 4
  • 27
  • 51
  • Took a bit of massaging upstream, but it worked, thanks! – Kurt Werber Feb 15 '22 at 16:17
  • @KurtWerber that's because you shouldn't have to build these classes manually when you are a beginner. I am very experienced in JAXB and even I don't dare much to build JAXB classes by hand. I rather build the schema if one is not available and then compile them using `xjc` command. I posted a comment with a link under your answer. Even if your issue has been resolved, I invite you to try it at your own leisure. It will be time worth spending if you plan to use JAXB more in the future. – hfontanez Feb 15 '22 at 17:10
  • "_Took a bit of massaging upstream..._" - This is why answers also need to provide minimal, **_reproducible_ code_**. The OP figured this out quickly, but future users will come here and might not be so lucky. I didn't downvote your answer (I actually upvoted it) because that's not my style, but don't be shocked if others come here and do that for nor being a complete answer. – hfontanez Feb 15 '22 at 17:13
0
@XmlRootElement(name="generalInformation")
@NoArgsConstructor
@AllArgsConstructor
@Data
public class GeneralInformation {
  private String senderReference;
  private List<Linkage>;
}

@XmlRootElement(name="linkage")
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Linkage {
  private String externalReference;
  private String internalReference;
  private String systemReference;
}
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 15 '22 at 17:31