java 如何在 JAXB 中使用 hashmap 属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5317172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:35:55  来源:igfitidea点击:

How to use hashmap properties with JAXB?

javajaxbhashmap

提问by opensourcegeek

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

我一直在摆弄 JAXB 一段时间,我需要生成如下的 xml

<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 元素的属性是动态的,可以来自属性文件或模板。将其放入如上所示结构的最佳方法是什么?我对动态变量使用哈希图,然后尝试用 XmlJavaTypeAdapter 映射它,我能做的最好的是

<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!

在 jaxb 中有没有办法说使用 hashmap 的键作为属性名称,将该键的值作为 xml 中该属性的值?或者,如果您认为有更好的方法可以做到,我愿意提供建议。很想用jaxb的marshaller来分别添加Root节点。但是,如果我可以使用 jaxb 的适配器会更好。谢谢!

回答by bdoughan

@XmlAnyAttribute is along the lines of what you need:

@XmlAnyAttribute 符合您的需求:

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>