I have written the following code. However I would like to add the items from characters.getData() into a custom array as below so I can then use it to carry out mathematical graphing. How can I create my own custom array list containing settDate, publishingPeriodCommencingTime and publishingPeriodCommencingTime?
while(parser.hasNext()) {
XMLEvent event = parser.nextEvent();
switch(event.getEventType()) {
case XMLStreamConstants.START_ELEMENT:
StartElement startElement = event.asStartElement();
String qName = startElement.getName().getLocalPart();
if (qName.equalsIgnoreCase("settDate")) {
bMarks = true;
} else if (qName.equalsIgnoreCase("publishingPeriodCommencingTime")) {
bLastName = true;
} else if (qName.equalsIgnoreCase("fuelTypeGeneration")) {
bNickName = true;
}
break;
case XMLStreamConstants.CHARACTERS:
Characters characters = event.asCharacters();
if(bMarks) {
System.out.println("settDate: " + characters.getData());
bMarks = false;
}
if(bLastName) {
System.out.println("publishingPeriodCommencingTime: " + characters.getData());
bLastName = false;
}
if(bNickName) {
System.out.println("fuelTypeGeneration: " + characters.getData());
bNickName = false;
}
rollingD subAction= new RollingD(characters.getData(), characters.getData(), characters.getData());
break;
case XMLStreamConstants.END_ELEMENT:
EndElement endElement = event.asEndElement();
if(endElement.getName().getLocalPart().equalsIgnoreCase("item")) {
System.out.println();
}
break;
}
}
This is my custom class
public class RollingD {
private String settDate;
private String publishingPeriodCommencingTime;
private String fuelTypeGeneration;
RollingD(String bMarks, String bLastName, String bNickName) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public String getSettDate() {
return settDate;
}
public void setSettDate(String settDate) {
this.settDate = settDate;
}
public String getPublishingPeriodCommencingTime() {
return publishingPeriodCommencingTime;
}
public void setPublishingPeriodCommencingTime(String publishingPeriodCommencingTime) {
this.publishingPeriodCommencingTime = publishingPeriodCommencingTime;
}
public String getFuelTypeGeneration() {
return fuelTypeGeneration;
}
public void setFuelTypeGeneration(String fuelTypeGeneration) {
this.fuelTypeGeneration = fuelTypeGeneration;
}
}
This is a sample of the XML
<settDate>2020-02-29</settDate>
<publishingPeriodCommencingTime>09:55:00</publishingPeriodCommencingTime>
<fuelTypeGeneration>31891</fuelTypeGeneration>
<settDate>2020-02-29</settDate>
<publishingPeriodCommencingTime>10:00:00</publishingPeriodCommencingTime>
<fuelTypeGeneration>31743</fuelTypeGeneration>
..
ArrayListto store the instances ofrollingD? By the way, you should follow the Java naming convention i.e.rollingDshould beRollingD.while(parser.hasNext()) {, isparseran instance of class SAXParser ? If it is, then is the code you posted part of some kind of ContentHandler ? Maybe you can edit your question and post a minimal reproducible example ?