从 List 中提取数据时出现 ClassCastException

ClassCastException while extracting data from List

这是我的 XML 文件 :-

<?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>

我使用了 JAXB 并为其创建了 POJO class 结构。

对于 Ingestion 元素,这是我的 POJO class 结构。

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+"]";
}

这是问题所在:-

当我尝试提取 ingestion 的对象时出现错误

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

在评论下方的行。

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());
}

那我错过了什么?

编辑:-(@Sanj 给出答案后)

如何使用 for 循环保存 tsdbElement 数据,然后在 for 循环之外再次访问它们?任何的想法?因为它只保存最后 XML 个数据,而不是所有

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

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

错误表明 ingestionData 是一个类型列表,而您正试图将其转换为 Ingestion class。

查看您的 XML,您有这些元素的列表

    <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>

所以你只需要迭代列表 ingestionData 来获取 tsdbElements。像

// 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);
}

遍历 tsdbElementsData 列表,只是另一个循环

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

注意上面的foreach循环,是同一个写法

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