Search code examples
javaxmljaxbclasscastexceptionpojo

ClassCastException while extracting data from List


This is my XML file :-

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
<logExtract>
    <configuration>
        <splunk>
            <splunkHost>localhost</splunkHost>
            <userName>abcd</userName>
            <password>1234</password>
            <port>8214</port>
        </splunk>
        <tsdb>
            <tsdbHost>localhsot</tsdbHost>
            <port>4242</port>
        </tsdb>
    </configuration>
    <dataPart>
        <ingestion id="abc">
            <tsdbElements>
                <metricname>linecount0</metricname>
                <tags>splunk_server0</tags>
            </tsdbElements>
            <splunkQuery>
                <Query>index=_internal source=*/splunkd_access.log |head 0000</Query>
            </splunkQuery>
        </ingestion>
        <ingestion id="xyz">
            <tsdbElements>
                <metricname>linecount</metricname>
                <tags>splunk_server</tags>
            </tsdbElements>
            <splunkQuery>
                <query>index=_internal source=*/splunkd_access.log |head 1000</query>
            </splunkQuery>
        </ingestion>
        <ingestion id="def">
            <tsdbElements>
                <metricname>linecount2</metricname>
                <tags>splunk_server2</tags>
            </tsdbElements>
            <splunkQuery>
                <query>index=_internal source=*/splunkd_access.log |head 2000</query>
            </splunkQuery>
        </ingestion>
    </dataPart>
</logExtract>

I have used JAXB and created POJO class structure for it.

For Ingestion element this is my POJO class structure.

private String id;

private List<TsdbElements> TsdbElements;

private List<SplunkQuery> SplunkQuery;

@XmlAttribute
public String getId ()
{
    return id;
}

public void setId (String id)
{
    this.id = id;
}

@XmlElement
public List<TsdbElements> getTsdbElements ()
{
    return TsdbElements;
}

public void setTsdbElements (List<TsdbElements> TsdbElements)
{
    this.TsdbElements = TsdbElements;
}

@XmlElement
public List<SplunkQuery> getSplunkQuery ()
{
    return SplunkQuery;
}

public void setSplunkQuery (List<SplunkQuery> SplunkQuery)
{
    this.SplunkQuery = SplunkQuery;
}

@Override
public String toString()
{
    return "ClassPojo [id = "+id+", TsdbElements = "+TsdbElements+", SplunkQuery = "+SplunkQuery+"]";
}

Here is the Problem :-

When I try to extract Objects of ingestion I get error

(java.util.ArrayList cannot be cast to com.jaxb.xmlfile.Ingestio) java.lang.ClassCastException

at line below comment.

String fileName = "Query.xml";
File file = new File(fileName);

//JAXB Parsing - Unmarshling XML File
JAXBContext jaxbContext = JAXBContext.newInstance(XMLData.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

LogExtract logExtract = (LogExtract) jaxbUnmarshaller.unmarshal(file);

Configuration config = logExtract.getConfiguration();

Splunk spluknData = config.getSplunk();

Tsdb tsdbData = config.getTsdb();

DataPart dataPart = logExtract.getDataPart();

List<Ingestion> ingestionData = dataPart.getIngestion();

//Here I get Error 
List<TsdbElements> tsdbElementsData = ((Ingestion) ingestionData).getTsdbElements();

//Here I get Error     
List<SplunkQuery> splunkQueryData = ((Ingestion) ingestionData).getSplunkQuery();

System.out.println(spluknData.getSplunkHost() + "  " + spluknData.getUserName() + "  " + spluknData.getPassword() + "  " +spluknData.getPort());

System.out.println(tsdbData.getTsdbHost() + "  " + tsdbData.getPort());

for (SplunkQuery splunkQuery : splunkQueryData) {
    System.out.println(splunkQuery.getQuery());
}

for (TsdbElements tsdbElements : tsdbElementsData) {
    System.out.println(tsdbElements.getMetricname() + "  " + tsdbElements.getTags());
}

So what am I missing?

EDIT:- (After answer given by @Sanj)

How to save tsdbElement data using for loop and then access them again out of for loop? Any Idea? Because its only saving last XML data, not all of them


Solution

  • List<Ingestion> ingestionData = dataPart.getIngestion();
    
    //Here I get Error 
    List<TsdbElements> tsdbElementsData = ((Ingestion) ingestionData).getTsdbElements();
    

    The error is stating that ingestionData is a type List, and you are trying to cast it to the Ingestion class.

    Looking at your XML, you have a list of of these elements

        <ingestion id="abc">
            <tsdbElements>
                <metricname>linecount0</metricname>
                <tags>splunk_server0</tags>
            </tsdbElements>
            <splunkQuery>
                <Query>index=_internal source=*/splunkd_access.log |head 0000</Query>
            </splunkQuery>
        </ingestion>
    

    So you just need to iterate the list ingestionData to get the tsdbElements. Something like

    // instantiate the tsdbElementsData list
    List<TsdbElements> tsdbElementsData = new ArrayList<>(TsdbElements)
    
    for (Ingestion ingestion: ingestionData)
    {
        // get the elements
        tsdbElements = ingestion.getTsdbElements();
    
        // do some with the elements, e,g add to a another list
        tsdbElementsData.add(tsdbElements);
    }
    

    To iterate through the tsdbElementsData list, it is just another loop

    for (TsdbElements tsdbElements: tsdbElementsData)
    {
        // ... do something with tsdbElements
    }
    

    Note that the foreach loop above, is the same a writing

    for (int i = 0; i < tsdbElementsData.size(); i++)
    {
        TsdbElements tsdbElements = tsdbElementsData.get(i);
        // ... do something with tsdbElements
    }