Exporting and Importing Data XML Processing Exporting and Importing Data XML Processing SoftUni Team Technical Trainers Software University http://softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Table of Contents XML Export XML Import * (c) 2008 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
Questions sli.do #db-advanced
XML
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
XML Example person.xml Person Person Name <?xml version="1.0" encoding="UTF-8"> <person> <firstName>Teodor</firstName> </person> Person Name
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
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
XML Prolog (Optional) person.xml Prolog <?xml version="1.0" encoding="UTF-8">
XML Root person.xml Root <?xml version="1.0" encoding="UTF-8">
XML Element person.xml Element <?xml version="1.0" encoding="UTF-8"> <person> <firstName>Teodor</firstName> </person> Element
XML Attribute person.xml Attribute <?xml version="1.0" encoding="UTF-8"> <person firstName ="Teodor"> </person> Attribute
XML Child person.xml Child <?xml version="1.0" encoding="UTF-8"> <address> <country>Bulgaria</country> <city>Stara Zagora</city> </address> </person> Child
XML Structure person.xml Wrapper <?xml version="1.0" encoding="UTF-8"> <person> <phoneNumbers> <phoneNumber> <number>08983248798</number> </phoneNumber> <number>08983243143</number> </phoneNumbers> </person> Wrapper
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>08983248798</number> </phoneNumber> <number>08983243143</number> </phoneNumbers> </person> Element Child Element Element Child Wrapper Element Child Element
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>
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());
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
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
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>
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);
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>
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
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>
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>
Summary XML Export XML Import
XML Processing https://softuni.bg/courses/databases-advanced-hibernate © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
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 – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Free Trainings @ Software University Software University Foundation – softuni.org Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg Software University @ Facebook facebook.com/SoftwareUniversity Software University Forums forum.softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.