I have another more tricky question:
E.g. Person Class has: --String firstName --String lastName --Map stringMap
Person person = new Person ();
person.setFirstName("FirstName");
person.setLastName("LastName");
Map<String,String> stringMap = new HashMap<String,String>();
stringMap.put("IwantThisKeyInXml","IwantThisValueInXml");
stringMap.put("IDONTwantThisKeyInXml","IDONTwantThisValueInXml");
InputStream iStream1 = classLoader.getResourceAsStream("person-binding.xml");
List<Object> fileList = new ArrayList<Object>();
fileList.add(iStream1);
Map<String, Object> properties = new HashMap<String,Object>();
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, fileList);
JAXBContext ctx = JAXBContext.newInstance(new Class[] { GaDictionary.class, Person.class }, properties);
Marshaller marshaller = ctx.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Writer output = new StringWriter();
marshaller.marshal(person, output);
System.out.println(output);
person-binding.xml is below
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="my.test.model" xml-mapping-metadata-complete="true">
<xml-schema
element-form-default="QUALIFIED"/>
<java-types>
<java-type name="Person" xml-accessor-type="NONE">
<xml-root-element/>
<xml-type prop-order="firstName lastName"/>
<java-attributes>
<xml-element java-attribute="firstName" name="first-name"/>
<xml-element java-attribute="lastName" name="last-name"/>
<xml-element java-attribute="stringMap" name="string-map"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
So accordingly the definition the result is:
<?xml version="1.0" encoding="UTF-8"?>
<person>
<first-name>FirstName</first-name>
<last-name>LastName</last-name>
<string-map>
<entry>
<key>IwantThisKeyInXml</key>
<value>IwantThisKeyInXml</value>
</entry>
<entry>
<key>IDONTwantThisKeyInXml</key>
<value>IDONTwantThisKeyInXml</value>
</entry>
</string-map>
</person>
How Could I exclude that entry on the stringMap? I tried with virtual access method but maybe I wasn't able to configure it properly?! Should I remove that value after marshaling? Which could it be a smart and maintainable way?
Last question is if I have null value for a property. How could I configure the person-binding-xml file (oxm.file) to get the relative xml tag empty?
I could change the model class but I prefer don't add any lines of code if it's possible. (This is the reason why I populated the xml file)
So the result that I would like to obtain is:
<?xml version="1.0" encoding="UTF-8"?>
<person>
<first-name>FirstName</first-name>
<last-name>LastName</last-name>
<string-map>
<entry>
<key>IwantThisKeyInXml</key>
<value>IwantThisKeyInXml</value>
</entry>
</string-map>
</person>
For the second related question: I tried nillable="true" in but it doesn't work ! So For istance If I have a null value for a stringMap or firstName or something else I will get respectively and tag closed with any body.
Or another way that I would like to obtain is:
<?xml version="1.0" encoding="UTF-8"?>
<person>
<first-name>FirstName</first-name>
<last-name>LastName</last-name>
<string-map>
<entry>
<key>IwantThisKeyInXml</key>
<value>IwantThisKeyInXml</value>
</entry>
<entry><!-- I have the entry but I have an empty value-->
<key>KEY</key>
<value></value>
</entry>
</string-map>
</person>
Thanks to all
If you wanted to prevent certain Map entries from being marshalled to XML, you could provide an XmlAdapter that would remove any unwanted entries at marshal time:
Not sure I understand your second question, could you explain a little more about what behaviour you'd like to see?
Hope this helps,
Rick
In your solution the null-policy is the right idea but it needs to be on the value attribute of the StringEntryType not on the whole map. So your bindings file should include something like this:
Your solution looks good but I thought I would also post an alternate soluction that makes use of MOXy's read-only and write-only attributes that you may prefer. In this example the variable myMap will be set during unmarshal but during marshal the Map returned from getModifiedMap method will be called. In the getModifiedMap we also replace the null values with empty strings so the empty tags will be marshalled.
and the corresponding bindings file would include something like this:
I found the right solution:
StringAdapterMap class:
StringEntryType class:
in oxm.file remember to configure the adapter