fork download
  1. @Override
  2. public void loadXml(String filepath) throws Exception {
  3. File f = new File(filepath);
  4. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  5. DocumentBuilder db = null;
  6. Document doc = null;
  7. try {
  8. db = dbf.newDocumentBuilder();
  9. } catch (ParserConfigurationException e) {
  10. e.printStackTrace();
  11. }
  12. try {
  13. doc = db.parse(f);
  14. } catch (SAXException | IOException | NullPointerException e) {
  15. e.printStackTrace();
  16. }
  17. Element root = doc.getDocumentElement();
  18.  
  19. Node firstChild = root.getFirstChild();
  20. String tag = firstChild.getNodeName();
  21. //here is the problem. I can't cast from Node to Element and Node
  22. //stores only an int value, not the name of the object I want to restore
  23.  
  24. ShapeDrawer drawable = null;
  25. switch (tag) {
  26. case "scribble":
  27. drawable = new ScribbleDrawer();
  28. }
  29.  
  30.  
  31.  
  32. //from the class to resore:
  33. @Override
  34. public void setValues(Element root) {
  35. NodeList nodelist = null;
  36.  
  37. nodelist = root.getElementsByTagName("color");
  38. colorManager.setColor((nodelist.item(0).getTextContent()));
  39. this.color = colorManager.getCurrentColor();
  40. System.out.println(color.toString());
  41.  
  42. nodelist = root.getElementsByTagName("pressx");
  43. pressx = Integer.parseInt(nodelist.item(0).getTextContent());
  44. System.out.println(pressx);
  45.  
  46. nodelist = root.getElementsByTagName("pressy");
  47. pressy = Integer.parseInt(nodelist.item(0).getTextContent());
  48. System.out.println(pressy);
  49.  
  50. nodelist = root.getElementsByTagName("lastx");
  51. lastx = Integer.parseInt(nodelist.item(0).getTextContent());
  52.  
  53. nodelist = root.getElementsByTagName("lasty");
  54. lasty = Integer.parseInt(nodelist.item(0).getTextContent());
  55. }
  56.  
  57. public void toDOM(Document doc, Element root) {
  58. System.out.println("ScribbleDrawer being saved");
  59. Element shapeBranch = doc.createElement("scribble");
  60. Attr attr1 = doc.createAttribute("hashcode");
  61. attr1.setValue(((Integer) this.hashCode()).toString());
  62. shapeBranch.setAttributeNode(attr1);
  63. root.appendChild(shapeBranch);
  64. Element eColor = doc.createElement("color");
  65. eColor.setTextContent(colorManager.namedColorToString(color));
  66. shapeBranch.appendChild(eColor);
  67.  
  68. // creating tree branch
  69. Element press = doc.createElement("press");
  70.  
  71. Attr attr2 = doc.createAttribute("pressx");
  72. attr2.setValue(((Integer) pressy).toString());
  73. press.setAttributeNode(attr2);
  74.  
  75. Attr attr3 = doc.createAttribute("pressy");
  76. attr3.setValue(((Integer) pressy).toString());
  77. press.setAttributeNode(attr3);
  78.  
  79. shapeBranch.appendChild(press);
  80.  
  81. Element last = doc.createElement("last");
  82.  
  83. Attr attr4 = doc.createAttribute("lastx");
  84. attr4.setValue(((Integer) lastx).toString());
  85. last.setAttributeNode(attr4);
  86.  
  87. Attr attr5 = doc.createAttribute("lasty");
  88. attr5.setValue(((Integer) lasty).toString());
  89. last.setAttributeNode(attr5);
  90.  
  91. shapeBranch.appendChild(last);
  92. }
  93. }
  94.  
  95.  
  96.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:2: error: class, interface, or enum expected
	public void loadXml(String filepath) throws Exception {
	       ^
Main.java:4: error: class, interface, or enum expected
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		^
Main.java:5: error: class, interface, or enum expected
		DocumentBuilder db = null;
		^
Main.java:6: error: class, interface, or enum expected
		Document doc = null;
		^
Main.java:7: error: class, interface, or enum expected
		try {
		^
Main.java:9: error: class, interface, or enum expected
		} catch (ParserConfigurationException e) {
		^
Main.java:11: error: class, interface, or enum expected
		}
		^
Main.java:14: error: class, interface, or enum expected
		} catch (SAXException | IOException | NullPointerException e) {
		^
Main.java:16: error: class, interface, or enum expected
		}
		^
Main.java:19: error: class, interface, or enum expected
		Node firstChild = root.getFirstChild();
		^
Main.java:20: error: class, interface, or enum expected
		String tag = firstChild.getNodeName();
		^
Main.java:24: error: class, interface, or enum expected
		ShapeDrawer drawable = null;
		^
