Missing name, able: START_OBJECT XML parsing using Jackson

I am 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 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 deleted everything from the last inner class, but it still doesn't parse. I tried removing <correlations asset="Name1" />from input, but this is not the source of the problem. If I remove private List<Correlation> correlation;from Correlations, then it will succeed, but obviously does not have the necessary information.

What do I need to do differently here to parse what is essentially a 2-dimensional array of XML in 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:

, correlation. 1.23, 2.34 3.45 , - - , .

+4
1

xml ( , setCorrelation 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) -
  • @JacksonXmlProperty(isAttribute = true) , asset numAssets
  • xml, , @JacksonXmlElementWrapper(useWrapping = false)
  • @JacksonXmlText, Java .
  • I introduced a wrapper class AssetMatrixto capturenumAssets
+1
source

Source: https://habr.com/ru/post/1692104/


All Articles