Created
April 24, 2013 13:22
-
-
Save anonymous/5452083 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
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 BicykliX { | |
private String name; | |
private String id; | |
private String gear; | |
public BicykliX(String name, String id, String gear) { | |
this.name = name; | |
this.id = id; | |
this.gear = gear; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getId() { | |
return id; | |
} | |
public void setId(String id) { | |
this.id = id; | |
} | |
public String getGear() { | |
return gear; | |
} | |
public void setGear(String gear) { | |
this.gear = gear; | |
} | |
//.......................................................................... | |
public void vypis(){ | |
System.out.println("Meno:" + this.name); | |
System.out.println("Id:" + this.id); | |
System.out.println("Gear:" + this.gear); | |
} | |
public void makeXML(){ | |
try{ | |
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | |
DocumentBuilder db = dbf.newDocumentBuilder(); | |
Document doc = db.newDocument(); | |
Element bikeshop = doc.createElement("bikeshop"); | |
doc.appendChild(bikeshop); | |
Element bike = doc.createElement("bike"); | |
bike.setAttribute("id", String.valueOf(id)); | |
bikeshop.appendChild(bike); | |
Element nameBike = doc.createElement("meno"); | |
bike.appendChild(nameBike); | |
Text nameBikeHodnota = doc.createTextNode(this.name); | |
nameBike.appendChild(nameBikeHodnota); | |
Element gearBike = doc.createElement("gear"); | |
bike.appendChild(gearBike); | |
Text gearBikeHodnota = doc.createTextNode(this.gear); | |
gearBike.appendChild(gearBikeHodnota); | |
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