Main.java:25: error: class, interface, or enum expected
		switch (tag) {
		^
Main.java:28: error: class, interface, or enum expected
	}
	^
Main.java:34: error: class, interface, or enum expected
	public void setValues(Element root) {
	       ^
Main.java:37: error: class, interface, or enum expected
		nodelist = root.getElementsByTagName("color");
		^
Main.java:38: error: class, interface, or enum expected
		colorManager.setColor((nodelist.item(0).getTextContent()));
		^
Main.java:39: error: class, interface, or enum expected
		this.color = colorManager.getCurrentColor();
		^
Main.java:40: error: class, interface, or enum expected
		System.out.println(color.toString());
		^
Main.java:42: error: class, interface, or enum expected
		nodelist = root.getElementsByTagName("pressx");
		^
Main.java:43: error: class, interface, or enum expected
		pressx = Integer.parseInt(nodelist.item(0).getTextContent());
		^
Main.java:44: error: class, interface, or enum expected
		System.out.println(pressx);
		^
Main.java:46: error: class, interface, or enum expected
		nodelist = root.getElementsByTagName("pressy");
		^
Main.java:47: error: class, interface, or enum expected
		pressy = Integer.parseInt(nodelist.item(0).getTextContent());
		^
Main.java:48: error: class, interface, or enum expected
		System.out.println(pressy);
		^
Main.java:50: error: class, interface, or enum expected
		nodelist = root.getElementsByTagName("lastx");
		^
Main.java:51: error: class, interface, or enum expected
		lastx = Integer.parseInt(nodelist.item(0).getTextContent());
		^
Main.java:53: error: class, interface, or enum expected
		nodelist = root.getElementsByTagName("lasty");
		^
Main.java:54: error: class, interface, or enum expected
		lasty = Integer.parseInt(nodelist.item(0).getTextContent());
		^
Main.java:55: error: class, interface, or enum expected
	}
	^
Main.java:57: error: class, interface, or enum expected
	public void toDOM(Document doc, Element root) {
	       ^
Main.java:59: error: class, interface, or enum expected
		Element shapeBranch = doc.createElement("scribble");
		^
Main.java:60: error: class, interface, or enum expected
		Attr attr1 = doc.createAttribute("hashcode");
		^
Main.java:61: error: class, interface, or enum expected
		attr1.setValue(((Integer) this.hashCode()).toString());
		^
Main.java:62: error: class, interface, or enum expected
		shapeBranch.setAttributeNode(attr1);
		^
Main.java:63: error: class, interface, or enum expected
		root.appendChild(shapeBranch);
		^
Main.java:64: error: class, interface, or enum expected
		Element eColor = doc.createElement("color");
		^
Main.java:65: error: class, interface, or enum expected
		eColor.setTextContent(colorManager.namedColorToString(color));
		^
Main.java:66: error: class, interface, or enum expected
		shapeBranch.appendChild(eColor);
		^
Main.java:69: error: class, interface, or enum expected
		Element press = doc.createElement("press");
		^
Main.java:71: error: class, interface, or enum expected
		Attr attr2 = doc.createAttribute("pressx");
		^
Main.java:72: error: class, interface, or enum expected
		attr2.setValue(((Integer) pressy).toString());
		^
Main.java:73: error: class, interface, or enum expected
		press.setAttributeNode(attr2);
		^
Main.java:75: error: class, interface, or enum expected
		Attr attr3 = doc.createAttribute("pressy");
		^
Main.java:76: error: class, interface, or enum expected
		attr3.setValue(((Integer) pressy).toString());
		^
Main.java:77: error: class, interface, or enum expected
		press.setAttributeNode(attr3);
		^
Main.java:79: error: class, interface, or enum expected
		shapeBranch.appendChild(press);
		^
Main.java:81: error: class, interface, or enum expected
		Element last = doc.createElement("last");
		^
Main.java:83: error: class, interface, or enum expected
		Attr attr4 = doc.createAttribute("lastx");
		^
Main.java:84: error: class, interface, or enum expected
		attr4.setValue(((Integer) lastx).toString());
		^
Main.java:85: error: class, interface, or enum expected
		last.setAttributeNode(attr4);
		^
Main.java:87: error: class, interface, or enum expected
		Attr attr5 = doc.createAttribute("lasty");
		^
Main.java:88: error: class, interface, or enum expected
		attr5.setValue(((Integer) lasty).toString());
		^
Main.java:89: error: class, interface, or enum expected
		last.setAttributeNode(attr5);
		^
Main.java:91: error: class, interface, or enum expected
		shapeBranch.appendChild(last);
		^
Main.java:92: error: class, interface, or enum expected
	}
	^
56 errors
stdout
Standard output is empty