Author Topic: Stax Parser / arraylist  (Read 1501 times)

0 Members and 1 Guest are viewing this topic.

Offline Ignavis

  • NULL
  • Posts: 2
  • Cookies: 0
    • View Profile
Stax Parser / arraylist
« on: January 13, 2014, 08:46:49 pm »
I used arraylist and Stax parser to extract data from XML file.
This is how each row looks in my XML file (each row contains different data):

Code: [Select]
    <row>
      <millis>1000</millis>
      <stamp> 1273010254</stamp>
      <datetime> 2010/5/4 21:57:34</datetime>
      <light> 333</light>
      <temp> 78.32</temp>
      <vcc> 3.54</vcc>
    </row>

Here is a ArrayList I am using:

Code: [Select]
    public List<Measurement> readMeasurements(String xmlFile) {
           
            List<Measurement> measurements = new ArrayList<Measurement>();
            Measurement measurement = null;

Here is piece of code from Stax Parser class:

Code: [Select]
    if (xmlEvent.isStartElement()){
                        StartElement startElement = xmlEvent.asStartElement();
                        if(startElement.getName().getLocalPart().equals("row")){
                            measurement = new Measurement();
                        }
                        else if(startElement.getName().getLocalPart().equals("millis")){
                            xmlEvent = xmlEventReader.nextEvent();
                            measurement.setMillis(xmlEvent.asCharacters().getData());

and so on...

Still new in programming so my question is: How to calculate (find mean/average) of "light" or "millis" elements from mine arraylist and display it in a textbox. Don't need to be displayed at textbox but will be easier for me, because I will presenting data from textbox using Jfreechart.
I did some research and I read that IndexOf may be helpful since I am using equal in my code, but I am not sure how to manipulate specific element of my arraylist.
« Last Edit: January 13, 2014, 08:54:31 pm by Kulverstukas »

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Stax Parser / arraylist
« Reply #1 on: January 13, 2014, 08:59:44 pm »
Why do you store everything in an arraylist? a proper xml parser allows you to manipulate the XML tree as if it was an array.

But just to answer your question, make a method that receives a string which will be an xml tag you want the value of. In that method loop through the tags and compare. If a match is found, return the value.