我试图直接从xml文件返回一个Map。我尝试过创建一个自定义适配器,但问题是这个适配器没有从xml接收带有值的输入参数。
以下是问题的截图供参考。
这是我试图解析的xml
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<GroupRoot>
<Root>
<Group>
<UUID>ahsdlfkjadslkfjalkdsjflakjdslf</UUID>
<Name>Root Group 1</Name>
<Entry>
<UUID>1212135454==</UUID>
<String>
<Key>Notes</Key>
<Value>Notes from Manager</Value>
</String>
<String>
<Key>Item1</Key>
<Value>Item1Value</Value>
</String>
<String>
<Key>Item2</Key>
<Value>Item2Value</Value>
</String>
<String>
<Key>Item3</Key>
<Value>Item3Value</Value>
</String>
<String>
<Key>Item4</Key>
<Value>Item4Value</Value>
</String>
</Entry>
<Entry>
<UUID>45645466546546464==</UUID>
<String>
<Key>Notes</Key>
<Value>Notes from Manager</Value>
</String>
<String>
<Key>Item1</Key>
<Value>Item1Value1</Value>
</String>
<String>
<Key>Item2</Key>
<Value>Item2Value1</Value>
</String>
<String>
<Key>Item3</Key>
<Value>Item3Value1</Value>
</String>
<String>
<Key>Item4</Key>
<Value>Item4Value1</Value>
</String>
</Entry>
</Group>
</Root>
</GroupRoot>
&这是我的pojo。
package com.parser.xml.model;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "GroupRoot")
public class KeePassFile {
@XmlElement(name = "Root")
public Root root;
}
package com.parser.xml.model;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
public class Root {
@XmlElement(name = "Group")
public List<Group> groups;
}
package com.parser.xml.model;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
public class Group {
@XmlElement(name = "Name")
public String name;
@XmlElement(name = "Group", nillable = false )
public List<Group> subGroups;
@XmlElement(name = "Entry",nillable = false)
public List<Entry> entries;
}
package com.parser.xml.model;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
public class Entry {
@XmlElement(name = "UUID")
public String UUID;
@XmlElement(name = "String")
@XmlJavaTypeAdapter(StringMapperAdapter.class)
public Map<String, String> MappedItems;
}
class MapperString {
@XmlElement(name = "Key")
public String key;
@XmlElement(name = "Value")
public String value;
public MapperString() {
}
public MapperString(String key, String value) {
this.key = key;
this.value = value;
}
}
class StringMapperAdapter extends XmlAdapter<MapperString[], Map<String, String>> {
@Override
public Map<String, String> unmarshal(MapperString[] v) throws Exception {
HashMap<String, String> map = new HashMap<String, String>();
for (MapperString element : v) {
map.put(element.key, element.value);
}
return map;
}
@Override
public MapperString[] marshal(Map<String, String> v) throws Exception {
// TODO Auto-generated method stub
return null;
}
}
主要方法
package com.parser.xml;
import java.io.File;
import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import org.springframework.core.io.ClassPathResource;
import com.parser.xml.model.KeePassFile;
public class RunAppMain {
private static final String FILE_PATH = "TestXML.xml";
public static void main(String[] Args) throws IOException, JAXBException
{
File resource = new ClassPathResource(FILE_PATH).getFile();
JAXBContext context = JAXBContext.newInstance(KeePassFile.class);
Unmarshaller un = context.createUnmarshaller();
KeePassFile entry = (KeePassFile) un.unmarshal(resource);
System.out.println("Testing");
}
}
1条答案
按热度按时间wkftcu5l1#
我不知道是否可以在xs:序列上使用xmladapter。我成功地将xmladapter设置为
Entry
元素:使用的测试xml: