import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.*;

public class XmlReader {

   static void processNode(Node node) {
      String msg = "node --> " + node.getNodeName();
      String nodeContent = node.getFirstChild().getNodeValue();
      if (!nodeContent.equals("\n"))  //check if has text content
         msg = msg + " | value: " + nodeContent; //add text content
      if (node.hasAttributes())  //add first attrib if exists
          msg = msg + " | attrib: " +
            node.getAttributes().item(0).getNodeName() + "=" +
            node.getAttributes().item(0).getNodeValue();
      System.out.println(msg);  //display node information
      for (Node subNode = node.getFirstChild(); subNode != null;
            subNode = subNode.getNextSibling())
         if (subNode.getNodeType() == Node.ELEMENT_NODE)
            processNode(subNode);
      }

   public static void main(String[] args) {
      try {
         DocumentBuilder xmlBuilder =
            DocumentBuilderFactory.newInstance().newDocumentBuilder();
         Document xmlDoc = xmlBuilder.parse(new File("countries.xml"));
         processNode(xmlDoc.getDocumentElement());
         }
      catch (Exception e) {
         System.out.println(e.getMessage());
         }
      }

   }

