简体   繁体   English

将Xml转换为Java Bean-地图和标签属性问题

[英]Convert Xml to Java Bean - Problems with Map and Tags attributes

I've a very tricky problem. 我有一个非常棘手的问题。 I'm not able to convert a piece of XML document in a corresponding structure of java beans. 我无法在Java Bean的相应结构中转换一个XML文档。 My problems are related to tags attributes and map "key/value" attributes. 我的问题与标签属性和映射“键/值”属性有关。 Look at this piece : 看这块:

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

I suppose that i've to create a bean "JAVA" as xml root. 我想我必须创建一个bean“ JAVA”作为xml根。 But the problem is related to the "Field" class, that appears to have differents implementation (how can rappresent this structure ?). 但是问题与“ Field”类有关,该类似乎具有不同的实现(如何呈现这种结构?)。 However a big problem is in the rappresentation of 'map' tag , explaining an hashmap that does not have single value for elements 'key' and 'value' , but has another tag instead ('string value=" ......"/'). 但是,“ map”标记的重现是一个大问题,它解释了一个散列图,它没有元素“ key”和“ value”的单个值,而是具有另一个标记(“ string value =“ ...... “/“)。 I've read a lot of answers about marshalling and unmarshalling but there'is only simple xmls, i need something more complex (maybe with type adapters ?? ).Please someone help me ! 我已经阅读了很多有关封送和拆组的答案,但是只有简单的xml,我需要更复杂的东西(也许使用类型适配器??)。请有人帮我! (and sorry for my horrible english :( ) (对不起,我的英语太糟糕了:()

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());
                }
            }
        }
    }      
}

Output 产量

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

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

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