SlideShare a Scribd company logo
1 of 11
WORK WITH XML-files in
Java
Для студентов
старших курсов университетов
Ст.преподаватель Дудник О.А.
• XML-eXtensible Markup
Language(расширяемыXй язык
разметок)
• XML-файлы можно использовать в
.качестве базы данных
• Для чтения и записи в такие файлы в
Java используется библиотека JAXB.
• Для ее подключения нужно дописать
• import
javax.xml.bind.annotation.XmlAccessType;
import
javax.xml.bind.annotation.XmlAccessorType;
import
javax.xml.bind.annotation.XmlRootElement;
• File: employee.xml
• <?xml version="1.0" encoding="UTF-
8" standalone="yes"?>
• <employee id="1">
• <name>Vimal Jaiswal</name>
• <salary>50000.0</salary>
• </employee>
• Как сгенерировать этот файл?
• File: ObjectToXml.java
• import java.io.FileOutputStream;  
•   
• import javax.xml.bind.JAXBContext;  
• import javax.xml.bind.Marshaller;  
•   
•   
• public class ObjectToXml {  
• public static void main(String[] args) throws Exception{  
•     JAXBContext contextObj = JAXBContext.newInstance(Employee.class);  
•   
•     Marshaller marshallerObj = contextObj.createMarshaller();  
•     marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);  
•   
•     Employee emp1=new Employee(1,"Vimal Jaiswal",50000);  
•       
•     marshallerObj.marshal(emp1, new FileOutputStream("employee.xml"));  
•        
• }  
• }  
• File: Employee.java
• import javax.xml.bind.annotation.XmlAttribute;  
• import javax.xml.bind.annotation.XmlElement;  
• import javax.xml.bind.annotation.XmlRootElement;  
•   
• @XmlRootElement  
• public class Employee {  
•     private int id;  
•     private String name;  
•     private float salary;  
•   
• public Employee() {}  
• public Employee(int id, String name, float salary) {  
•     super();  
•     this.id = id;  
•     this.name = name;  
•     this.salary = salary;  
• }  
• @XmlAttribute  
• public int getId() {  
•     return id;  
• }  
• public void setId(int id) {  
•     this.id = id;  
• }  
• @XmlElement  
• public String getName() {  
•     return name;  
• }  
• public void setName(String name) {  
•     this.name = name;  
• Рассмотрим еще пример XML-файла:
• <?xml version="1.0" encoding="UTF-8"
standalone="yes"?>
<horseList>
<horse>
<horse_number>1</horse_number>
<name_horse>That Darn Gray Cat</name_horse>
<odds>5</odds>
<did_win>lost</did_win>
</horse>
<horse>
<horse_number>2</horse_number>
<name_horse>Fort Utopia</name_horse>
<odds>10</odds>
<did_win>win</did_win>
</horse>
• </horseList>
• @XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement( name ="horse" )
• public class Horse {
public String horse_number;
public String name_horse;
public String odds;
public String did_win;
public void setHorse_number(String horse_number) {
this.horse_number = horse_number;
}
public void setName_horse(String name_horse) {
this.name_horse = name_horse;
}
public void setOdds(String odds) {
this.odds = odds;
}
public void setDid_win(String did_win) {
this.did_win = did_win;
}
public String getHorse_number() {
return horse_number;
}
public String getName_horse() {
return name_horse;
}
public String getOdds() {
return odds;
• @XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement( name ="horseList" )
public class HorseList {
@XmlElement( name = "horse", type =Horse.class )
private List<Horse> horseList =new ArrayList<Horse>();
public HorseList(){}
public HorseList(List<Horse> horseList){
this.horseList = horseList;
}
public List<Horse> getHorseList() {
return horseList;
}
public void setHorseList(List<Horse> horseList) {
this.horseList = horseList;
}
// Export
public static void marshal(List<Horse> ids, File selectedFile)
throws IOException, JAXBException {
JAXBContext context;
BufferedWriter writer = null;
writer = new BufferedWriter(new FileWriter(selectedFile));
context = JAXBContext.newInstance(HorseList.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(new HorseList(ids), writer);
writer.close();
}
// Import
public static List<Horse> unmarshal(File importFile) throws
• УСПЕХОВ!

More Related Content

What's hot

BI, Integration, and Apps on Couchbase using Simba ODBC and JDBC
BI, Integration, and Apps on Couchbase using Simba ODBC and JDBCBI, Integration, and Apps on Couchbase using Simba ODBC and JDBC
BI, Integration, and Apps on Couchbase using Simba ODBC and JDBCSimba Technologies
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1myrajendra
 
Project First presentation about introduction to technologies to be used
Project First presentation about introduction to technologies to be usedProject First presentation about introduction to technologies to be used
Project First presentation about introduction to technologies to be usedarya krazydude
 
Java for XPages Development
Java for XPages DevelopmentJava for XPages Development
Java for XPages DevelopmentTeamstudio
 
itPage LDC 09 Presentation
itPage LDC 09 PresentationitPage LDC 09 Presentation
itPage LDC 09 PresentationEric Landmann
 
Day 2 - Intro to Rails
Day 2 - Intro to RailsDay 2 - Intro to Rails
Day 2 - Intro to RailsBarry Jones
 

What's hot (9)

BI, Integration, and Apps on Couchbase using Simba ODBC and JDBC
BI, Integration, and Apps on Couchbase using Simba ODBC and JDBCBI, Integration, and Apps on Couchbase using Simba ODBC and JDBC
BI, Integration, and Apps on Couchbase using Simba ODBC and JDBC
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1
 
ORM Methodology
ORM MethodologyORM Methodology
ORM Methodology
 
Css
CssCss
Css
 
Project First presentation about introduction to technologies to be used
Project First presentation about introduction to technologies to be usedProject First presentation about introduction to technologies to be used
Project First presentation about introduction to technologies to be used
 
Java for XPages Development
Java for XPages DevelopmentJava for XPages Development
Java for XPages Development
 
Introduce to XML
Introduce to XMLIntroduce to XML
Introduce to XML
 
itPage LDC 09 Presentation
itPage LDC 09 PresentationitPage LDC 09 Presentation
itPage LDC 09 Presentation
 
Day 2 - Intro to Rails
Day 2 - Intro to RailsDay 2 - Intro to Rails
Day 2 - Intro to Rails
 

Viewers also liked

Ivan rodrigo elt 601
Ivan rodrigo   elt 601Ivan rodrigo   elt 601
Ivan rodrigo elt 601Ivan Rodrigo
 
फ़ारसी ट्रांस्लेशन सर्विस
फ़ारसी ट्रांस्लेशन सर्विसफ़ारसी ट्रांस्लेशन सर्विस
फ़ारसी ट्रांस्लेशन सर्विसfarsitranslator
 
microsoft bootstrap
microsoft bootstrapmicrosoft bootstrap
microsoft bootstrapUjjwal Sahay
 
2014 UBS Mitarbeitermagazin startup academy
2014 UBS Mitarbeitermagazin startup academy2014 UBS Mitarbeitermagazin startup academy
2014 UBS Mitarbeitermagazin startup academyFelix Wenger
 
Enfermedades de transmisión sexual
Enfermedades de transmisión sexualEnfermedades de transmisión sexual
Enfermedades de transmisión sexualNayeli Balladares
 
Reporte act 3.15
Reporte act 3.15Reporte act 3.15
Reporte act 3.15mao_aviles
 
Classificação seniores 2015
Classificação seniores 2015Classificação seniores 2015
Classificação seniores 2015grupouniaosport
 
LED paneel - LEDs Inspire
LED paneel - LEDs InspireLED paneel - LEDs Inspire
LED paneel - LEDs InspireLEDs Inspire
 
Trabajo victor y luis
Trabajo victor y luisTrabajo victor y luis
Trabajo victor y luissebas199711
 
Bnw propaganda - Ana T, Ana I, Eduarda, and Iann
Bnw propaganda - Ana T, Ana I, Eduarda, and IannBnw propaganda - Ana T, Ana I, Eduarda, and Iann
Bnw propaganda - Ana T, Ana I, Eduarda, and IannAna Novella
 

Viewers also liked (14)

Ivan rodrigo elt 601
Ivan rodrigo   elt 601Ivan rodrigo   elt 601
Ivan rodrigo elt 601
 
फ़ारसी ट्रांस्लेशन सर्विस
फ़ारसी ट्रांस्लेशन सर्विसफ़ारसी ट्रांस्लेशन सर्विस
फ़ारसी ट्रांस्लेशन सर्विस
 
microsoft bootstrap
microsoft bootstrapmicrosoft bootstrap
microsoft bootstrap
 
2014 UBS Mitarbeitermagazin startup academy
2014 UBS Mitarbeitermagazin startup academy2014 UBS Mitarbeitermagazin startup academy
2014 UBS Mitarbeitermagazin startup academy
 
Cfo kpi
Cfo kpiCfo kpi
Cfo kpi
 
Enfermedades de transmisión sexual
Enfermedades de transmisión sexualEnfermedades de transmisión sexual
Enfermedades de transmisión sexual
 
Reporte act 3.15
Reporte act 3.15Reporte act 3.15
Reporte act 3.15
 
Kelas xii
Kelas xiiKelas xii
Kelas xii
 
Classificação seniores 2015
Classificação seniores 2015Classificação seniores 2015
Classificação seniores 2015
 
LED paneel - LEDs Inspire
LED paneel - LEDs InspireLED paneel - LEDs Inspire
LED paneel - LEDs Inspire
 
Sunu1
Sunu1Sunu1
Sunu1
 
Trabajo victor y luis
Trabajo victor y luisTrabajo victor y luis
Trabajo victor y luis
 
Bnw propaganda - Ana T, Ana I, Eduarda, and Iann
Bnw propaganda - Ana T, Ana I, Eduarda, and IannBnw propaganda - Ana T, Ana I, Eduarda, and Iann
Bnw propaganda - Ana T, Ana I, Eduarda, and Iann
 
Medidas
MedidasMedidas
Medidas
 

Similar to WORK WITH XML FILES IN JAVA

Similar to WORK WITH XML FILES IN JAVA (20)

Hibernate&ejb3 . part3.
Hibernate&ejb3 . part3.Hibernate&ejb3 . part3.
Hibernate&ejb3 . part3.
 
Iasi code camp 12 october 2013 jax-rs-jee-ecosystem - catalin mihalache
Iasi code camp 12 october 2013   jax-rs-jee-ecosystem - catalin mihalacheIasi code camp 12 october 2013   jax-rs-jee-ecosystem - catalin mihalache
Iasi code camp 12 october 2013 jax-rs-jee-ecosystem - catalin mihalache
 
Approaches to document/report generation
Approaches to document/report generation Approaches to document/report generation
Approaches to document/report generation
 
Java architecture for xml binding
Java architecture for xml bindingJava architecture for xml binding
Java architecture for xml binding
 
AJS UNIT-1 2021-converted.pdf
AJS UNIT-1 2021-converted.pdfAJS UNIT-1 2021-converted.pdf
AJS UNIT-1 2021-converted.pdf
 
CRUD Operation of images through XML
CRUD Operation of images through XMLCRUD Operation of images through XML
CRUD Operation of images through XML
 
OpenNTF Webinar 2022-08 - XPages Jakarta EE Support in Practice
OpenNTF Webinar 2022-08 - XPages Jakarta EE Support in PracticeOpenNTF Webinar 2022-08 - XPages Jakarta EE Support in Practice
OpenNTF Webinar 2022-08 - XPages Jakarta EE Support in Practice
 
JAXB
JAXBJAXB
JAXB
 
Java
JavaJava
Java
 
java01.pdf
java01.pdfjava01.pdf
java01.pdf
 
XML parsing using jaxb
XML parsing using jaxbXML parsing using jaxb
XML parsing using jaxb
 
Java Web Services
Java Web ServicesJava Web Services
Java Web Services
 
Solr Application Development Tutorial
Solr Application Development TutorialSolr Application Development Tutorial
Solr Application Development Tutorial
 
Lecture 19 dynamic web - java - part 1
Lecture 19   dynamic web - java - part 1Lecture 19   dynamic web - java - part 1
Lecture 19 dynamic web - java - part 1
 
Java01
Java01Java01
Java01
 
Java intro
Java introJava intro
Java intro
 
Java01
Java01Java01
Java01
 
Java
JavaJava
Java
 
java01.ppt
java01.pptjava01.ppt
java01.ppt
 
java01.ppt
java01.pptjava01.ppt
java01.ppt
 

More from Asya Dudnik

Get started with docker &amp; dev ops
Get started with docker &amp; dev opsGet started with docker &amp; dev ops
Get started with docker &amp; dev opsAsya Dudnik
 
Get started with docker &amp; dev ops
Get started with docker &amp; dev opsGet started with docker &amp; dev ops
Get started with docker &amp; dev opsAsya Dudnik
 
Work with my_sql_-_database_in_java
Work with my_sql_-_database_in_javaWork with my_sql_-_database_in_java
Work with my_sql_-_database_in_javaAsya Dudnik
 
Work with xml in java
Work with xml in javaWork with xml in java
Work with xml in javaAsya Dudnik
 
Java.fundamentals
Java.fundamentalsJava.fundamentals
Java.fundamentalsAsya Dudnik
 
Data bases in pictures
Data bases in picturesData bases in pictures
Data bases in picturesAsya Dudnik
 
использование Hibernate java persistence.part 4.
использование Hibernate java persistence.part 4.использование Hibernate java persistence.part 4.
использование Hibernate java persistence.part 4.Asya Dudnik
 
использование Hibernate java persistence.part 2.
использование Hibernate java persistence.part 2.использование Hibernate java persistence.part 2.
использование Hibernate java persistence.part 2.Asya Dudnik
 
Work with my sql database in java
Work with my sql   database in javaWork with my sql   database in java
Work with my sql database in javaAsya Dudnik
 
Java.fundamentals
Java.fundamentalsJava.fundamentals
Java.fundamentalsAsya Dudnik
 
Java fx for interface
Java fx for interfaceJava fx for interface
Java fx for interfaceAsya Dudnik
 
Java fx for interface
Java fx for interfaceJava fx for interface
Java fx for interfaceAsya Dudnik
 
Apache maven in java projects
Apache maven in java projectsApache maven in java projects
Apache maven in java projectsAsya Dudnik
 

More from Asya Dudnik (20)

Get started with docker &amp; dev ops
Get started with docker &amp; dev opsGet started with docker &amp; dev ops
Get started with docker &amp; dev ops
 
Get started with docker &amp; dev ops
Get started with docker &amp; dev opsGet started with docker &amp; dev ops
Get started with docker &amp; dev ops
 
Work with my_sql_-_database_in_java
Work with my_sql_-_database_in_javaWork with my_sql_-_database_in_java
Work with my_sql_-_database_in_java
 
Oracle database
Oracle databaseOracle database
Oracle database
 
Work with xml in java
Work with xml in javaWork with xml in java
Work with xml in java
 
Jdbc in java
Jdbc in javaJdbc in java
Jdbc in java
 
Java.fundamentals
Java.fundamentalsJava.fundamentals
Java.fundamentals
 
Data bases in pictures
Data bases in picturesData bases in pictures
Data bases in pictures
 
использование Hibernate java persistence.part 4.
использование Hibernate java persistence.part 4.использование Hibernate java persistence.part 4.
использование Hibernate java persistence.part 4.
 
использование Hibernate java persistence.part 2.
использование Hibernate java persistence.part 2.использование Hibernate java persistence.part 2.
использование Hibernate java persistence.part 2.
 
Work with my sql database in java
Work with my sql   database in javaWork with my sql   database in java
Work with my sql database in java
 
Java.fundamentals
Java.fundamentalsJava.fundamentals
Java.fundamentals
 
Web&java. gwt
Web&java. gwtWeb&java. gwt
Web&java. gwt
 
Web&java.jsf.
Web&java.jsf.Web&java.jsf.
Web&java.jsf.
 
Web&java. jsp
Web&java. jspWeb&java. jsp
Web&java. jsp
 
Web&java. jsp
Web&java. jspWeb&java. jsp
Web&java. jsp
 
Java fx for interface
Java fx for interfaceJava fx for interface
Java fx for interface
 
Java fx for interface
Java fx for interfaceJava fx for interface
Java fx for interface
 
Apache maven in java projects
Apache maven in java projectsApache maven in java projects
Apache maven in java projects
 
Threads in java
Threads in javaThreads in java
Threads in java
 

Recently uploaded

IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Effects of rheological properties on mixing
Effects of rheological properties on mixingEffects of rheological properties on mixing
Effects of rheological properties on mixingviprabot1
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
EduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIEduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIkoyaldeepu123
 

Recently uploaded (20)

IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Effects of rheological properties on mixing
Effects of rheological properties on mixingEffects of rheological properties on mixing
Effects of rheological properties on mixing
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
EduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIEduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AI
 

WORK WITH XML FILES IN JAVA

  • 1. WORK WITH XML-files in Java Для студентов старших курсов университетов Ст.преподаватель Дудник О.А.
  • 3. • XML-файлы можно использовать в .качестве базы данных • Для чтения и записи в такие файлы в Java используется библиотека JAXB. • Для ее подключения нужно дописать • import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement;
  • 4. • File: employee.xml • <?xml version="1.0" encoding="UTF- 8" standalone="yes"?> • <employee id="1"> • <name>Vimal Jaiswal</name> • <salary>50000.0</salary> • </employee>
  • 5. • Как сгенерировать этот файл? • File: ObjectToXml.java • import java.io.FileOutputStream;   •    • import javax.xml.bind.JAXBContext;   • import javax.xml.bind.Marshaller;   •    •    • public class ObjectToXml {   • public static void main(String[] args) throws Exception{   •     JAXBContext contextObj = JAXBContext.newInstance(Employee.class);   •    •     Marshaller marshallerObj = contextObj.createMarshaller();   •     marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);   •    •     Employee emp1=new Employee(1,"Vimal Jaiswal",50000);   •        •     marshallerObj.marshal(emp1, new FileOutputStream("employee.xml"));   •         • }   • }  
  • 6. • File: Employee.java • import javax.xml.bind.annotation.XmlAttribute;   • import javax.xml.bind.annotation.XmlElement;   • import javax.xml.bind.annotation.XmlRootElement;   •    • @XmlRootElement   • public class Employee {   •     private int id;   •     private String name;   •     private float salary;   •    • public Employee() {}   • public Employee(int id, String name, float salary) {   •     super();   •     this.id = id;   •     this.name = name;   •     this.salary = salary;   • }   • @XmlAttribute   • public int getId() {   •     return id;   • }   • public void setId(int id) {   •     this.id = id;   • }   • @XmlElement   • public String getName() {   •     return name;   • }   • public void setName(String name) {   •     this.name = name;  
  • 7. • Рассмотрим еще пример XML-файла: • <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <horseList> <horse> <horse_number>1</horse_number> <name_horse>That Darn Gray Cat</name_horse> <odds>5</odds> <did_win>lost</did_win> </horse> <horse> <horse_number>2</horse_number> <name_horse>Fort Utopia</name_horse> <odds>10</odds> <did_win>win</did_win> </horse> • </horseList>
  • 8. • @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement( name ="horse" ) • public class Horse { public String horse_number; public String name_horse; public String odds; public String did_win; public void setHorse_number(String horse_number) { this.horse_number = horse_number; } public void setName_horse(String name_horse) { this.name_horse = name_horse; } public void setOdds(String odds) { this.odds = odds; } public void setDid_win(String did_win) { this.did_win = did_win; } public String getHorse_number() { return horse_number; } public String getName_horse() { return name_horse; } public String getOdds() { return odds;
  • 9. • @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement( name ="horseList" ) public class HorseList { @XmlElement( name = "horse", type =Horse.class ) private List<Horse> horseList =new ArrayList<Horse>(); public HorseList(){} public HorseList(List<Horse> horseList){ this.horseList = horseList; } public List<Horse> getHorseList() { return horseList; } public void setHorseList(List<Horse> horseList) { this.horseList = horseList; } // Export public static void marshal(List<Horse> ids, File selectedFile) throws IOException, JAXBException { JAXBContext context; BufferedWriter writer = null; writer = new BufferedWriter(new FileWriter(selectedFile)); context = JAXBContext.newInstance(HorseList.class); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.marshal(new HorseList(ids), writer); writer.close(); } // Import public static List<Horse> unmarshal(File importFile) throws
  • 10.