Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.io.FileInputStream;

import java.io.InputStream;

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

import org.w3c.dom.Document;
import org.w3c.dom.Element;

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

public class Main {
    public static String readProjectName(String file) throws Exception {
        DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
        DocumentBuilder dombuilder = domfac.newDocumentBuilder();
        InputStream is = new FileInputStream(file);
        Document doc = dombuilder.parse(is);
        Element root = doc.getDocumentElement();
        NodeList prjInfo = root.getChildNodes();
        if (prjInfo != null) {
            for (int i = 0; i < prjInfo.getLength(); i++) {
                Node project = prjInfo.item(i);
                if (project.getNodeType() == Node.ELEMENT_NODE) {
                    String strProject = project.getNodeName();
                    if (strProject.equals("project")) {
                        for (Node node = project.getFirstChild(); node != null; node = node.getNextSibling()) {
                            if (node.getNodeType() == Node.ELEMENT_NODE) {
                                String strNodeName = node.getNodeName();
                                if (strNodeName.equals("name")) {
                                    return node.getTextContent();
                                }
                            }
                        }
                    }
                }

            }
        }
        return "";
    }
}