简体   繁体   English

如何在JAXB中使用hashmap属性?

[英]How to use hashmap properties with JAXB?

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. Root元素的属性是动态的,可以来自属性文件或模板。 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 我正在使用动态变量的哈希映射,然后尝试使用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? 在jaxb中有没有办法说使用hashmap的键作为属性名称,并将该键的值作为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. 我正在考虑使用jaxb的marshaller分别添加Root节点。 However it would be nicer if I can just use jaxb's adapter. 但是如果我可以使用jaxb的适配器会更好。 Thanks! 谢谢!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM