8

Does anybody know how to attach custom XmlAdapter to objects contained in heterogenous list (like List)? For example - suppose we have a container class with a list of miscelaneous objects:

@XmlRootElement(name = "container")
public class Container {
    private List<Object> values = new ArrayList<Object>();

    @XmlElement(name = "value")
    public List<Object> getValues() {
        return values;
    }

    public void setValues(List<Object> values) {
        this.values = values;
    }   
}

If we put String-s, Integer-s and so on they will be mapped by default. Now suppose we want to put a custom object:

public class MyObject {
    //some properties go here   
}

The obvious solution is to write a custom adapter:

public class MyObjectAdapter extends XmlAdapter<String, MyObject> {
    //marshalling/unmarshalling
}

But unfortunately it doesn't work with package-level @XmlJavaTypeAdapter - adapter just never called so marhalling fails. And I can't apply it to getValues() directly because there can be other types of objects. @XmlJavaTypeAdapter only works if I have a field of MyObject type in Container (also with package-level annotaton):

public class Container {
    private MyObject myObject;
}

But this is not what I want - I want a list of arbitrary objects. I'm using standard Sun's JAXB implementation:

<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.2.3-1</version>
</dependency>

Any ideas how to do the mapping?

P.S.: One small clarification - I'd like to avoid annotating MyObject with JAXB annotations. It may be a class from third-party library which is not under my control. So any changes should be outside of MyObject.


Copy a non-shared file on a local network with a windows command

I'm using Windows 8.

How can I copy a non-shared file from another pc on my home network (another IP)?

This command can copy files, but only shared files:

copy \\{ip address}\Downloads\example C:\example_folder
copy (where and what) to (here) 

I'd like something like this. But I don't know how can I copy a non-shared File? Is there a simple windows command I can run to do this?

4

1 回答 1

1

您可以尝试更改您的MyObjectAdapter以便扩展XmlAdapter<String, List<Object>>。所以它应该看起来像这样:

public class MyObjectAdapter extends XmlAdapter<String, List<Object>> {
    //marshalling/unmarshalling
}

当您还向Container类中的 getter 添加 XmlJavaTypeAdapter 注释时,它应该可以工作。

@XmlRootElement(name = "container")
public class Container {
    private ArrayList<Object> values = new ArrayList<Object>();

    @XmlElement(name = "value")
    @XmlJavaTypeAdapter(MyObjectAdapter.class)
    public ArrayList<Object> getValues() {
        return values;
    }

    public void setValues(ArrayList<Object> values) {
        this.values = values;
    }   
}
于 2013-12-05T14:28:30.073 回答