0

I have the following xml with measurement section, I want convert it to java objects. But I have problem with design POJO's object for this section. Can you help me please? Thanks

<root_element>
   <element>some_value</element>
   <element>some_value</element>     // SOME OTHERS ELEMENT'S
   <element>some_value</element>
   <measurement>                     // BEGINING OF THE SECTION WHERE I HAVE A PROBLEM
       <component>SO2</component>
       <averaged_time>
            <averaged_hours>1</averaged_hours>
            <value>9.054</value>
       </averaged_time>
       <component>NO2</component>
       <averaged_time>
            <averaged_hours>1</averaged_hours>
            <value>14.155</value>
       </averaged_time>
   </measurement>                    // END OF THE SECTION WHERE I HAVE A PROBLEM
   <element>some_value</element>
</root_element>

I have something like this (I did not write here other parameters, those work without problem). JAXB return me in Measurement just last one "component" and "averaged_time" elements. I know that ArrayList <Measurement> is bad approach.

@XmlRootElement
public class root_element {

      private ArrayList<Measurement> measurements;

      public ArrayList<Measurement> getMeasurements() {
            return measurements;
      }

      @XmlElement (name = "measurement")
      public void setMeasurement(ArrayList<Measurement> measurements) {
            this.measurements = measurements;
      }
}

Measurement object

public class Measurement {

    private String component;
    private AveragedTime averagedTime;  

    public String getComponent() {
        return component;
    }

    @XmlElement (name = "component")
    public void setComponent(String component) {
        this.component = component;
    }

    public AveragedTime getAveragedTime() {
        return averagedTime;
    }

    @XmlElement (name = "averaged_time")
    public void setAveragedTime(AveragedTime averagedTime) {
        this.averagedTime = averagedTime;
    }
}

AveragedTime object

public class AveragedTime {

        private int averaged_hours;
        private double value;

        public int getAveraged_hours() {
            return averaged_hours;
        }

        @XmlElement (name = "averaged_hours")
        public void setAveraged_hours(int averaged_hours) {
            this.averaged_hours = averaged_hours;
        }

        public double getValue() {
            return value;
        }

        @XmlElement (name = "value")
        public void setValue(double value) {
            this.value = value;
        }
0

1 Answer 1

2

The problem I see with XML, it should be

<root_element>
   <element>some_value</element>
   <element>some_value</element>     // SOME OTHERS ELEMENT'S
   <element>some_value</element>
   <measurement>                     // BEGINING OF THE SECTION WHERE I HAVE A PROBLEM
       <component>SO2</component>
       <averaged_time>
            <averaged_hours>1</averaged_hours>
            <value>9.054</value>
       </averaged_time>
   </measurement>
   <measurement>   
       <component>NO2</component>
       <averaged_time>
            <averaged_hours>1</averaged_hours>
            <value>14.155</value>
       </averaged_time>
   </measurement>                    // END OF THE SECTION WHERE I HAVE A PROBLEM
   <element>some_value</element>
</root_element>

Rest all your POJO seems fine i.e. in sync as per above suggested xml.

Otherwise if you need to continue with your existing XML, change your classes as follows:

@XmlRootElement
public class root_element {

      private Measurement measurements;

     //getter-setters
}

Measurement class

public class Measurement {

    private List<String> component;
    private List<AveragedTime> averagedTime;  

    //getter-setters

 }

Rest all seems ok.

Sign up to request clarification or add additional context in comments.

3 Comments

Yeh, I know, but this is not my xml :( I just need parse it.
@Samot updated the answer, but if possible change your xml schema as suggested in upper part of answer.
Thank you, it works. No, unfortunately I can not change the XML schema, because this xml is from Czech Hydrometeorological Institute website, yeah, national institute can't do xml schema correctly.

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.