tikxml icon indicating copy to clipboard operation
tikxml copied to clipboard

Support for reading List<String> and other primitves

Open sockeqwe opened this issue 8 years ago • 3 comments

As reported in #44 parsing an xml like

<week>
  <day>Monday</day>
  <day>Tuesday</day>
  <day>Wednesday</day>
  <day>Thursday</day>
  <day>Friday</day>
  <day>Saturday</day>
  <day>Sunday</day>
</week>

is not possible with @PropertyElement nor with @Element List<String> directly.

The current workaround is to use @TextContent but this requires an extra wrapper:

@Xml
public class Week {
  @Element
  List<Day> days;
}
@Xml
public class Day {
  @TextContent String name;
}

sockeqwe avatar Oct 19 '16 10:10 sockeqwe

On Latest also i.e 0.8.13, Can't we use @Element List<String> directly? it is giving exception like com.tickaroo.tikxml.TypeAdapterNotFoundException: No TypeAdapter for class java.lang.String found. Expected name of the type adapter is java.lang.String$$TypeAdapter

SindhuKonda avatar Sep 19 '18 12:09 SindhuKonda

You can use @Element on a List like List<SomeClass> but not on a list of primitives like List<String>, List<Int> and List<Double>`.

For List<String> a workaround is needed right now:

@Xml
public class SomeData {
  @Element
  List<String> someStrings;
}

@Xml
public class StringWorkaround {
  @TextContent String stringValue;
}

then you can parse it as a List<StringWorkaround>

sockeqwe avatar Sep 19 '18 13:09 sockeqwe

@sockeqwe But in my case not working. items always null

@Xml(name = "data")
public class Data{
...
   @Path("Items")
    @Element
    private List<Item> items;
....

@Xml(name = "item")
    public static class Item {
        @PropertyElement(name = "type")
        private int type;
       
        ....
}

XML content


<?xml version="1.0" encoding="utf-8"?>
<data>
…xml
    <Items>
        <item>
		 …
            <type>1</type>
        </item>
    </Items>
…
</workspace>

monxarat avatar Sep 09 '21 05:09 monxarat