Converting XML File to another XML File using the XSLT Conversion Method
- Changing the of input.xml to a different
| package com.company; | |
| import javax.xml.bind.annotation.XmlElement; | |
| import javax.xml.bind.annotation.XmlRootElement; | |
| @XmlRootElement(name="airline") | |
| public class Airline { | |
| private String airlineName; | |
| private int seatsAvailable; | |
| private String fromDestination; | |
| private String toDestination; | |
| private String date; | |
| private int availSeats; | |
| public Airline() { | |
| super(); | |
| } | |
| public Airline(String airlineName, int seatsAvailable, String fromDestination, String toDestination, String date, | |
| int availSeats) { | |
| super(); | |
| this.airlineName = airlineName; | |
| this.seatsAvailable = seatsAvailable; | |
| this.fromDestination = fromDestination; | |
| this.toDestination = toDestination; | |
| this.date = date; | |
| this.availSeats = availSeats; | |
| } | |
| @XmlElement | |
| public String getAirlineName() { | |
| return airlineName; | |
| } | |
| public void setAirlineName(String airlineName) { | |
| this.airlineName = airlineName; | |
| } | |
| @XmlElement | |
| public int getSeatsAvailable() { | |
| return seatsAvailable; | |
| } | |
| public void setSeatsAvailable(int seatsAvailable) { | |
| this.seatsAvailable = seatsAvailable; | |
| } | |
| @XmlElement | |
| public String getFromDestination() { | |
| return fromDestination; | |
| } | |
| public void setFromDestination(String fromDestination) { | |
| this.fromDestination = fromDestination; | |
| } | |
| @XmlElement | |
| public String getToDestination() { | |
| return toDestination; | |
| } | |
| public void setToDestination(String toDestination) { | |
| this.toDestination = toDestination; | |
| } | |
| @XmlElement | |
| public String getDate() { | |
| return date; | |
| } | |
| public void setDate(String date) { | |
| this.date = date; | |
| } | |
| @XmlElement | |
| public int getAvailSeats() { | |
| return availSeats; | |
| } | |
| public void setAvailSeats(int availSeats) { | |
| this.availSeats = availSeats; | |
| } | |
| } |
| package com.company; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import javax.xml.bind.annotation.XmlElement; | |
| import javax.xml.bind.annotation.XmlRootElement; | |
| @XmlRootElement(name="airlines") | |
| public class AirlineList { | |
| private List<Airline> listAirline = new ArrayList<Airline>(); | |
| @XmlElement(name ="airline") | |
| public List<Airline> getListAirline() { | |
| return listAirline; | |
| } | |
| public void setListAirline(List<Airline> listAirline) { | |
| this.listAirline = listAirline; | |
| } | |
| } |
| <?xml version="1.0" encoding="UTF-8" standalone="yes"?> | |
| <airlines> | |
| <airline> | |
| <airlineName>Oman Air</airlineName> | |
| <availSeats>34</availSeats> | |
| <date>12-08-25</date> | |
| <fromDestination>DXB</fromDestination> | |
| <seatsAvailable>50</seatsAvailable> | |
| <toDestination>SYD</toDestination> | |
| </airline> | |
| <airline> | |
| <airlineName>Singapore Airlines</airlineName> | |
| <availSeats>34</availSeats> | |
| <date>02-12-22</date> | |
| <fromDestination>DXB</fromDestination> | |
| <seatsAvailable>35</seatsAvailable> | |
| <toDestination>IND</toDestination> | |
| </airline> | |
| <airline> | |
| <airlineName>Egypt Air</airlineName> | |
| <availSeats>34</availSeats> | |
| <date>12-08-25</date> | |
| <fromDestination>AUH</fromDestination> | |
| <seatsAvailable>33</seatsAvailable> | |
| <toDestination>AE</toDestination> | |
| </airline> | |
| <airline> | |
| <airlineName>Emriates</airlineName> | |
| <availSeats>34</availSeats> | |
| <date>12-08-25</date> | |
| <fromDestination>AVR</fromDestination> | |
| <seatsAvailable>45</seatsAvailable> | |
| <toDestination>PAK</toDestination> | |
| </airline> | |
| </airlines> |
| package com.company; | |
| import com.company.Airline; | |
| import com.company.AirlineList; | |
| import javax.xml.bind.JAXBContext; | |
| import javax.xml.bind.JAXBException; | |
| import javax.xml.bind.Marshaller; | |
| import javax.xml.soap.Node; | |
| import javax.xml.transform.*; | |
| import javax.xml.transform.stream.StreamResult; | |
| import javax.xml.transform.stream.StreamSource; | |
| import java.io.File; | |
| import java.io.IOException; | |
| import java.net.URISyntaxException; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| public class Main { | |
| public static void ConvertFile() throws IOException, URISyntaxException, TransformerException { | |
| Source xslt = new StreamSource(new File("transformAirline.xslt")); | |
| Source text = new StreamSource(new File("Airline-First.xml")); | |
| TransformerFactory factory = TransformerFactory.newInstance(); | |
| Transformer transformer = factory.newTransformer(xslt); | |
| transformer.transform(text, new StreamResult(new File("Airline-Second.xml"))); | |
| System.out.println("Done XSLT"); | |
| } | |
| public static void convertToMarshall( ) throws JAXBException { | |
| List<Airline> listAirline = new ArrayList<Airline>(); | |
| JAXBContext jc = JAXBContext.newInstance(AirlineList.class); | |
| Marshaller ms = jc.createMarshaller(); | |
| try { | |
| listAirline.add(new Airline("Oman Air", 50, "DXB", "SYD", "12-08-25", 34)); | |
| listAirline.add(new Airline("Singapore Airlines", 35, "DXB", "IND", "02-12-22", 34)); | |
| listAirline.add(new Airline("Egypt Air", 33, "AUH", "AE", "12-08-25", 34)); | |
| listAirline.add(new Airline("Emriates", 45, "AVR", "PAK", "12-08-25", 34)); | |
| AirlineList al = new AirlineList(); | |
| al.setListAirline(listAirline); | |
| ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); | |
| ms.marshal(al, System.out); | |
| ms.marshal(al, new File("Airline-First.xml")); | |
| System.out.println("Done Marshall"); | |
| }catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| public static void main(String[] args) throws JAXBException { | |
| // TODO Auto-generated method stub | |
| System.out.println("Begin"); | |
| convertToMarshall(); // Create the XML File | |
| try { | |
| ConvertFile(); | |
| } catch (IOException e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| } catch (URISyntaxException e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| } catch (TransformerException e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| } | |
| } | |
| } |
| <xsl:stylesheet version="1.0" | |
| xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> | |
| <xsl:template match="/"> | |
| <html> | |
| <body> | |
| <h2>Airline Flights</h2> | |
| <table border="1"> | |
| <xsl:for-each select="airlines/airline"> | |
| <flights> | |
| <fName><xsl:value-of select="airlineName"/></fName> | |
| <favailSeats><xsl:value-of select="availSeats"/></favailSeats> | |
| <fdate><xsl:value-of select="date"/></fdate> | |
| <FlightfromDestination><xsl:value-of select="fromDestination"/></FlightfromDestination> | |
| <FlightseatsAvailable><xsl:value-of select="seatsAvailable"/></FlightseatsAvailable> | |
| <FlightToDestination><xsl:value-of select="toDestination"/></FlightToDestination> | |
| </flights> | |
| </xsl:for-each> | |
| </table> | |
| </body> | |
| </html> | |
| </xsl:template> | |
| </xsl:stylesheet> |