-
Notifications
You must be signed in to change notification settings - Fork 0
Parsing an XML File using an Array
Parsing an XML file using an array is a bit more tricky than the previous two examples. In order to create an array, you must know the various elements/attributes of the data within the XML file. The arrays store information pertaining to their respective elements with the attributes associated with them.
Here is the use case example for parsing an XML file using an array. In this example, we create a States class that stores a State object as an arraylist. Each State object stores information for one individual state, and all of its various attributes. The XML file can be found here.
/**
* This class serves as the ArrayList for the 50 states, which will grab each element from the array and turn
* each state into an object.
*/
@Root
class States{
@ElementList(inline = true)
private ArrayList<State> states;
public Object[] getStates(){return states.toArray();}
}//End of States Class/**
* This class serves to be the actual object for the state, grabbing each element from the states.
*/
@Root
class State{
//We grab the state element
@Element
private String state;
//We grab the slug element
@Element
private String slug;
//We grab the code element
@Element
private String code;
//We grab the nickname element
@Element
private String nickname;
//We grab the website element
@Element
private String website;
//We grab the admission_date element
@Element
private String admission_date;
//We grab the admission_number element
@Element
private int admission_number;
//We grab the capital_city element
@Element
private String capital_city;
//We grab the capital_url element
@Element
private String capital_url;
//We grab the population element
@Element
private int population;
//We grab the population_rank element
@Element
private int population_rank;
//We grab the constitution_url element
@Element
private String constitution_url;
//We grab the state_flag_url element
@Element
private String state_flag_url;
//We grab the state_seal_url element
@Element
private String state_seal_url;
//We grab the map_image_url element
@Element
private String map_image_url;
//We grab the landscape_background element
@Element
private String landscape_background_url;
//We grab the skyline_background element
@Element
private String skyline_background_url;
//We grab the twitter_url element
@Element(required = false)
private String twitter_url;
//We grab the facebook_url element
@Element(required = false)
private String facebook_url; public static void main(String[] args){
try{
//We start off by creating the Persister.
Serializer serializer = new Persister();
//We then grab from the source "states.xml"
File file = new File("states.xml");
//We then create a States object, and use the serializer to read from the file, which will then
//create the array of states and put the elements into that array.
States states = serializer.read(States.class, file);
System.out.println(Arrays.toString(states.getStates()));
}catch(Exception e){
//In case we are unable to grab from the XML file, we have to throw an exception
System.out.println("Exception: " + e.getMessage());
System.out.println("Cause: " + e.getCause());
System.exit(1);
}
}
}In order to further explain this, I will zoom in on one state from the array. If you look in the XML file, you can find the state of Alabama. Within the state of Alabama, there are various elements within, including its date of admission to the union, its capital city, and its population. One State Object array stores all elements within the Alabama element. The parcer then looks at the next state (Alaska), finds its elements, and stores that in the array. Once the parcer reaches the end of the XML file, it no longer creates variables inside of the array. Once the final state's elements are stored in the array, the main line prints out the array.
<state>
<state>Alabama</state>
<slug>alabama</slug>
<code>AL</code>
<nickname>Yellowhammer State</nickname>
<website>http://www.alabama.gov</website>
<admission_date>1819-12-14</admission_date>
<admission_number>22</admission_number>
<capital_city>Montgomery</capital_city>
<capital_url>http://www.montgomeryal.gov</capital_url>
<population>4833722</population>
<population_rank>23</population_rank>
<constitution_url>http://alisondb.legislature.state.al.us/alison/default.aspx</constitution_url>
<state_flag_url>https://cdn.civil.services/us-states/flags/alabama-large.png</state_flag_url>
<state_seal_url>https://cdn.civil.services/us-states/seals/alabama-large.png</state_seal_url>
<map_image_url>https://cdn.civil.services/us-states/maps/alabama-large.png</map_image_url>
<landscape_background_url>https://cdn.civil.services/us-states/backgrounds/1280x720/landscape/alabama.jpg</landscape_background_url>
<skyline_background_url>https://cdn.civil.services/us-states/backgrounds/1280x720/skyline/alabama.jpg</skyline_background_url>
<twitter_url>https://twitter.com/alabamagov</twitter_url>
<facebook_url>https://www.facebook.com/alabamagov</facebook_url>
</state>