gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:11:07 28 4
gpt4 key购买 nike

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

<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”标签的表示,解释了一个 HashMap ,元素“key”和“value”没有单一值,而是有另一个标签(“string value =”…… “/')。我读过很多关于编码和解码的答案,但只有简单的 xml,我需要更复杂的东西(也许使用类型适配器??)。请有人帮助我! (对我糟糕的英语感到抱歉:( )

最佳答案

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

关于java - 将 Xml 转换为 Java Bean - Map 和 Tags 属性的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27278225/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com