0

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>

..

10
  • I didn't understand your problem. Do you want to know how to create an ArrayList to store the instances of rollingD? By the way, you should follow the Java naming convention i.e. rollingD should be RollingD. Commented Mar 28, 2020 at 12:10
  • From the first line of the code in your question, i.e. while(parser.hasNext()) {, is parser an 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 ? Commented Mar 28, 2020 at 12:35
  • @Arvind Kumar Avinash Its STAX rather than SAX. That is correct i would like to store it in an ArrayList. The while is from an API Commented Apr 4, 2020 at 12:45
  • Can you edit your question and post a sample XML that you are trying to parse? Commented Apr 4, 2020 at 12:51
  • @Abra I have provided below the custom class a sample of the XML which is repeated Commented Apr 4, 2020 at 12:56

1 Answer 1

1

Below is my implementation according to how I understood your question.

I created the following XML file that contains only the sample data from your question.

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <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>
</root>

Through experimentation, I saw that the effective order of the XML events is:

  1. START_ELEMENT
  2. CHARACTERS
  3. END_ELEMENT

The elements of interest are:

  1. settDate
  2. publishingPeriodCommencingTime
  3. fuelTypeGeneration

The basic algorithm is therefore:

  • If event type is START_ELEMENT and event name is settDate, create a new instance of class RollingD.
  • If event type is CHARACTERS, then set the value of the member in RollingD according to the event name.
  • If event type is END_ELEMENT and event name is fuelTypeGeneration, add the RollingD object to the list.

Note that you need to replace path-to-XML-file in the below code with the actual path to your XML file.

Also, method toString() in class RollingD is purely for debugging purposes and therefore can safely be removed from the below code if you think you don't need it.

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

public class StaxTest {
    private static final String SETT_DATE = "settDate";
    private static final String PUBLISHING_PERIOD_COMMENCING_TIME = "publishingPeriodCommencingTime";
    private static final String FUEL_TYPE_GENERATION = "fuelTypeGeneration";

    public static void main(String[] args) {
        XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
        XMLEventReader reader = null;
        Path path = Paths.get("path-to-XML-file");
        try (FileInputStream fis = new FileInputStream(path.toFile())) {
            reader = xmlInputFactory.createXMLEventReader(fis);
            List<RollingD> list = new ArrayList<>();
            RollingD rD = null;
            String localName = null;
            while (reader.hasNext()) {
                XMLEvent event = reader.nextEvent();
                switch (event.getEventType()) {
                    case XMLEvent.START_ELEMENT:
                        StartElement elem = event.asStartElement();
                        QName qName = elem.getName();
                        localName = qName.getLocalPart();
                        switch (localName) {
                            case SETT_DATE:
                                rD = new RollingD();
                                break;
                            case PUBLISHING_PERIOD_COMMENCING_TIME:
                                break;
                            case FUEL_TYPE_GENERATION:
                                break;
                            default:
                        }
                        break;
                    case XMLEvent.END_ELEMENT:
                        EndElement endElem = event.asEndElement();
                        QName qNamEnd = endElem.getName();
                        String endName = qNamEnd.getLocalPart();
                        switch (endName) {
                            case SETT_DATE:
                                break;
                            case PUBLISHING_PERIOD_COMMENCING_TIME:
                                break;
                            case FUEL_TYPE_GENERATION:
                                list.add(rD);
                                break;
                            default:
                        }
                        break;
                    case XMLEvent.CHARACTERS:
                        Characters chars = event.asCharacters();
                        if (!chars.isWhiteSpace()) {
                            String data = chars.getData();
                            switch (localName) {
                                case SETT_DATE:
                                    rD.setSettDate(data);
                                    break;
                                case PUBLISHING_PERIOD_COMMENCING_TIME:
                                    rD.setPublishingPeriodCommencingTime(data);
                                    break;
                                case FUEL_TYPE_GENERATION:
                                    rD.setFuelTypeGeneration(data);
                                    break;
                                default:
                            }
                        }
                        break;
                    default:
                        System.out.println("Unhandled XML event: " + event.getEventType());
                }
            }
            list.stream().forEach(System.out::println); // Prints the list.
        }
        catch (IOException | XMLStreamException x) {
            x.printStackTrace();
        }
        finally {
            if (reader != null) {
                try {
                    reader.close();
                }
                catch (XMLStreamException xXmlStream) {
                    System.out.println("WARNING (ignored): Failed to close XML event reader");
                    xXmlStream.printStackTrace();
                }
            }
        }
    }
}

class RollingD {
    private String settDate;
    private String publishingPeriodCommencingTime;
    private String fuelTypeGeneration;

    public RollingD() {
        this(null, null, null);
    }

    public RollingD(String settDate,
                    String publishingPeriodCommencingTime,
                    String fuelTypeGeneration) {
        this.settDate = settDate;
        this.publishingPeriodCommencingTime = publishingPeriodCommencingTime;
        this.fuelTypeGeneration = fuelTypeGeneration;
    }

    public void setSettDate(String settDate) {
        this.settDate = settDate;
    }

    public void setPublishingPeriodCommencingTime(String publishingPeriodCommencingTime) {
        this.publishingPeriodCommencingTime = publishingPeriodCommencingTime;
    }

    public void setFuelTypeGeneration(String fuelTypeGeneration) {
        this.fuelTypeGeneration = fuelTypeGeneration;
    }

    public String toString() {
        return String.format("%s,%s,%s",
                             settDate,
                             publishingPeriodCommencingTime,
                             fuelTypeGeneration);
    }
}

Here is the output I get after running the above code.

2020-02-29,09:55:00,31891
2020-02-29,10:00:00,31743
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.