Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » XMLAdapter for list not working
XMLAdapter for list not working [message #1847351] Mon, 25 October 2021 00:28 Go to next message
C. Stuart Hardwick is currently offline C. Stuart HardwickFriend
Messages: 1
Registered: October 2021
Junior Member
I just cannot get the XMLAdapter to work where a collection of name/value pairs is required. I have a large application which I am converting FROM Castor TO Moxy, so I am mostly stuck with the classes I have, though I've refactored out the non-static inner classes. Things are working okay except until...

When it comes to an object with properties, I need to serialize the properties as name/value pairs something like this: <property key="File" value="classpath:tufts\sds\startup.sds"/>

Simple enough. The data is coming (probably with unnecessary complexity) from a deep, ugly custom class I don't want to mess with, and Moxy ignores it during marshalling. This shouldn't be a problem, it's what the XMLAdapter is for, right?

But...I can't get that to work--at all. Depending on how I set things, I can get a string or I can get nothing. I never get the xml I expect.

When I call the Marshal method on my adapter, it works in that it fetches all my property values and populates a nice simple Arraylist, but the data in that arraylist is not written out to my export file with the rest of the marshalling output.


//The XML ADAPTER====================================
package tufts.sds;

import jakarta.xml.bind.annotation.adapters.XmlAdapter;

public class MarshallingMetaMapAdapter extends XmlAdapter<MarshallingMyMapStringType,MetaMap> {

@Override
public MarshallingMyMapStringType marshal(MetaMap v) throws Exception {
MarshallingMyMapStringType myMap = new MarshallingMyMapStringType();

// Works (populates myMap.Items), but emits empty node in xml output
// for(Entry<String, Object> entry : v.getPersistEntries()) {
// MarshallingMyStringEntryType myMapEntryType = new MarshallingMyStringEntryType();
// myMapEntryType.key = entry.getKey();
// myMapEntryType.value = "DONKEY"; //entry.getValue();
// myMap.items.add(myMapEntryType);
// }
MarshallingMyStringEntryType myItem= new MarshallingMyStringEntryType();
myItem.key="A";
myItem.value="1";
myMap.items.add(myItem);
return myMap;

// Use with List<String> returns everything as one string, no elements.
// emailAddresses.add("donkey=fool");
// emailAddresses.add("~");
// emailAddresses.add("newt=weaslefool");
// return emailAddresses;

// Emits string, but not with usable xml node wrappers.
// return "<YANKEE>Doodle</YANKEE><YANKEE>dandy</YANKEE>"; //myMap;
}

@Override
public MetaMap unmarshal(MarshallingMyMapStringType arg0) throws Exception {
return null;
// MetaMap<Integer, String> hashMap = new HashMap<Integer, String>();
// for(MarshallingMyMapEntryType myEntry : arg0.entry) {
// hashMap.put(myEntry.key, myEntry.value);
// }
// return hashMap;
}
}

//THE ARRAYLIST (which should be mappable)

public class MarshallingMyMapStringType {

MarshallingMyMapStringType(){
}

public List<MarshallingMyStringEntryType> items = new ArrayList<MarshallingMyStringEntryType>();

}

//THE ENTRY TYPE (Which should also be mappable)
public class MarshallingMyStringEntryType {

MarshallingMyStringEntryType(){

}

@XmlAttribute
public String key;

@XmlValue
public String value;

}

No matter what I try, it doesn't work. I can populate the arraylist, but it isn't written out. I can populate a List<string> but it isn't written as XML (all content of all elements is written as a single string). The only thing that works is, if I reconfigure to return a string. it writes out that string. I suppose I could manually marshall and unmarshall into csv or something for just these property lists to get around this, but that's an aweful kludge.

Here's the binding file entry that tells moxy to use the adapter. It seems to work correctly.
<xml-java-type-adapter value="tufts.sds.MarshallingMetaMapAdapter" type="tufts.sds.MetaMap" />

Also from the bindings file, here's the object reference I'm currently stuck on. All attribs & elements work except "Properties" which is a list of name/value data. When it writes anything at all, it writes it inside the propertylist tag. When it does't,it leaves that tag empty or omits it entirly.

Surely I must be doing something very wrong, but I just can't see what it is, and at this point I'm just making things worse for playing with the code, hoping some experiment reveals the magic.

Help!

Thanks.

Re: XMLAdapter for list not working [message #1847849 is a reply to message #1847351] Wed, 10 November 2021 12:06 Go to previous message
Radek Felcman is currently offline Radek FelcmanFriend
Messages: 9
Registered: March 2021
Junior Member
Hello,
I hope, that attached example should help.
Previous Topic:MOXy - Json - Remove brackets from collection members
Next Topic:The class was compiled with an unsupported JDK.
Goto Forum:
  


Current Time: Fri Dec 09 20:40:24 GMT 2022

Powered by FUDForum. Page generated in 0.03457 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top