Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.File;
import java.io.FileInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

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

public class Main {
    public static String getCDATAContentFromFile(File file, String tagName) {
        Document doc = readXMLFile(file);
        Node node = getNode(doc, tagName);
        return getCDATAContent(node);
    }

    public static Document readXMLFile(File file) {

        try {
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(new FileInputStream(file));
            doc.getDocumentElement().normalize();
            return doc;
        } catch (Exception e) {
            throw new RuntimeException("Error occured while reading xmlfile");
        }
    }

    public static Node getNode(Document doc, String tagName) {
        NodeList scriptNodes = doc.getElementsByTagName(tagName);
        return scriptNodes.item(0);
    }

    public static String getCDATAContent(Node node) {
        NodeList nodeList = node.getChildNodes();
        if (nodeList != null) {
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node child = nodeList.item(i);
                if (child.getNodeType() == Node.CDATA_SECTION_NODE) {
                    CDATASection cdata = (CDATASection) child;
                    return cdata.getData();
                }
            }
        }
        return null;
    }
}