Skip to content

Instantly share code, notes, and snippets.

Created April 24, 2013 16:17
Show Gist options
  • Save anonymous/5453407 to your computer and use it in GitHub Desktop.
Save anonymous/5453407 to your computer and use it in GitHub Desktop.
package bikeshop;
import java.io.File;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
public class Employee {
private String name;
private String rodneCislo;
private String oddelenie;
public Employee(String name, String rodneCislo, String oddelenie){
this.name = name;
this.rodneCislo = rodneCislo;
this.oddelenie = oddelenie;
}
public String getOddelenie() {
return oddelenie;
}
public void setOddelenie(String oddelenie) {
this.oddelenie = oddelenie;
}
public void vypis() {
System.out.println("name: " + this.name + " rodnecislo: " + this.rodneCislo + " oddelenie: " + this.oddelenie);
}
//getter and setter
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRodneCislo() {
return rodneCislo;
}
public void setRodneCislo(String rodneCislo) {
this.rodneCislo = rodneCislo;
}
//end of getter and setter
public boolean equals(Employee e) {
if (this.rodneCislo.equals(getRodneCislo()) ) {
return true;
} else {
return false;
}
}
// Object toXml() {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
// }
public void toXML(){
try{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
Element employeee = doc.createElement("employee");
doc.appendChild(employeee);
Element employee = doc.createElement("employee");
employee.setAttribute("rodneCislo", String.valueOf(rodneCislo));
employee.appendChild(employeee);
Element nameE = doc.createElement("meno");
employee.appendChild(nameE);
Text nameEHodnota = doc.createTextNode(this.name);
nameE.appendChild(nameEHodnota);
Element oddelenieE = doc.createElement("oddelenie");
employee.appendChild(oddelenieE);
Text oddelenieEHodnota = doc.createTextNode(this.oddelenie);
oddelenieE.appendChild(oddelenieEHodnota);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("xmldoc.xml"));
t.transform(source, result);
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment