title: Getting Element (not Node) below root in Java DOM (XML parser)
body: <p>I need to get the tag of an element right below the root, but DOM seems only to offer methods getting child nodes (not elements) and you cant cast from one to the other.</p>
  
  <p><a href="http://ideone.com/SUjRmn" rel="nofollow">http://ideone.com/SUjRmn</a></p>
  
  <pre><code>@Override
  public void loadXml(String filepath) throws Exception {
      File f = new File(filepath);
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = null;
      Document doc = null;
      try {
          db = dbf.newDocumentBuilder();
      } catch (ParserConfigurationException e) {
          e.printStackTrace();
      }
      try {
          doc = db.parse(f);
      } catch (SAXException | IOException | NullPointerException e) {
          e.printStackTrace();
      }
      Element root = doc.getDocumentElement();
  
      Node firstChild = root.getFirstChild();
      String tag = firstChild.getNodeName();
  
      //here is the problem. I can't cast from Node to Element and Node
      //stores only an int value, not the name of the object I want to restore
  
      ShapeDrawer drawable = null;
      switch (tag) {
          case "scribble":
              drawable = new ScribbleDrawer();
      ...
  </code></pre>
  
  <p>From the class to restore:</p>
  
  <pre><code>@Override
  public void setValues(Element root) {
      NodeList nodelist = null;
  
      nodelist = root.getElementsByTagName("color");
      colorManager.setColor((nodelist.item(0).getTextContent()));
      this.color = colorManager.getCurrentColor();
      System.out.println(color.toString());
  
      nodelist = root.getElementsByTagName("pressx");
      pressx = Integer.parseInt(nodelist.item(0).getTextContent());
      System.out.println(pressx);
  
      nodelist = root.getElementsByTagName("pressy");
      pressy = Integer.parseInt(nodelist.item(0).getTextContent());
      System.out.println(pressy);
  
      nodelist = root.getElementsByTagName("lastx");
      lastx = Integer.parseInt(nodelist.item(0).getTextContent());
  
      nodelist = root.getElementsByTagName("lasty");
      lasty = Integer.parseInt(nodelist.item(0).getTextContent());
  }
  
  public void toDOM(Document doc, Element root) {
      System.out.println("ScribbleDrawer being saved");
      Element shapeBranch = doc.createElement("scribble");
      Attr attr1 = doc.createAttribute("hashcode");
      attr1.setValue(((Integer) this.hashCode()).toString());
      shapeBranch.setAttributeNode(attr1);
      root.appendChild(shapeBranch);
      Element eColor = doc.createElement("color");
      eColor.setTextContent(colorManager.namedColorToString(color));
      shapeBranch.appendChild(eColor);
  
      // creating tree branch
      Element press = doc.createElement("press");
  
      Attr attr2 = doc.createAttribute("pressx");
      attr2.setValue(((Integer) pressy).toString());
      press.setAttributeNode(attr2);
  
      Attr attr3 = doc.createAttribute("pressy");
      attr3.setValue(((Integer) pressy).toString());
      press.setAttributeNode(attr3);
  
      shapeBranch.appendChild(press);
  
      Element last = doc.createElement("last");
  
      Attr attr4 = doc.createAttribute("lastx");
      attr4.setValue(((Integer) lastx).toString());
      last.setAttributeNode(attr4);
  
      Attr attr5 = doc.createAttribute("lasty");
      attr5.setValue(((Integer) lasty).toString());
      last.setAttributeNode(attr5);
  
      shapeBranch.appendChild(last);
  }
  </code></pre>
  
  <p>I know other parsers are easier, but I am almost finished and when it comes to polymorphy JAXB seems to be just as complicated with Option-marshalling etc</p>
  
  <p>EDIT: this is what the xml looks like; instead of "scribble" other tags/polymorphic children are possible which are deserialized from different instance variables (and thus different DOM-trees except for the root)</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;Drawables&gt;
    &lt;scribble hashcode="189680059"&gt;
      &lt;color&gt;Black&lt;/color&gt;
      &lt;press pressx="221" pressy="221"/&gt;
      &lt;last lastx="368" lasty="219"/&gt;
    &lt;/scribble&gt;
    &lt;scribble hashcode="1215837841"&gt;
      &lt;color&gt;Black&lt;/color&gt;
      &lt;press pressx="246" pressy="246"/&gt;
      &lt;last lastx="368" lasty="221"/&gt;
    &lt;/scribble&gt;
  </code></pre>