ngallagher / simplexml

Simple XML
Apache License 2.0
97 stars 38 forks source link

@ElementMap problem with composite values #2

Open hoomanv opened 9 years ago

hoomanv commented 9 years ago

There is no way I can parse the following xml using ElementMap

<dataSources>
    <dataSource name="a">
        <url>http://host1/</url>
    </dataSource>
    <dataSource name="b">
        <url>http://host2/</url>
    </dataSource>
</dataSources>

ElementMap forces me to change the structure of the xml to something like this:

<dataSources>
    <entry name="a">
        <dataSource name="a">
            <url>http://host1/</url>
        </dataSource>
    </entry>
    <entry name="b">
        <dataSource name="b">
            <url>http://host2/</url>
        </dataSource>
    </entry>
</dataSources>

It seems the only way to parse the orignal xml is to use ElementList and convert it to Map manually. Tell me if I'm wrong please

Kisty commented 9 years ago

Can you post your Java classes?

Why not try using @ElementList(inline=true)

hoomanv commented 9 years ago

It works with ElementList as I said. But I want a java.util.Map not a List

class Config
{
    @ElementMap(entry = "dataSources", attribute = true, key = "name")
    private Map<String, DataSource> dataSources;
}

class DataSource
{
    @Attribute
    String name;

    @Element
    String url;
}

I played with all permutations you can think of with ElementMap's attributes but nothing worked.

Kisty commented 9 years ago

Try this

class Config
{
    @ElementMap(name = "dataSources", attribute = true, inline = true, key = "name")
    private Map<String, DataSource> dataSources;
}

I have a structure something like this, but for @ElementList

@Root(name="dataSources")
class Config
{
    @ElementMap(attribute = true, inline = true, key = "name")
    private Map<String, DataSource> dataSources;
}

See if either solution works.

hoomanv commented 9 years ago

I have tried it before.

@Root(name="dataSources")
public class Config
{
    @ElementMap(attribute = true, inline = true, key = "name")
    public Map<String, DataSource> dataSources;
}

@Root(name = "dataSource")
public class DataSource
{
    @Attribute
    public String name;

    @Element
    public String url;
}

Above classes will not parse this

<dataSources>
    <dataSource name="a">
        <url>a</url>
    </dataSource>
</dataSources>

but will parse this

<dataSources>
    <dataSource name="a">
        <dataSource name="a">
            <url>a</url>
        </dataSource>
    </dataSource>
</dataSources>
ngallagher commented 9 years ago
@ElementMap(entry="dataSource", key="name", value="url", attribute=true) Map<String, String> dataSources Its documented here Overview, the unit tests on github contain dozens on examples of this.             Overview@Retention(value=RUNTIME)public @interface ElementMap The ElementMap annotation represents a method or field that is a Map for storing key value pairs.
View on simple.sourceforge.net Preview by Yahoo
 
  From: hoomanv <notifications@github.com>

To: ngallagher/simplexml simplexml@noreply.github.com Sent: Monday, 30 November 2015, 13:10 Subject: [simplexml] @ElementMap is just not right (#2)

There is no way I can parse the following xml using ElementMap

http://host1/
<dataSource name="b">
    <url>http://host2/</url>
</dataSource>

ElementMap forces me to change the structure of the xml to something like this:

http://host1/
<entry name="b">
    <dataSource name="b">
        <url>http://host2/</url>
    </dataSource>
</entry>

It seems the only way to parse the orignal xml is to use ElementList and convert it to Map manually. Tell me if I'm wrong please— Reply to this email directly or view it on GitHub.

hoomanv commented 9 years ago

First: I need a map of <String, DataSource> not <String, String>.

Second: You mentioned @ElementMap(... value="url" ...) What if dataSource was like this

<dataSource name="...">
    <url>...</url>
    <user>...</user>
    <pass>...</pass>
</dataSource>

It is with the composite types that breaks the code.

ngallagher commented 9 years ago

look at @ElementMapUnion From: hoomanv notifications@github.com To: ngallagher/simplexml simplexml@noreply.github.com Cc: Niall Gallagher gallagher_niall@yahoo.com Sent: Saturday, 5 December 2015, 15:41 Subject: Re: [simplexml] @ElementMap problem with composite values (#2)

First: I need a map of not .Second: You mentioned @ElementMap(... value="url" ...) What if dataSource was like this

...
<user>...</user>
<pass>...</pass>

It is with the composite types that breaks the code.

— Reply to this email directly or view it on GitHub.

hoomanv commented 9 years ago

I don't think @ElementMapUnion will be of any help in this case. Can you provide a sample code that works exactly with this xml

<dataSources>
    <dataSource name="...">
        <url>...</url>
        <user>...</user>
    </dataSource>
</dataSources>

And read it in a Map<String, DataSource> using either ElementMap or ElementMapUnion? I appreciate if you take your time to test it yourself

tprochazka commented 8 years ago

It looks that I have very similar problem http://stackoverflow.com/questions/36366430/list-as-value-of-map Any maybe this is also the same problem http://stackoverflow.com/questions/9058477/right-usage-of-org-simpleframework-xml-elementmap