package com.bitrazor.test; import java.net.MalformedURLException; import java.net.URL; import java.util.Iterator; import java.util.List; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import com.bitrazor.jaxb.regionmaster.Region; import com.bitrazor.jaxb.regionmaster.RegionDescriptionFileURLs; import com.bitrazor.jaxb.regionmaster.Regions; public class Main { public static final String masterURL = "http://bitrazor.com/content/tivo/hme/trafficcam/RegionMaster.xml"; public static final String jaxbPackage = "com.bitrazor.jaxb.regionmaster"; public static void main(String[] args) { JAXBContext jc = Main.setContext(jaxbPackage); Unmarshaller u = Main.getUnmarshaller(jc); Regions regions = Main.getRegions(masterURL, u); // regionList get assigned to the first region in regions List regionList = regions.getRegion(); // For each region for (Iterator iter = regionList.iterator(); iter.hasNext();) { toString((Region) iter.next()); } } // Display contents for a region to the console. This should be called // for each region in the RegionMaster file. public static void toString(Region region) { System.out.println("Region found:"); System.out.println(" ID: " + region.getRegionID() + ", Name: " + region.getRegionName()); RegionDescriptionFileURLs rdfurls = (RegionDescriptionFileURLs) region .getRegionDescriptionFileURLs(); List rdfUrlList = rdfurls.getRegionDescriptionFileURL(); // Print out each RDF URL for (Iterator rdfurl = rdfUrlList.iterator(); rdfurl.hasNext();) { System.out.println(" URL: " + rdfurl.next()); } } public static JAXBContext setContext(String jaxbPackage) { JAXBContext jc = null; try { // Set the JAXB context to the package name we uses // when we created all the JAXB classes from the XSD jc = JAXBContext.newInstance(jaxbPackage); } catch (JAXBException e) { e.printStackTrace(); } return jc; } public static Unmarshaller getUnmarshaller(JAXBContext jc) { Unmarshaller u = null; try { // Create an unmarshaller (unmarshal from XML to Java objects) u = jc.createUnmarshaller(); } catch (JAXBException e) { e.printStackTrace(); } return u; } public static Regions getRegions(String masterURL, Unmarshaller u) { Regions regions = null; try { URL url = new URL(masterURL); // Use the following line to read from a file instead of a URL: // regions = (Regions)u.unmarshal(new // FileInputStream("RegionMaster.xml")); regions = (Regions) u.unmarshal(url); } catch (JAXBException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } return regions; } }