如何在JAXB中使用hashmap属性? [英] How to use hashmap properties with JAXB?

查看:201
本文介绍了如何在JAXB中使用hashmap属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < Root       attr1 =attr2 =.. attrn => 
< CNode attr1 =attr2 =/>



< CNode .. />
< / Root>

根元素的属性是动态的,可以来自属性文件或模板。如上所示,将其纳入结构的最佳方法是什么?我为动态变量使用hashmaps,然后尝试使用XmlJavaTypeAdapter进行映射,我可以做的最好的是

 < Root> 
< Attribs>
< entry key =attr1>值< / entry>
< / Attribs>
< CNode .. />
< / Root>

在jaxb中有一种方法可以使用hashmap的键作为属性名称和该键的值作为XML属性的值?或者如果您认为有更好的方法可以做到这一点,我很乐意提供建议。我很想使用jaxb的编组器来分别添加Root节点。不过,如果我可以使用jaxb的适配器,它会更好。谢谢!

解决方案

@XmlAnyAttribute符合您的需求:



  import java.util.List; 
import java.util.Map;

import javax.xml.bind.annotation.XmlAnyAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.namespace.QName;

@XmlRootElement(name =Root)
public class Root {

private Map< QName,String>延期;
私人列表< CNode> cnodes;

@XmlAnyAttribute
public Map< QName,String> getExtension(){
返回扩展;


public void setExtension(Map< QName,String>扩展){
this.extension = extension;
}

@XmlElement(name =CNode)
public List< CNode> getCnodes(){
返回cnodes;
}

public void setCnodes(List< CNode> cnodes){
this.cnodes = cnodes;
}

}

CNode

  import java.util.Map; 
import javax.xml.bind.annotation.XmlAnyAttribute;
import javax.xml.namespace.QName;

公共类CNode {

私人地图< QName,String>延期;

@XmlAnyAttribute
public Map< QName,String> getExtension(){
返回扩展;


public void setExtension(Map< QName,String>扩展){
this.extension = extension;
}

}

演示

  import java.io.File; 
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
$ b $ public class Demo {

public static void main(String [] args)throws Exception {
JAXBContext jc = JAXBContext.newInstance(Root.class);

Unmarshaller unmarshaller = jc.createUnmarshaller();
Root root =(Root)unmarshaller.unmarshal(new File(input.xml));

Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
marshaller.marshal(root,System.out);
}

}

input.xml

 <?xml version =1.0encoding =UTF-8?> 
<根att1 =Aatt2 =B>
< CNode att3 =Catt4 =D/>
< CNode att5 =Eatt6 =F/>
< / Root>


I've been fiddling around with JAXB for a while now, I need to generate xml like below

<Root attr1="" attr2="" .. attrn="" >
  <CNode attr1="" attr2="" />
   .
   .
   .
   <CNode .. />
</Root>

The attributes of Root element is dynamic and would come from either a properties file or a template. What is the best way to get it into the structure as shown above? I'm using hashmaps for dynamic variables and then tried mapping it with XmlJavaTypeAdapter, the best I could do is

<Root>
  <Attribs>
      <entry key="attr1">Value</entry>
  </Attribs>
  <CNode .. />
</Root>

Is there a way in jaxb to say use hashmap's key as the attribute name and the value for that key as the value for that attribute in xml? Or if you think there is a better way to do it, I'm open for suggestions. I'm quite thinking about using jaxb's marshaller to add the Root node separately. However it would be nicer if I can just use jaxb's adapter. Thanks!

解决方案

@XmlAnyAttribute is along the lines of what you need:

Root

import java.util.List;
import java.util.Map;

import javax.xml.bind.annotation.XmlAnyAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.namespace.QName;

@XmlRootElement(name="Root")
public class Root {

    private Map<QName, String> extension;
    private List<CNode> cnodes;

    @XmlAnyAttribute
    public Map<QName, String> getExtension() {
        return extension;
    }

    public void setExtension(Map<QName, String> extension) {
        this.extension = extension;
    }

    @XmlElement(name="CNode")
    public List<CNode> getCnodes() {
        return cnodes;
    }

    public void setCnodes(List<CNode> cnodes) {
        this.cnodes = cnodes;
    }

}

CNode

import java.util.Map;
import javax.xml.bind.annotation.XmlAnyAttribute;
import javax.xml.namespace.QName;

public class CNode {

    private Map<QName, String> extension;

    @XmlAnyAttribute
    public Map<QName, String> getExtension() {
        return extension;
    }

    public void setExtension(Map<QName, String> extension) {
        this.extension = extension;
    }

}

Demo

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Root root = (Root) unmarshaller.unmarshal(new File("input.xml"));

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }

}

input.xml

<?xml version="1.0" encoding="UTF-8"?>
<Root att1="A" att2="B">
    <CNode att3="C" att4="D"/>
    <CNode att5="E" att6="F"/>
</Root>

这篇关于如何在JAXB中使用hashmap属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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