Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

public class Main {

    public static void main(String argv[]) throws Exception {
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(new File("data.xml"));
        doc.getDocumentElement().normalize();

        System.out.println("City: " + doc.getDocumentElement().getChildNodes().item(0).getFirstChild()
                .getChildNodes().item(0).getAttributes().getNamedItem("data").getNodeValue());

        System.out.println("Postal Code: " + doc.getDocumentElement().getChildNodes().item(0).getFirstChild()
                .getChildNodes().item(1).getAttributes().getNamedItem("data").getNodeValue());

        System.out.println("Date: " + doc.getDocumentElement().getChildNodes().item(0).getFirstChild()
                .getChildNodes().item(4).getAttributes().getNamedItem("data").getNodeValue());
    }
}