如何添加同名的对象节点?
How to add ObjectNodes with same names?
我正在尝试为 Web 端点创建 xml 输出。
使用 ObjectNodes 构建 xml - 我无法添加具有相同名称的节点,
所以我需要
<feature_set>
<feature>
...
</feature>
<feature>
...
</feature>
....
我正在尝试这样做,但行不通:
ObjectNode featureSetNode = ((ObjectNode) sequenceNode).putObject("feature_set");
for (SimpleFeature feature : simpleFeatures ) {
JsonNode featureNode = featureSetNode.putObject("feature");
}
结果我只得到一个 feature
节点。
如何添加同名节点?
您可以让 jackson 使用这样的注释自动转换对象:
@JacksonXmlRootElement(localName = "feature_set")
public class FeatureRoot {
@JacksonXmlElementWrapper(useWrapping=false) //this removes wrapper tag
@JacksonXmlProperty(localName="feature")
private List<Feature> features;
public FeatureRoot(List<Feature> features) {
this.features = features;
}
public List<Feature> getFeatures() {
return features;
}
}
public class Feature {
private String name;
public Feature(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
代码如下
FeatureRoot featureRoot = new FeatureRoot(
List.of(
new Feature("test"),
new Feature("asd")
)
);
XmlMapper mapper = new XmlMapper();
String xml = mapper.writeValueAsString(featureRoot);
会产生
<feature_set>
<feature>
<name>test</name>
</feature>
<feature>
<name>asd</name>
</feature>
</feature_set>
要从头开始创建相同的密钥条目,请使用:
ObjectNode featureSetNode = sequenceNode.putObject("feature_set");
ArrayNode featureNode = featureSetNode.putArray("feature");
for (SimpleFeature feature : simpleFeatures ) {
ObjectNode featureNodeImpl = featureNode.addObject();
featureNodeImpl.put("start", feature.getSeqRegionStart());
}
XmlMapper mapper = new XmlMapper();
String xml = mapper.writeValueAsString(featureRoot);
我正在尝试为 Web 端点创建 xml 输出。 使用 ObjectNodes 构建 xml - 我无法添加具有相同名称的节点, 所以我需要
<feature_set>
<feature>
...
</feature>
<feature>
...
</feature>
....
我正在尝试这样做,但行不通:
ObjectNode featureSetNode = ((ObjectNode) sequenceNode).putObject("feature_set");
for (SimpleFeature feature : simpleFeatures ) {
JsonNode featureNode = featureSetNode.putObject("feature");
}
结果我只得到一个 feature
节点。
如何添加同名节点?
您可以让 jackson 使用这样的注释自动转换对象:
@JacksonXmlRootElement(localName = "feature_set")
public class FeatureRoot {
@JacksonXmlElementWrapper(useWrapping=false) //this removes wrapper tag
@JacksonXmlProperty(localName="feature")
private List<Feature> features;
public FeatureRoot(List<Feature> features) {
this.features = features;
}
public List<Feature> getFeatures() {
return features;
}
}
public class Feature {
private String name;
public Feature(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
代码如下
FeatureRoot featureRoot = new FeatureRoot(
List.of(
new Feature("test"),
new Feature("asd")
)
);
XmlMapper mapper = new XmlMapper();
String xml = mapper.writeValueAsString(featureRoot);
会产生
<feature_set>
<feature>
<name>test</name>
</feature>
<feature>
<name>asd</name>
</feature>
</feature_set>
要从头开始创建相同的密钥条目,请使用:
ObjectNode featureSetNode = sequenceNode.putObject("feature_set");
ArrayNode featureNode = featureSetNode.putArray("feature");
for (SimpleFeature feature : simpleFeatures ) {
ObjectNode featureNodeImpl = featureNode.addObject();
featureNodeImpl.put("start", feature.getSeqRegionStart());
}
XmlMapper mapper = new XmlMapper();
String xml = mapper.writeValueAsString(featureRoot);