解析 Element 的方法

 private static void parseElement(Element root)
	    {
	     System.out.print("<");
	     System.out.print(root.getNodeName());
	     
	     NamedNodeMap nnm = root.getAttributes();
	     for(int i = 0; i < nnm.getLength(); i++)
	     {
	      Attr attr = (Attr)nnm.item(i);
	      System.out.print(" ");
	      System.out.print(attr.getName());
	      System.out.print("=\"");
	      System.out.print(attr.getValue());
	      System.out.print("\"");
	     }
	     
	     System.out.print(">");
	     
	     NodeList list = root.getChildNodes();
	     for(int i = 0; i < list.getLength(); i++)
	     {
	      Node node = list.item(i);
	      if(node instanceof Element)
	      {
	       Element e = (Element)node;
	       parseElement(e);
	      }
	      else if(node instanceof Text)
	      {
	       Text t = (Text)node;
	       System.out.print(t.getNodeValue());
	      }
	     }
	     
	     System.out.print("</");
	     System.out.print(root.getNodeName());
	     System.out.print(">");
	    }

 

你可能感兴趣的:(element)