Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.OutputStreamWriter;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

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 void nodeSubDelete(Element root, String table, String queryType) {
        Node find = getTag(root, table);
        Document doc = find.getOwnerDocument();

        NodeList nl = find.getChildNodes();
        int numnodes = nl.getLength();

        System.out.println("length : " + numnodes);

        Node deleteNode = null;

        for (int i = 0; i < numnodes; i++) {
            Node n = nl.item(i);
            String name = n.getNodeName();

            if (name.equals(queryType)) {
                deleteNode = n;
                System.out.println("Finding Node : " + deleteNode.getNodeName());

                break;
            }
        }

        find.removeChild(deleteNode);

        print(doc);
    }

    public static Node getTag(Element root, String tagName) {
        NodeList list = root.getElementsByTagName(tagName);
        for (int loop = 0; loop < list.getLength(); loop++) {
            Node node = list.item(loop);
            System.out.println("nodeName : " + node.getNodeName() + ":::::" + node.getNodeType());
            if (node.getNodeName().equals(tagName)) {
                return node;
            }
        }
        return null;
    }

    public static void print(Document doc) {
        try {
            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer = tFactory.newTransformer();
            transformer.setOutputProperty("encoding", "utf-8");

            transformer.transform(new DOMSource(doc),
                    new StreamResult(new OutputStreamWriter(System.out, "utf-8")));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}