Skip to content
Advertisement

I am trying to convert Xml doc to java obj.Its showing array index out of bounds exception fro the parameter serviceproviders

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
@JsonIgnoreProperties(ignoreUnknown = true)
@JacksonXmlRootElement(localName = "Data")
public class Data {
    @JacksonXmlProperty(localName="ServiceProviders")
private ServiceProviders serviceproviders;

public ServiceProviders getServiceproviders() {
    return serviceproviders;
}

public void setServiceproviders(ServiceProviders serviceproviders) {
    this.serviceproviders = serviceproviders;
}

public Data() {
    super();
}

@Override
public String toString() {
    return "Data [serviceproviders=" + serviceproviders + "]";
}


}

import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
@JsonIgnoreProperties(ignoreUnknown = true)
@JacksonXmlRootElement(localName = "ServiceProviders")
public class ServiceProviders {
    
    @JacksonXmlElementWrapper(localName="ServiceProvider")
@JsonProperty("ServiceProvider")
private List<ServiceProvider> serviceprovider;

public ServiceProviders() {
    super();
}

public List<ServiceProvider> getServiceprovider() {
    return serviceprovider;
}

public void setServiceprovider(List<ServiceProvider> serviceprovider) {
    this.serviceprovider = serviceprovider;
}

@Override
public String toString() {
    return "ServiceProviders [serviceprovider=" + serviceprovider + "]";
}
}
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
@JacksonXmlRootElement(localName = "ServiceProvider")
public class ServiceProvider {
    
    @JacksonXmlProperty(localName = "ID")
      @JsonProperty("ID")
    private String id;
    @JacksonXmlProperty(localName = "Name")
    private String Name;
    
    public ServiceProvider() {
        super();
    }
    
    public String getId() {
        
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return Name;
    }
    public void setName(String name) {
        Name = name;
    }
    public ServiceProvider(String id, String name) {
        super();
        this.id = id;
        Name = name;
    }
    @Override
    public String toString() {
        return "ServiceProvider [id=" + id + ", Name=" + Name + "]";
    }
    
   
}
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Parser {
    public static void main(String[] args) throws IOException {
        ObjectMapper objectMapper = new XmlMapper();
        // Reads from XML and converts to POJO
        Data data = objectMapper.readValue(
                StringUtils.toEncodedString(Files.readAllBytes(Paths.get("C:\Users\d.roopa.gadiparthi\Desktop\sprint25\CREtoJSON\src\main\resources\CRE.xml")), StandardCharsets.UTF_8),
                Data.class);
        System.out.println(data.getServiceproviders().getServiceprovider().get(1));
//        ServiceProvider data1 = objectMapper.readValue(
//                StringUtils.toEncodedString(Files.readAllBytes(Paths.get("C:\Users\d.roopa.gadiparthi\Desktop\sprint25\CREtoJSON\src\main\resources\CRE.xml")), StandardCharsets.UTF_8),
//                ServiceProvider.class);
//        System.out.println(data1.getId()+data1.getName());
       
    }
}

<Data>
<ServiceProviders>
      <ServiceProvider ID="0" Name="0:Divya" />
      <ServiceProvider ID="1" Name="1:Roopa" />
      <ServiceProvider ID="2" Name="2:ransit" />
     </ServiceProviders>
  </Data>
dependecies are
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.11.1</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
    <version>2.11.1</version>
</dependency>
<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-lang3</artifactId>
  <version>3.4</version>
</dependency>
</dependencies>

error Exception in thread “main” java.lang.IndexOutOfBoundsException: Index 1 out of bounds for length 0 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248) at java.base/java.util.Objects.checkIndex(Objects.java:373) at java.base/java.util.ArrayList.get(ArrayList.java:427) at Parser.main(Parser.java:18)

Advertisement

Answer

The problem is in your ServiceProviders class. There you have modeled the sequence of <ServiceProvider> elements by

@JacksonXmlElementWrapper(localName = "ServiceProvider")
@JsonProperty("ServiceProvider")
private List<ServiceProvider> serviceprovider;

That means, within <ServiceProviders>...</ServiceProviders> you expect XML input like this

<ServiceProvider>
    <ServiceProvider ID="0" Name="0:Divya" />
    <ServiceProvider ID="1" Name="1:Roopa" />
    <ServiceProvider ID="2" Name="2:ransit" />
</ServiceProvider>

instead of simply

<ServiceProvider ID="0" Name="0:Divya" />
<ServiceProvider ID="1" Name="1:Roopa" />
<ServiceProvider ID="2" Name="2:ransit" />

Of course you do not want the extra <ServiceProvider>...</ServiceProvider> wrapping. Therefore you need to modify the @JacksonXmlElementWrapper annotation to simply

@JacksonXmlElementWrapper(useWrapping = false)
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement