simplexml icon indicating copy to clipboard operation
simplexml copied to clipboard

@ElementMap problem with composite values

Open hoomanv opened this issue 10 years ago • 9 comments

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

hoomanv avatar Nov 30 '15 13:11 hoomanv

Can you post your Java classes?

Why not try using @ElementList(inline=true)

Kisty avatar Nov 30 '15 14:11 Kisty

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.

hoomanv avatar Dec 01 '15 08:12 hoomanv

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.

Kisty avatar Dec 01 '15 10:12 Kisty

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>

hoomanv avatar Dec 02 '15 08:12 hoomanv

@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 <[email protected]>

To: ngallagher/simplexml [email protected] 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<dataSources> <dataSource name="a"> http://host1/ </dataSource> <dataSource name="b"> http://host2/ </dataSource> </dataSources>ElementMap forces me to change the structure of the xml to something like this:<dataSources> <dataSource name="a"> http://host1/ </dataSource> <dataSource name="b"> http://host2/ </dataSource> </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— Reply to this email directly or view it on GitHub.

ngallagher avatar Dec 05 '15 11:12 ngallagher

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.

hoomanv avatar Dec 05 '15 15:12 hoomanv

look at @ElementMapUnion From: hoomanv [email protected] To: ngallagher/simplexml [email protected] Cc: Niall Gallagher [email protected] 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<dataSource name="..."> ... ... ... </dataSource>It is with the composite types that breaks the code.

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

ngallagher avatar Dec 05 '15 16:12 ngallagher

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

hoomanv avatar Dec 06 '15 10:12 hoomanv

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

tprochazka avatar Apr 02 '16 20:04 tprochazka