phade
phade

Reputation: 3

How can I POST a list of Elements without Root item as xml in Retrofit

I'm trying to POST a entity to a REST endpoint with Retrofit. I have no control over the endpoint code.

The entity consists of a list of objects of another entity.

public class DemoEntity {
  @ElementList(inline = true);
  private List<SubEntry> entries;
}

@Root(name = "subEntry")
public class SubEntry {
  @Attribute(name = "attr")
  private String attribute;
}

I want the resulting xml to look like this

<?xml version="1.0"...>
<subEntry attr="a" />
<subEntry attr="b" />
<subEntry attr="c" />

But the serializer always includes the root element of DemoEntity.

<?xml version="1.0"... ?>
<DemoEntity>
  <subEntry attr="a" />
  <subEntry attr="b" />
  <subEntry attr="c" />
</DemoEntity>

Is there any way to circumvent the default (and correct) behaviour to "ignore" the root element?

Upvotes: 0

Views: 125

Answers (1)

droidpl
droidpl

Reputation: 5892

The serializer seems to follow the official spec for XML. You can check that following SO answer. So basically for a well formed XML and compliant with the standard you need a root element.

What I can suggest is, if your really need to jump over the standard, you can:

  1. Build some processor that reading this annotations produce the output you want.
  2. Compose your document manually as strings and post it as the body for your request.

I would suggest firstly if the endpoint you are accessing really uses a non-compliant xml, just before going with a harder solution.

Upvotes: 1

Related Questions