Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exporting and Importing Data

Similar presentations


Presentation on theme: "Exporting and Importing Data"— Presentation transcript:

1 Exporting and Importing Data
XML Exporting and Importing Data Databases Frameworks SoftUni Team Technical Trainers Software University © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

2 Table of Content XML Export XML Import
© Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

3 Questions sli.do #Hibernate

4 XML

5 XML Specifics XML = eXtensible Mark-up Language
XML is a lightweight format that is used for data interchanging XML is easy to read and write XML is language independent XML format is primarily used to transmit data between a server and web application The filename extension is .xml

6 XML Example person.xml Person Person Name
<?xml version="1.0" encoding="UTF-8"> <person> <firstName>Teodor</firstName> </person> Person Name

7 XML Function Client Server person .js person.xml car. js car.xml
JavaScript Java, PHP, C# Client Server XML PersonController.java person .js person.xml CarController.java car. js car.xml

8 XML Structure XML documents are formed as element trees.
An XML tree starts at a root element and branches from the root to child elements. All elements can have sub elements (child elements): person.xml Root <?xml version="1.0" encoding="UTF-8"> <person> <firstName>Teodor</firstName> <address> <country>Bulgaria</country> <city>Stara Zagora</city> </address> </person> Prolog Tag XML Element Child Closing Tag

9 XML Prolog (Optional) person.xml Prolog
<?xml version="1.0" encoding="UTF-8">

10 XML Root person.xml Root <?xml version="1.0" encoding="UTF-8">

11 XML Element person.xml Element
<?xml version="1.0" encoding="UTF-8"> <person> <firstName>Teodor</firstName> </person> Element

12 XML Attribute person.xml Attribute
<?xml version="1.0" encoding="UTF-8"> <person firstName ="Teodor"> </person> Attribute

13 XML Child person.xml Child <?xml version="1.0" encoding="UTF-8">
<address> <country>Bulgaria</country> <city>Stara Zagora</city> </address> </person> Child

14 XML Structure person.xml Wrapper
<?xml version="1.0" encoding="UTF-8"> <person> <phoneNumbers> <phoneNumber> <number> </number> </phoneNumber> <number> </number> </phoneNumbers> </person> Wrapper

15 XML Structure person.xml Prolog Root Element Child Element Element
<?xml version="1.0" encoding="UTF-8"> <person> <firstName>Teodor</firstName> <address> <country>Bulgaria</country> <city>Stara Zagora</city> </address> <phoneNumbers> <phoneNumber> <number> </number> </phoneNumber> <number> </number> </phoneNumbers> </person> Element Child Element Element Child Wrapper Element Child Element

16 JAXB Provide easy to use mechanisms to convert XML to JSON and vice-versa Generate compact and readability XML output pom.xml <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> </dependency>

17 JAXB Initialization JAXBContext objects are responsible for the XML manipulations JAXBContext.newInstance(object.getClass()) creates an instance of JAXBContext object.getClass is the class that we will export/import XMLParser.java this.jaxbContext = JAXBContext.newInstance(object.getClass());

18 JAXB Annotations @XmlRootElement – Root of the XML. It will be the beginning tag. @XmlAccessorType(XmlAccessType.FIELD) – put your annotations on fields only @XmlAttribute – makrs the field as an attribute @XmlElement – makrs the field as an element @XmlElementWrapper(name = “numbers") – wraps the array of phone numbers with tag “numbers” @XmlTransient – the field won’t be exported/imported

19 Export Single Object to XML
AddressDto.java @XmlRootElement(name = "address") @XmlAccessorType(XmlAccessType.FIELD) public class AddressJsonDto implements Serializable { @XmlAttribute(name = "country") private String country; @XmlElement(name = "city") private String city; } XMLParser.java Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); OutputStream outputStream = new FileOutputStream(fileName); BufferedWriter bfw = new BufferedWriter(new OutputStreamWriter(outputStream)); jaxbMarshaller.marshal(object, bfw); Creates XML

20 Export Single Object to XML
AddressDto.java @XmlRootElement(name = "address") @XmlAccessorType(XmlAccessType.FIELD) public class AddressJsonDto implements Serializable { @XmlAttribute(name = "country") private String country; @XmlElement(name = "city") private String city; } address.xml <?xml version="1.0" encoding="UTF-8"?> <address country="Bulgaria"> <city>Sofia</city> </address>

21 Export Multiple Object to XML
AddressesDto.java @XmlRootElement(name = "addresses") @XmlAccessorType(XmlAccessType.FIELD) public class AddressesDto { @XmlElement(name = "address") private List<AddressJsonDto> addressJsonDtos; } XMLParser.java AddressesDto addressJsonDtos = new AddressesDto(); jaxbMarshaller.marshal(addressesDto, bfw);

22 Export Multiple Object to XML
XMLParser.java AddressesDto addressJsonDtos = new AddressesDto(); jaxbMarshaller.marshal(addressesDto, bfw); addresses.json <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <addresses> <address country="Bulgaria"> <city>Sofia</city> </address> <address country="Spain"> <city>Barcelona</city> </addresses>

23 Import Single Object from XML
AddressDto.java @XmlRootElement(name = "address") @XmlAccessorType(XmlAccessType.FIELD) public class AddressJsonDto implements Serializable { @XmlAttribute(name = "country") private String country; @XmlElement(name = "city") private String city; } XMLParser.java JAXBContext jaxbContext = JAXBContext.newInstance(AddressDto.class); InputStream inputStream = getClass().getResourceAsStream("/files/input/xml/address.xml"); BufferedReader bfr = new BufferedReader(new InputStreamReader(inputStream)); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); AddressDto addressDto = (AddressDto) unmarshaller.unmarshal(bfr); Creates Object

24 Import Single Object from XML
AddressDto.java address.xml @XmlRootElement(name = "address") @XmlAccessorType(XmlAccessType.FIELD) public class AddressJsonDto implements Serializable { @XmlAttribute(name = "country") private String country; @XmlElement(name = "city") private String city; } <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <address country="Bulgaria"> <city>Sofia</city> </address>

25 Import Multiple Object to XML
XMLParser.java Object that holds objects JAXBContext jaxbContext = JAXBContext.newInstance(AddressesDto.class); InputStream inputStream = getClass().getResourceAsStream("/files/input/xml/addresses.xml"); BufferedReader bfr = new BufferedReader(new InputStreamReader(inputStream)); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); AddressesDto addressesDto = (AddressesDto) unmarshaller.unmarshal(bfr); addresses.xml <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <addresses> <address country="Bulgaria"> <city>Sofia</city> </address> <address country="Spain"> <city>Barcelona</city> </addresses>

26 Summary XML Export XML Import

27 JDBC https://softuni.bg/courses/
© Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

28 SoftUni Diamond Partners

29 License This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" license Attribution: this work may contain portions from "Databases" course by Telerik Academy under CC-BY-NC-SA license © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

30 Free Trainings @ Software University
Software University Foundation – softuni.org Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg Software Facebook facebook.com/SoftwareUniversity Software YouTube youtube.com/SoftwareUniversity Software University Forums – forum.softuni.bg © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.


Download ppt "Exporting and Importing Data"

Similar presentations


Ads by Google