
370 • XML & Related Technologies
Exercise 4: For the same XML document, display all the sub-element
details in addition to that of the cd element.
Solution 4:
import org.w3c.dom.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
public class DOMExample4 {
public static void main (String[] args) {
NodeList elements, Children;
String elementName = “cd”;
String local = “”;
Element element = null;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.
newInstance ();
DocumentBuilder builder = factory.newDocumentBuilder ();
Document document = builder.parse (“cdcatalog.xml”);
Element root = document.getDocumentElement ();
...