Exporting and Importing Data

Slides:



Advertisements
Similar presentations
Processing JSON in.NET JSON, JSON.NET LINQ-to-JSON and JSON to XML SoftUni Team Technical Trainers Software University
Advertisements

Doctrine The PHP ORM SoftUni Team Technical Trainers Software University
Stacks and Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University
XML Processing SoftUni Team Database Applications Technical Trainers
Version Control Systems
Auto Mapping Objects SoftUni Team Database Applications
Static Members and Namespaces
Databases basics Course Introduction SoftUni Team Databases basics
Abstract Classes, Abstract Methods, Override Methods
Interface Segregation / Dependency Inversion
Services & Dependency Injection
Data Structures Course Overview SoftUni Team Data Structures
Introduction to MVC SoftUni Team Introduction to MVC
Deploying Web Application
PHP MVC Frameworks Course Introduction SoftUni Team Technical Trainers
PHP Fundamentals Course Introduction SoftUni Team Technical Trainers
Reflection SoftUni Team Technical Trainers Java OOP Advanced
C# Database Fundamentals with Microsoft SQL Server
Classes, Properties, Constructors, Objects, Namespaces
Mocking tools for easier unit testing
Parsing JSON JSON.NET, LINQ-to-JSON
State Management Cookies, Sessions SoftUni Team State Management
EF Code First (Advanced)
PHP MVC Frameworks MVC Fundamentals SoftUni Team Technical Trainers
C# Databases Advanced with Microsoft SQL Server
Processing Sequences of Elements
EF Relations Object Composition
Entity Framework: Code First
Parsing XML XDocument and LINQ
Basic Tree Data Structures
Data Definition and Data Types
Databases advanced Course Introduction SoftUni Team Databases advanced
C#/Java Web Development Basics
Install and configure theme
Balancing Binary Search Trees, Rotations
Entity Framework: Relations
Fast String Manipulation
Functional Programming
ASP.NET Razor Engine SoftUni Team ASP.NET MVC Introduction
MVC Architecture, Symfony Framework for PHP Web Apps
C# Advanced Course Introduction SoftUni Team C# Technical Trainers
Exporting and Importing Data
Databases Advanced Course Introduction SoftUni Team Databases Advanced
Combining Data Structures
Best practices and architecture
Arrays and Multidimensional Arrays
Built-in Functions. Usage of Wildcards
Multidimensional Arrays, Sets, Dictionaries
Extending functionality using Collections
Exporting and Importing Data
Making big SPA applications
Manual Mapping and AutoMapper Library
Functional Programming
ASP.NET Razor Engine SoftUni Team ASP.NET MVC Introduction
C# Advanced Course Introduction SoftUni Team C# Technical Trainers
Exporting and Importing Data
Introduction to TypeScript & Angular
CSS Transitions and Animations
Train the Trainers Course
Iterators and Comparators
Hibernate (JPA) Code First Entity Relations
Spring Data Advanced Querying
Software Quality Assurance
Directives & Forms Capturing User Input SoftUni Team
Version Control Systems
Polymorphism, Interfaces, Abstract Classes
/^Hel{2}o\s*World\n$/
CSS Transitions and Animations
Iterators and Generators
Multidimensional Arrays
Presentation transcript:

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.