JAVA - 使用 FasterXml.Jackson 将 XML 文件解析为 Map



我有以下XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<Site xmlns="bla" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  id="0" affiliations="foo" status="OPERATIONAL" color="#DB2253" updateTime="2002-12-17T09:30:47.0Z" name="GFB_0035" subject="GF">
<location>
<vertices Lat="41.905889" Lon="12.452019"/>
<vertices Lat="41.905973" Lon="12.453596"/>
<vertices Lat="41.905206" Lon="12.453707"/>
<vertices Lat="41.905058" Lon="12.452109"/>
</location>
</Site>

我正在使用FasterXml.Jackson将数据解析为Map<String, Object>对象使用以下代码:

XmlMapper xmlMapper = new XmlMapper();
Map<String, Object> data = xmlMapper.readValue(new File(xmlFile), Map.class);

但是,当我查看"location"字段的内容时,我只有一个顶点而不是四个顶点(最后一个(。

更新:我得到的地图对象看起来像下面的一个:

{id=0, affiliations=foo, status=OPERATIONAL, color=#DB2253, updateTime=2002-12-17T09:30:47.0Z, name=GFB_0035, subject=GF, location={vertices={Lat=41.905058, Lon=12.452109}}}

有没有办法使用FasterXml.Jackson获取所有四个条目?

根据Java文档 -

映射不能包含重复的键;每个键最多可以映射到一个值。

这就是为什么当您查看"位置"字段的内容时,您只有一个顶点而不是四个顶点(最后一个(。

我认为您应该在自己的类中解析XML,而不是将其存储到Map

见以下示例 -

import java.io.IOException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
public class Stack {

public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
String xmlFile = "<?xml version="1.0" encoding="UTF-8"?>"
+ "<Site xmlns="bla" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  id="0" affiliations="foo""
+ " status="OPERATIONAL" color="#DB2253" updateTime="2002-12-17T09:30:47.0Z" name="GFB_0035" "
+ "subject="GF">"
+ "    <location>"
+ "        <vertices lat="41.905889" lon="12.452019"/>"
+ "        <vertices lat="41.905973" lon="12.453596"/>"
+ "        <vertices lat="41.905206" lon="12.453707"/>"
+ "        <vertices lat="41.905058" lon="12.452109"/>"
+ "    </location>"
+ "</Site>";
XmlMapper mapper = new XmlMapper();
SiteData site = mapper.readValue(xmlFile, SiteData.class);
System.out.println(site);
}
}

站点数据.java

import com.fasterxml.jackson.xml.annotate.JacksonXmlProperty;
import com.fasterxml.jackson.xml.annotate.JacksonXmlRootElement;
@JacksonXmlRootElement(localName = "Site")
public class SiteData {
private String id;
private String affiliations;
private String status;
private String color;
private String updateTime;
private String name;
private String subject;
@JacksonXmlProperty(localName = "location")
private LocationData location;
public String getId() {
return id;
}
public void setIdValue(String id) {
this.id = id;
}
public String getAffiliations() {
return affiliations;
}
public void setAffiliations(String affiliations) {
this.affiliations = affiliations;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getUpdateTime() {
return updateTime;
}
public void setUpdateTime(String updateTime) {
this.updateTime = updateTime;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public LocationData getLocation() {
return location;
}
public void setLocation(LocationData location) {
this.location = location;
}
@Override
public String toString() {
return "Site [id=" + id + ", affiliations=" + affiliations
+ ", status=" + status + ", color=" + color + ", updateTime="
+ updateTime + ", name=" + name + ", subject=" + subject
+ ", locations=" + location + "]";
}
}

位置数据.java

import java.util.List;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
public class LocationData {
@JacksonXmlElementWrapper(useWrapping=false)
private List<VerticesData> vertices;
public List<VerticesData> getVertices() {
return vertices;
}
public void setVertices(List<VerticesData> vertices) {
this.vertices = vertices;
}
@Override
public String toString() {
return "LocationData [vertices=" + vertices + "]";
}
}

顶点数据.java

public class VerticesData {
private String lat;
private String lon;
public String getLat() {
return lat;
}
public void setLat(String lat) {
this.lat = lat;
}
public String getLon() {
return lon;
}
public void setLon(String lon) {
this.lon = lon;
}
@Override
public String toString() {
return "Vertices [lat=" + lat + ", lon=" + lon + "]";
}
}

输出:

Site [id=0, affiliations=foo, status=OPERATIONAL, color=#DB2253, updateTime=2002-12-17T09:30:47.0Z, name=GFB_0035, subject=GF, locations=LocationData [vertices=[Vertices [lat=41.905889, lon=12.452019], Vertices [lat=41.905973, lon=12.453596], Vertices [lat=41.905206, lon=12.453707], Vertices [lat=41.905058, lon=12.452109]]]]

相关内容

  • 没有找到相关文章

最新更新