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;
import org.w3c.dom.Text;

public class Main {
    public static void nodeSubUpdate(Element root, String table, String queryType, String queryValue) {
        Node find = getTag(root, table);
        Document doc = find.getOwnerDocument();

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

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

        Node replNode = null;

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

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

                break;
            }
        }

        Element type = doc.createElement(queryType);
        Text value = doc.createTextNode(queryValue);

        type.appendChild(value);

        find.replaceChild(type, replNode);

        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();
        }
    }
}