Fork me on GitHub

Tutorial 1

E01: Printing some weather data

This example should give you a first impression of the basic XMLBeam features:

  • Accessing XML attributes with automatic type conversion.
  • Sub projections grouping data

XML Content

Using the MSN weather service will get you some XML content similar to this:

<weatherdata>
      <weather
        ... 
        degreetype="F"
        lat="50.5520210266113" lon="6.24060010910034" 
        searchlocation="Monschau, Stadt Aachen, NW, Germany" 
                ... >
        <current ... skytext="Clear" temperature="46"/>
      </weather>
</weatherdata>

We see that the XML structure consists of three elements and number of attributes holding the data. If you execute the tutorial example code you will see that there are even more attributes that are shortened out for readability. If you would use a data binding XML library to access this data you would get three Java classes, two of them actually holding interesting parts.

Projection

Using data projection we define a single interface, hiding the XML structure:

public interface WeatherData {
 
    @XBRead("/weatherdata/weather/@searchlocation")   
    String getLocation();
 
    @XBRead("/weatherdata/weather/current/@temperature")
    int getTemperature();
 
    @XBRead("/weatherdata/weather/@degreetype")
    String getDegreeType();
 
    @XBRead("/weatherdata/weather/current/@skytext")
    String getSkytext();
 
    /**
     * This would be our "sub projection". A structure grouping two attribute
     * values in one object.
     */
    interface Coordinates {
        @XBRead("@lon")
        double getLongitude();
 
        @XBRead("@lat")
        double getLatitude();
    }
 
    @XBRead("/weatherdata/weather")
    Coordinates getCoordinates();
}

Hey, what does the inner interface "Location" do there? This declares that we like to group the two attributes holding the coordinates together in one object. It is not only possible to hide elements of the xml structure, but to enrich the structure with pretended objects.

Example Code

Here the code to run the example:

private void printWeatherData(String location) throws IOException {
     
    // The weather service was discontinued. Changed to read from file.
    final String fileURL = "resource://WeatherData.xml";
     
    // We let the projector fetch the data for us
    WeatherData weatherData = new XBProjector().io().url(fileURL).read(WeatherData.class);
     
    // Print some values
    System.out.println("The weather in " + weatherData.getLocation() + ":");
    System.out.println(weatherData.getSkytext());
    System.out.println("Temperature: " + weatherData.getTemperature() + "°"
                                       + weatherData.getDegreeType());
     
    // Access our sub projection
    Coordinates coordinates = weatherData.getCoordinates();
    System.out.println("The place is located at " + coordinates.getLatitude() + ","
                                                  + coordinates.getLongitude());
 }

Notice that reading the data is just one line of code.