Commit a341396a authored by Stefan Michel's avatar Stefan Michel

Arbeit an laden durch XML-Parsing

parent 1cdb3ef7
......@@ -31,3 +31,4 @@ Würde src/controller/ löschen
/out/artifacts/StefanHamster_jar/
/out/production/
/.idea/
# Ignore everything in this directory
*
# Except this file
!.gitignore
\ No newline at end of file
......@@ -3,7 +3,12 @@ package controller;
import application.Main;
import application.StringResource;
import model.Terrain;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
......@@ -135,6 +140,93 @@ public class TerrainController {
}
void print(Element element, String indent) {
System.out.print(element.getNodeName());
NamedNodeMap attributes = element.getAttributes();
for (int i = 0; i < attributes.getLength(); ++i) {
Node attribute = attributes.item(i);
System.out.print(attribute.getNodeName());
}
}
void print(Document document) {
print(document.getDocumentElement(), "");
}
//abgewandelt aus https://stackoverflow.com/questions/7392646/parsing-xml-in-java-gettextcontent-and-getnodevalue-only-return-n-t-an
private String getValueByTag(String sTag, Element element) {
NodeList nlList = element.getElementsByTagName(sTag).item(0).getChildNodes();
Node nValue = (Node) nlList.item(0);
return nValue.getNodeValue();
}
public void loadXML() {
String path = "terrains/niceguys.xml";
Terrain terrain = new Terrain();
// aus den folien UE20
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
DocumentBuilder parser = dbf.newDocumentBuilder();
File input = new File(path);
Document document = parser.parse(input);
Element docElement = document.getDocumentElement();
//TODO: setHamsterCorns hinzufuegen
terrain.setName(getValueByTag("name", docElement));
terrain.setHamsterName(getValueByTag("hamstername", docElement));
terrain.setMaxCols(Integer.parseInt(getValueByTag("maxcols", docElement)));
terrain.setMaxRows(Integer.parseInt(getValueByTag("maxrows", docElement)));
terrain.setHamsterPostion(Integer.parseInt(getValueByTag("hamstercol", docElement)), Integer.parseInt(getValueByTag("hamsterrow", docElement)));
terrain.setHamsterDirection(Integer.parseInt(getValueByTag("hamstercorns", docElement)));
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// try (FileInputStream fileInputStream = new FileInputStream("terrains/best.xml")) {
// Terrain terrain = new Terrain();
//
//
//
// TODO: kein plan??
// XMLInputFactory factory = XMLInputFactory.newInstance();
// XMLStreamReader parser = factory.createXMLStreamReader(fileInputStream);
// while (parser.hasNext()) {
// switch (parser.getEventType()) {
// case XMLStreamConstants.START_ELEMENT:
// System.out.println("START_ELEMENT: " + parser.getLocalName());
//
// for (int i = 0; i < parser.getAttributeCount(); i++)
// System.out.println(" Attribut: "
// + parser.getAttributeLocalName(i)
// + " Wert: " + parser.getAttributeValue(i));
// break;
// case XMLStreamConstants.CHARACTERS:
// System.out.println(" CHARACTERS: " + parser.getText());
// break;
// }
// parser.next();
// }
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// } catch (XMLStreamException e) {
// e.printStackTrace();
// }
}
public boolean deserializeTerrain(String name) {
if (name == null)
return false;
......
......@@ -282,4 +282,8 @@ public class Terrain extends Observable implements Serializable {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
......@@ -240,6 +240,7 @@ public class MainViewCreationFX extends VBox implements Observer {
laden.getItems().addAll(xmlladen, jaxbladen, seriladen);
seriladen.setOnAction(event -> openTerrainFileChooser());
xmlladen.setOnAction(event -> terrainController.loadXML());
Menu bildspeichern = new Menu("_Als Bild speichern");
MenuItem jpg = new MenuItem("_JPEG");
......
# Ignore everything in this directory
*
# Except this file
!.gitignore
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment