【问题标题】:Convert Xml to Java Bean - Problems with Map and Tags attributes将 Xml 转换为 Java Bean - Map 和 Tags 属性的问题
【发布时间】:2014-12-03 17:44:27
【问题描述】:

我有一个非常棘手的问题。我无法将一段 XML 文档转换为相应的 java bean 结构。我的问题与标签属性和映射“键/值”属性有关。看看这块:

<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 根。但问题与“Field”类有关,它似乎有不同的实现(如何描述这个结构?)。然而,一个大问题是在 'map' 标签的 rappresentation 中,解释了一个 hashmap,它没有元素 'key' 和 'value' 的单个值,而是有另一个标签('string value=" ...... "/')。我已经阅读了很多关于编组和解组的答案,但只有简单的 xml,我需要更复杂的东西(可能使用类型适配器??)。请有人帮助我! (对不起我糟糕的英语:()

【问题讨论】:

标签: java xml jaxb marshalling unmarshalling


【解决方案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

【讨论】:

    猜你喜欢
    • 2011-08-15
    • 1970-01-01
    • 2016-02-02
    • 2014-11-19
    • 2016-04-11
    • 2014-09-24
    • 2016-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多