Tim B
Tim B

Reputation: 41188

Missing name, in state: START_OBJECT parsing XML using Jackson

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

Answers (1)

Manos Nikolaidis
Manos Nikolaidis

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;
}
  1. I didn't need @JsonIgnoreProperties(ignoreUnknown = true) anywhere
  2. @JacksonXmlProperty(isAttribute = true) is needed for attributes like asset and numAssets
  3. There are 2 types of lists in the xml that are both unwrapped so specify it with this @JacksonXmlElementWrapper(useWrapping = false)
  4. You can parse the innermost double numbers with this @JacksonXmlText although the field in Java is not text.
  5. I introduced a wrapper class AssetMatrix to capture numAssets

Upvotes: 1

Related Questions