将Xml转换为Java Bean - Map和Tags属性的问题

时间:2014-12-03 17:44:27

标签: java xml jaxb marshalling unmarshalling

我有一个非常棘手的问题。我无法在相应的java bean结构中转换一段XML文档。我的问题与标签属性和地图“键/值”属性有关。看看这篇文章:

<java>
<field name="stepName">
    <string value="inject-attribute-step"/>
</field>
<field name="params">
    <map>
        <entry>
            <key>
                <string value="value"/>
            </key>
            <value>
                <string value="4"/>
            </value>
        </entry>
        <entry>
            <key>
                <string value="variable"/>
            </key>
            <value>
                <string value="progress_bar_status_desc"/>
            </value>
        </entry>
    </map>
</field>

我想我要创建一个bean“JAVA”作为xml root。但是这个问题与“Field”类有关,它似乎有不同的实现(如何能够解决这个结构?)。然而,一个很大的问题是“map”标签的代表性,解释了一个没有单个值的元素'key'和'value'的hashmap,而是另一个标签('string value =“...... “/“)。我已经阅读了很多关于编组和解组的答案,但是只有简单的xmls,我需要更复杂的东西(也许是类型适配器?)。请有人帮助我! (对不起我可怕的英语:()

1 个答案:

答案 0 :(得分:1)

package JAXBImpl;

import java.io.File;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
class Java {

    @XmlElement(name="field")
    private List<Field> fields;

    public List<Field> getFields() {
        return fields;
    }

    public void setFields(List<Field> fields) {
        this.fields = fields;
    }
}

@XmlAccessorType(XmlAccessType.FIELD)
class Field {

    @XmlElement(name="map")
    private MapNode mapNode;

    @XmlElement(name="string")
    private StringNode stringNode;

    @XmlAttribute
    private String name;

    public MapNode getMapNode() {
        return mapNode;
    }

    public void setMapNode(MapNode mapNode) {
        this.mapNode = mapNode;
    }

    public StringNode getStringNode() {
        return stringNode;
    }

    public void setStringNode(StringNode stringNode) {
        this.stringNode = stringNode;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

@XmlAccessorType(XmlAccessType.FIELD)
class StringNode {
    @XmlAttribute
    private String value;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

@XmlAccessorType(XmlAccessType.FIELD)
class MapNode {

    @XmlElement(name="entry")
    List<Entry> entries;

    public List<Entry> getEntries() {
        return entries;
    }

    public void setEntries(List<Entry> entries) {
        this.entries = entries;
    }   
}

@XmlAccessorType(XmlAccessType.FIELD)
class Entry {

    @XmlElement(name="key")
    private Key key;

    @XmlElement(name="value")
    private Value value;

    public Key getKey() {
        return key;
    }

    public void setKey(Key key) {
        this.key = key;
    }

    public Value getValue() {
        return value;
    }

    public void setValue(Value value) {
        this.value = value;
    }
}

@XmlAccessorType(XmlAccessType.FIELD)
class Key {

    @XmlElement(name="string") 
    StringNode stringNode;

    public StringNode getStringNode() {
        return stringNode;
    }

    public void setStringNode(StringNode stringNode) {
        this.stringNode = stringNode;
    }
}

@XmlAccessorType(XmlAccessType.FIELD)
class Value {
    @XmlElement(name="string") 
    private StringNode stringNode;

    public StringNode getStringNode() {
        return stringNode;
    }

    public void setStringNode(StringNode stringNode) {
        this.stringNode = stringNode;
    }
}

public class Main {

    public static void main (String args []) 
    throws Exception
    {
        File file = new File("test.xml");
        JAXBContext jaxbContext = JAXBContext.newInstance(Java.class);

        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        Java java = (Java) jaxbUnmarshaller.unmarshal(file);
        for(Field field : java.getFields()) {
            System.out.println("-----------------------------------------------");
            System.out.println("Field Name = " + field.getName());
            if(field.getStringNode() != null) {
                System.out.println("String Value = " + field.getStringNode().getValue());
            }

            if(field.getMapNode() != null) {
                for(Entry entry : field.getMapNode().getEntries()) {
                    System.out.println("Key = " + entry.getKey().getStringNode().getValue());
                    System.out.println("Value = " + entry.getValue().getStringNode().getValue());
                }
            }
        }
    }      
}

<强>输出

-----------------------------------------------
Field Name = stepName
String Value = inject-attribute-step
-----------------------------------------------
Field Name = params
Key = value
Value = 4
Key = variable
Value = progress_bar_status_desc