4

I was wondering whether there is a way to create an object such that a list of such object does not need a root element. For example, if I wanted to create an XML like

<Dogs>
  <Dog>A</Dog>
  <Dog>B</Dog>
  <Dog>C</Dog>
</Dogs>

I could have the class Dogs which would be the root element and has a List<Dog>. Now supposed I want to get rid of the encapsulating element <Dogs>. So that the list of dog would look like

<Dog>A</Dog>
<Dog>B</Dog>
<Dog>C</Dog>

how should I construct my classes?

1
  • 2
    I'm pretty sure that is not valid XML. Or to be more precise, it's a valid XML fragment but not a valid XML document. So I don't think JAX-RS will allow that, at least not without doing a lot of weird changes.
    – Tyler
    Commented Jun 9, 2011 at 22:41

1 Answer 1

3

In XML this is not possible. The specification at http://www.w3.org/TR/xml/#NT-document clearly says that a document has one root element.

Your second XML-like code is therefore not an XML document, but a concatenation of three XML documents. But parsers aren't usually prepared for this kind of input.

1
  • But I did see xml file without one root element encapsulating all elements.And golang's package parse that with no problem, But I would like to know how to do it on java
    – Allan Ruin
    Commented Mar 5, 2014 at 2:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.