Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

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

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setValidating(false);
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new FileInputStream(new File("input.xml")));
        File OutputDOM = new File("out.txt");
        FileOutputStream fostream = new FileOutputStream(OutputDOM);
        OutputStreamWriter oswriter = new OutputStreamWriter(fostream);
        BufferedWriter bwriter = new BufferedWriter(oswriter);
        if (!OutputDOM.exists()) {
            OutputDOM.createNewFile();
        }
        visitRecursively(doc, bwriter);
        bwriter.close();
        oswriter.close();
        fostream.close();
    }

    public static void visitRecursively(Node node, BufferedWriter bw) throws Exception {
        NodeList list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            // get child node
            Node childNode = list.item(i);
            if (childNode.getNodeType() == Node.TEXT_NODE) {
                System.out.println("Found Node: " + childNode.getNodeName() + " - with value: "
                        + childNode.getNodeValue() + " Node type:" + childNode.getNodeType());

                String nodeValue = childNode.getNodeValue();
                nodeValue = nodeValue.replace("\n", "").replaceAll("\\s", "");
                if (!nodeValue.isEmpty()) {
                    System.out.println(nodeValue);
                    bw.write(nodeValue);
                    bw.newLine();
                }
            }
            visitRecursively(childNode, bw);
        }
    }
}