Projekt
commit
e09eabe9b4
|
@ -0,0 +1,10 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
|
@ -0,0 +1 @@
|
|||
/bin/
|
|
@ -0,0 +1,17 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Fakturama</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
|
@ -0,0 +1,2 @@
|
|||
eclipse.preferences.version=1
|
||||
encoding/<project>=UTF-8
|
|
@ -0,0 +1,14 @@
|
|||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=17
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
|
||||
org.eclipse.jdt.core.compiler.release=enabled
|
||||
org.eclipse.jdt.core.compiler.source=17
|
|
@ -0,0 +1,166 @@
|
|||
package Fakturama;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.transform.OutputKeys;
|
||||
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 org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.SAXException;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class Kunde {
|
||||
private String name;
|
||||
private String anschrift;
|
||||
private String email;
|
||||
private String telefonnummer;
|
||||
|
||||
// Konstruktor
|
||||
public Kunde(String name, String anschrift, String email, String telefonnummer) {
|
||||
this.name = name;
|
||||
this.anschrift = anschrift;
|
||||
this.email = email;
|
||||
this.telefonnummer = telefonnummer;
|
||||
}
|
||||
|
||||
// Leerer Konstruktor
|
||||
public Kunde() {
|
||||
}
|
||||
|
||||
// Getter und Setter
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getAnschrift() {
|
||||
return anschrift;
|
||||
}
|
||||
|
||||
public void setAnschrift(String anschrift) {
|
||||
this.anschrift = anschrift;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getTelefonnummer() {
|
||||
return telefonnummer;
|
||||
}
|
||||
|
||||
public void setTelefonnummer(String telefonnummer) {
|
||||
this.telefonnummer = telefonnummer;
|
||||
}
|
||||
|
||||
// Methode zum Anzeigen der Kundendaten
|
||||
public void anzeigen() {
|
||||
System.out.println("Kunde: " + name);
|
||||
System.out.println("Anschrift: " + anschrift);
|
||||
System.out.println("Email: " + email);
|
||||
System.out.println("Telefonnummer: " + telefonnummer);
|
||||
}
|
||||
|
||||
// Methode zum Speichern der Kundendaten im XML-Format
|
||||
public void speichereAlsXML(String dateiPfad) {
|
||||
try {
|
||||
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
|
||||
Document doc = dBuilder.newDocument();
|
||||
|
||||
// Root-Element
|
||||
Element rootElement = doc.createElement("Kunde");
|
||||
doc.appendChild(rootElement);
|
||||
|
||||
// Name-Element
|
||||
Element nameElement = doc.createElement("Name");
|
||||
nameElement.appendChild(doc.createTextNode(name));
|
||||
rootElement.appendChild(nameElement);
|
||||
|
||||
// Anschrift-Element
|
||||
Element anschriftElement = doc.createElement("Anschrift");
|
||||
anschriftElement.appendChild(doc.createTextNode(anschrift));
|
||||
rootElement.appendChild(anschriftElement);
|
||||
|
||||
// Email-Element
|
||||
Element emailElement = doc.createElement("Email");
|
||||
emailElement.appendChild(doc.createTextNode(email));
|
||||
rootElement.appendChild(emailElement);
|
||||
|
||||
// Telefonnummer-Element
|
||||
Element telefonnummerElement = doc.createElement("Telefonnummer");
|
||||
telefonnummerElement.appendChild(doc.createTextNode(telefonnummer));
|
||||
rootElement.appendChild(telefonnummerElement);
|
||||
|
||||
// Transformieren und Speichern in Datei
|
||||
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
||||
Transformer transformer = transformerFactory.newTransformer();
|
||||
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
DOMSource source = new DOMSource(doc);
|
||||
StreamResult result = new StreamResult(new File(dateiPfad));
|
||||
transformer.transform(source, result);
|
||||
|
||||
System.out.println("Die XML-Datei wurde erfolgreich gespeichert.");
|
||||
|
||||
} catch (ParserConfigurationException | TransformerException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// Methode zum Auslesen der Kundendaten aus einer XML-Datei
|
||||
public void leseAusXML(String dateiPfad) {
|
||||
try {
|
||||
File xmlDatei = new File(dateiPfad);
|
||||
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
|
||||
Document doc = dBuilder.parse(xmlDatei);
|
||||
doc.getDocumentElement().normalize();
|
||||
|
||||
NodeList nList = doc.getElementsByTagName("Kunde");
|
||||
|
||||
for (int temp = 0; temp < nList.getLength(); temp++) {
|
||||
Node nNode = nList.item(temp);
|
||||
|
||||
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element eElement = (Element) nNode;
|
||||
|
||||
this.name = eElement.getElementsByTagName("Name").item(0).getTextContent();
|
||||
this.anschrift = eElement.getElementsByTagName("Anschrift").item(0).getTextContent();
|
||||
this.email = eElement.getElementsByTagName("Email").item(0).getTextContent();
|
||||
this.telefonnummer = eElement.getElementsByTagName("Telefonnummer").item(0).getTextContent();
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("Die XML-Datei wurde erfolgreich gelesen.");
|
||||
} catch (ParserConfigurationException | SAXException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// Methode zum Auslesen der Kundendaten aus einer XML-Datei und in der Konsole ausgeben
|
||||
public void leseUndGibAusXML(String dateiPfad) {
|
||||
leseAusXML(dateiPfad);
|
||||
anzeigen();
|
||||
}
|
||||
|
||||
// Hauptprogramm zum Testen der Klasse
|
||||
public static void main(String[] args) {
|
||||
Kunde kunde = new Kunde("Max Mustermann", "Musterstraße 5555, 12345 Musterstadt", "max@mustermann.de", "0123456789");
|
||||
kunde.speichereAlsXML("kunde.xml");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package Fakturama;
|
||||
|
||||
public class Test {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Kunde neuerKunde = new Kunde();
|
||||
neuerKunde.leseUndGibAusXML("kunde.xml");
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue