Oracle XML DOM
Creating a Java class that uses XML parser and DOM APIs
import java.io.*; 
import java.net.*; 
import org.w3c.dom.*; 
import org.w3c.dom.Node; 
import oracle.xml.parser.v2.*; 

public class myXMLDOMlesson { 
  static public void main(String[] argv) { 
    try { 
      if (argv.length != 1) { 
        System.err.println("Usage: java myXMLDOMlesson filename"); 
        System.exit(1); 
      } 
      DOMParser dp = new DOMParser(); 
      URL strURL = createURL(argv[0]); 
      dp.setErrorStream(System.err); 
      dp.setValidationMode(true); 
      dp.showWarnings(true); 
      dp.parse(strURL); 
      XMLDocument xDoc = parser.getDocument(); 
      System.out.print("The Elements Are: "); 
      showElements(xDoc); 
      System.out.println("The Attributes of Each Element Are: "); 
      showElementAttributes(xDoc); 
    } 
    catch (Exception e) { 
      System.out.println(e.toString()); 
  } 
} 
  
static void showElements(Document xDoc) { 
    NodeList nl = xDoc.getElementsByTagName("*"); 
    Node n; 
    for (int i=0; i < nl.getLength(); i++) { 
      n = nl.item(i); 
      System.out.print(n.getNodeName() + " "); 
    } 
    System.out.println(); 
} 

For corrections or feedback, other useful links please contact webmaster

Oracle and Java are registered trademarks of Oracle and/or its affiliates

Last Revised on: March 01, 2012

  1474