0

I am new to java and I am stuck at one place. I am taking xml request which is coming in array.

<PassengersDetails>
 <Passenger>
  <passesngerName>ABC</passesngerName>
  <passengerAge>25</passengerAge>
 </Passenger>
 <Passenger>
  <passesngerName>DEF</passesngerName>
  <passengerAge>28</passengerAge>
 </Passenger>
 <Passenger>
  <passesngerName>GHI</passesngerName>
  <passengerAge>48</passengerAge>
 </Passenger>
 <Passenger>
  <passesngerName>KLM</passesngerName>
  <passengerAge>18</passengerAge>
 </Passenger>
</PassengersDetails>

My xml object is getting turned into array like below.

Passenger [] passenger= passengersDetails.getPassenger();

And my java object looks like

class Passenger {
passengerName1;
passengerAge1;
passengerName2;
passengerAge2;
passengerName3;
passengerAge3;
passengerName4;
passengerAge4;
}

with setter and getter.

I wanted to set all the data from array in java object. I am able to set it individually after checking if array is preset or not.

if(arr[0] != null){
setPassengerName1 = arr[0].getPassengerName;
setPassengerAge1 = arr[0].getPassengerAge;
}
if(arr[1] != null){
setPassengerName2 = arr[1].getPassengerName;
setPassengerAge2 = arr[1].getPassengerAge;
}....

How can I set passenger array into Passenger java object mentioned above.

But I wanted to know if there is any other way we can do this dynamically.

Something like in the same loop and setting it dynamically.

6
  • 2
    You should reconsider the modelling that lead to the Passenger class to not have 8 distinct fields that just happen to have similar names, but instead to use an array or a list of entries. Commented Feb 7, 2022 at 10:34
  • Hi @RalfKleberhoff , I have forgot to add <Passenger> tag between them. Thanks for letting me know. Commented Feb 7, 2022 at 10:47
  • 1
    Why then does your Passenger java class not correspond to the <Passenger> XML element? And in the class corresponding to PassengerDetails (the one you confusingly called Passenger), why don't you use a Java Passenger[] array or List<Passenger> list instead of the 8 individual fields? Commented Feb 7, 2022 at 10:55
  • Hi @RalfKleberhoff This is what is am trying to learn and do that if i have array elements coming from xml request how can i add all the details in 1 java object in very efficient way. Commented Feb 7, 2022 at 11:00
  • 1
    For a Java learning curve, I'd recommend to postpone XML to a later time, and for the moment concentrate on arrays, lists, for loops etc. And also forget "very efficient way". Think about efficiency only when you find efficiency problems. Code clarity should come first at top priority. Commented Feb 7, 2022 at 11:05

2 Answers 2

2

Create a model class Passenger to hold the name and age only. Then you create an array that holds Passenger objects.

You then use:

getResources().getString(getResources().getIdentifier("propertyName", "string", getPackageName()))

to iterate through string resources, given you know how many passengers there are.

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

1 Comment

Hi, Thanks for answer. But i am bit confused that how can i do that. I didn`t quite understand the solution.
0

But I wanted to know if there is any other way we can do this dynamically as we can get 0 or max 4 passenger elements in array.

Either:

  • Start with one size of array, such as ten. As you place objects into the elements of the array, keep count. As you reach the limits of the array, create a new bigger array. Copy over from old array to new array. Discard old array. Repeat until done.
  • Use a List implementation such as ArrayList.

Using a list.

List< Person > persons = new ArrayList<>() ;

Define a record class to represent each person. You can declare the record as a separate class, a nested class, or as a local class (within a method).

record Person ( String name , int age ) {}

Add each person.

Person person = new Person( "Bob" , 42 ) ;
persons.add( person ) ;

When done, you may want to make an unmodifiable list to replace the modifiable one.

persons = List.of( persons ) ;

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.