Reputation: 41188
I'm trying to parse some XML that looks like this:
<correlationMatrix>
<assetMatrix numAssets="45">
<correlations asset="Name1" />
<correlations asset="Name2">
<correlation asset="Name3">1.23</correlation>
</correlations>
<correlations asset="Name4">
<correlation asset="Name5">2.34</correlation>
<correlation asset="Name6">3.45</correlation>
</correlations>
</assetMatrix>
</correlationMatrix>
I've created 3 classes:
@JsonIgnoreProperties(ignoreUnknown = true)
public class CorrelationMatrix {
private List<Correlations> assetMatrix;
public List<Correlations> getAssetMatrix() {
return assetMatrix;
}
public void setAssetMatrix(List<Correlations> assetMatrix) {
this.assetMatrix = assetMatrix;
}
}
And
@JsonIgnoreProperties(ignoreUnknown = true)
public class Correlations {
private String asset;
private List<Correlation> correlation;
public String getAsset() {
return asset;
}
public void setAsset(String asset) {
this.asset = asset;
}
public List<Correlation> getCorrelation() {
return correlation;
}
public void setCorrelations(List<Correlation> correlation) {
this.correlation = correlation;
}
}
Then finally
@JsonIgnoreProperties(ignoreUnknown = true)
public class Correlation {
}
As you can see I've removed everything from the final inner class, but it still fails to parse. I've tried removing <correlations asset="Name1" />
from the input but that's not the source of the problem. If I remove private List<Correlation> correlation;
from Correlations then that does then parse successfully but obviously doesn't have the information I need.
What is it that I need to do differently here to parse what is essentially a 2 dimensional array from XML into Java using Jackson (2.2.0 if that matters)?
The error I get is:
Missing name, in state: START_OBJECT (through reference chain: CorrelationMatrix["assetMatrix"]->Correlations["correlation"])
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(
Update:
The problem seems to be associated with the values inside correlation
. If I remove 1.23, 2.34 and 3.45 from my example data then it parses - so I need to somehow tell Jackson how to map them.
Upvotes: 4
Views: 208
Reputation: 22234
I was able to parse all the elements in the example xml with these modified classes (add getters, setters and use correct name setCorrelation
in Correlations
):
class CorrelationMatrix {
private AssetMatrix assetMatrix;
}
class AssetMatrix {
@JacksonXmlProperty(isAttribute = true)
private int numAssets;
@JacksonXmlElementWrapper(useWrapping = false)
private List<Correlations> correlations;
}
class Correlations {
@JacksonXmlProperty(isAttribute = true)
private String asset;
@JacksonXmlElementWrapper(useWrapping = false)
private List<Correlation> correlation;
}
class Correlation {
@JacksonXmlProperty(isAttribute = true)
private String asset;
@JacksonXmlText
private double correlation;
}
@JsonIgnoreProperties(ignoreUnknown = true)
anywhere@JacksonXmlProperty(isAttribute = true)
is needed for attributes like asset
and numAssets
@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlText
although the field in Java is not text.AssetMatrix
to capture numAssets
Upvotes: 1