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 Vimal Jaiswal
Как сгенерировать этот файл? 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 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; public int getId() { return id; } public void setId(int id) { this.id = id; public String getName() { return name; } public void setName(String name) { this.name = name; public float getSalary() { return salary; } public void setSalary(float salary) { this.salary = salary; } }
Рассмотрим еще пример XML-файла: 1 That Darn Gray Cat 5 lost 2 Fort Utopia 10 win
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; } public String getDid_win() { return did_win; } }
name ="horseList" ) public class HorseList name = "horse", type =Horse.class ) private List horseList =new ArrayList (); public HorseList(){} public HorseList(List horseList){ this.horseList = horseList; } public List getHorseList() { return horseList; } public void setHorseList(List horseList) { this.horseList = horseList; } // Export public static void marshal(List 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 unmarshal(File importFile) throws JAXBException { HorseList ids = new HorseList(); JAXBContext context = JAXBContext.newInstance(HorseList.class); Unmarshaller um = context.createUnmarshaller(); ids = (HorseList) um.unmarshal(importFile); return ids.getHorseList(); } }
УСПЕХОВ!