//--------------------------------------
//DOMBsp.java
//1.0
//--------------------------------------

import org.w3c.dom.*;     // 1
import javax.xml.parsers.*;

public class DOMBsp       // 2
{
  Document doc;           // 3
  
  // ---- main ----
  public static void main (String args[])
  {
    if(args.length != 1) 
    {
      System.err.println
        ("Aufruf: DOMBsp01 <dateiname>");
      System.exit(1);
    }
    new DOMBsp(args);     // 4
  }

  // ---- constructor ----
  public DOMBsp(String[] ar)  // 5
  {
    DocumentBuilderFactory factory =  // 6
      DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    try 
    {
      DocumentBuilder parser =  // 7
        factory.newDocumentBuilder();
      doc = parser.parse(ar[0]);  // 8
      
      dokumentAusgeben();   // 9
      
    } catch (Throwable t) {
      t.printStackTrace();
    }
    System.exit(0);       // 10
  }
  
  // ---- Baumstruktur auf mehrere Arten auslesen ---
  public void dokumentAusgeben()  // 11
  {                   
    System.out.println("\n### Beispiel 1 ###\n");
    System.out.println      // 12
      (doc.getDocumentElement());
    
    System.out.println("\n### Beispiel 2 ###\n");
    NodeList nList = doc.   // 13
      getElementsByTagName("*");
    int anzNode = nList.getLength();  // 14
    for (int x=0; x < anzNode; x++) // 15
    {
      System.out.println    // 16
        (""+x+" "+nList.item(x));
    }
    
    System.out.println("\n### Beispiel 3 ###\n");
    Element rootElement =     // 17
      doc.getDocumentElement();
    NodeList nList2 =       // 18
      rootElement.getChildNodes();
    int anzNode2 = nList2.getLength();
    for (int x=0; x < anzNode2; x++)  // 19
    {  
      if (nList2.item(x).getNodeType()==  // 20
        Node.COMMENT_NODE)
      System.out.println(nList2.item(x));
    }
  }
}
