Browse Source

Project finished

main
Jesus 2 years ago
parent
commit
fa4d909446
6 changed files with 102 additions and 22 deletions
  1. +23
    -0
      Readme.md
  2. +6
    -0
      people.xml
  3. BIN
      person.bin
  4. +12
    -10
      src/Main.java
  5. +55
    -0
      src/controller/CreateXml.java
  6. +6
    -12
      src/model/Person.java

+ 23
- 0
Readme.md View File

@ -0,0 +1,23 @@
## Título
Actividad 11 - Guardando varios objetos en un archivo .xml
## Uso
Leyendo clase que extienden de Serializable para despues escribir los datos a un fichero xml creando un DOM.
## Resultado
```
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<people>
<person age="32" id="1" name="Manolo"/>
<person age="2" id="2" name="Jorge"/>
<person age="89" id="3" name="Pepe"/>
</people>
```
## Dependencias
- java.io.*
- org.w3c.dom.*
- javax.xml.*
## Licencia
Uso libre

+ 6
- 0
people.xml View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<people>
<person age="32" id="1" name="Manolo"/>
<person age="2" id="2" name="Jorge"/>
<person age="89" id="3" name="Pepe"/>
</people>

BIN
person.bin View File


+ 12
- 10
src/Main.java View File

@ -1,10 +1,11 @@
import controller.CreateXml;
import controller.ObjReader; import controller.ObjReader;
import controller.ObjWriter; import controller.ObjWriter;
import model.People; import model.People;
import model.Person; import model.Person;
import java.io.File; import java.io.File;
import java.util.ArrayList;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
@ -12,21 +13,22 @@ public class Main {
//Create list //Create list
People data = new People(); People data = new People();
//Init list //Init list
data.add(new Person("Manolo", 32)); data.add(new Person("Manolo", 32));
data.add(new Person("Jorge", 2)); data.add(new Person("Jorge", 2));
data.add(new Person("Pepe", 89)); data.add(new Person("Pepe", 89));
//Check writing
boolean isWritten = ObjWriter.write(data,file);
//Test serialization
People p;
if (isWritten) {
p = (People) ObjReader.read(file);
System.out.println(p.toString());
}
//Write data
ObjWriter.write(data,file);
//Read data
People p = (People) ObjReader.read(file);
System.out.println(p.toString());
ArrayList<Person> peopleList = p.getData();
//Create xml from data
CreateXml c = new CreateXml();
c.create(peopleList, new File("people.xml"));
} }
} }

+ 55
- 0
src/controller/CreateXml.java View File

@ -0,0 +1,55 @@
package controller;
import model.Person;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.util.ArrayList;
public class CreateXml {
public void create(ArrayList<Person> data, File fileOut){
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
//root elements
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("people");
doc.appendChild(rootElement);
for (Person p : data) {
Element person = doc.createElement("person");
rootElement.appendChild(person);
//set attribute to element
person.setAttribute("id", p.getId());
person.setAttribute("name", p.getName());
person.setAttribute("age", p.getAge());
}
//write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(fileOut);
transformer.transform(source, result);
System.out.println("Done!");
}catch(ParserConfigurationException | TransformerException pce){
pce.printStackTrace();
}
}
}

+ 6
- 12
src/model/Person.java View File

@ -9,25 +9,19 @@ public class Person implements Serializable {
private int age; private int age;
public Person(String name, int age) { public Person(String name, int age) {
id = ++CONT;
id = ++CONT ;
this.name = name; this.name = name;
this.age = age; this.age = age;
} }
public String getId() {
return ""+id;
}
public String getName() { public String getName() {
return name; return name;
} }
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
public String getAge() {
return ""+age;
} }
@Override @Override


Loading…
Cancel
Save