JAXB Eclipse MOXy可空值,并且仅检索Map中的指定键 [英] JAXB Eclipse MOXy Nullable value and retrieve only a specified key in a Map

查看:94
本文介绍了JAXB Eclipse MOXy可空值,并且仅检索Map中的指定键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还有另一个更棘手的问题:

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低于

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>

如何在stringMap上排除该条目?我尝试使用虚拟访问方法,但可能无法正确配置它!封送处理后是否应该删除该值?

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?

最后一个问题是,如果我的属性值为空。我如何配置person-binding-xml文件(oxm.file)以使相关xml标签为空?

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?

我可以更改模型类,但我更喜欢不要如果可能,请添加任何代码行。 (这就是我填充xml文件的原因)

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>

对于第二个相关问题:
我尝试了nillable = true,但它没有不行!
因此,如果我对stringMap或firstName或其他任何东西使用null值,我将分别获得
并用任何主体将标签关闭。

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>

感谢所有人

推荐答案

我找到了正确的解决方案:

I found the right solution:

        import java.util.HashMap;
        import java.util.Map;
        import java.util.Map.Entry;

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

        public final class StringMapAdapter extends XmlAdapter<StringAdapterMap, Map<String, String>> {

            private final String OMIT = "birthPlaceString";

            @Override
            public Map<String, String> unmarshal(StringAdapterMap v) throws Exception {
                 HashMap<String, String> hashMap = new HashMap<String, String>();
                 System.out.println("unmarshal"); 
                 for(StringEntryType myEntryType : v.entry) {
                     hashMap.put(myEntryType.key, myEntryType.value);
                  }
                  return hashMap;
            }

            @Override
            public StringAdapterMap marshal(Map<String, String> v) throws Exception {
                StringAdapterMap myMapType = new StringAdapterMap();
              for(Entry<String, String> entry : v.entrySet()) {
                  StringEntryType EntryType = new StringEntryType();
                   EntryType.key= entry.getKey();
                   EntryType.value = entry.getValue();
                   if (!OMIT.equals(EntryType.key))
                       myMapType.entry.add(EntryType);
              }
              return myMapType;

            }

StringAdapterMap类:

StringAdapterMap class:

        import java.util.ArrayList;
        import java.util.List;

        public class StringAdapterMap {
             public List<StringEntryType> entry = new ArrayList<StringEntryType>();

        }

StringEntryType类:

StringEntryType class:

        public class StringEntryType {

               public String key;

               public String value;

        }

记住配置适配器

     <xml-element java-attribute="stringMap" name="string-map" >
      <xml-java-type-adapter value="it.cineca.jaxb.adapter.StringMapAdapter" />    
     </xml-element>

这篇关于JAXB Eclipse MOXy可空值,并且仅检索Map中的指定键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