Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

public class Main {
    /**
     * 
     * @param currentNode
     * @param tagName
     * @param attributeValue
     * @return
     */
    public static String getTextContentByElementNameANDAttributeValue(Node currentNode, String tagName,
            String attributeValue) {
        String result = "";

        NodeList childNodeList = currentNode.getChildNodes();
        for (int i = 0; i < childNodeList.getLength(); i++) {
            Node childNode = childNodeList.item(i);

            switch (childNode.getNodeType()) {
            case Node.DOCUMENT_NODE:
                break;
            case Node.ELEMENT_NODE:
                Element childElement = (Element) childNodeList.item(i);
                // logger.debug("childElement name : " + childElement.getTagName());
                if (childElement != null && childElement.getNodeName().equals(tagName)) {
                    NamedNodeMap attributes = childElement.getAttributes();
                    for (int j = 0; j < attributes.getLength(); j++) {
                        Node current = attributes.item(j);

                        if (current.getNodeName().equals("type") && current.getNodeValue().equals(attributeValue)) {
                            result = childElement.getTextContent();
                            break;
                        }
                    }
                }
            case Node.TEXT_NODE:
                // logger.debug("textElement name : " + currentNode.getNodeValue());
                break;
            case Node.COMMENT_NODE:
                break;
            case Node.PROCESSING_INSTRUCTION_NODE:
                break;
            case Node.ENTITY_REFERENCE_NODE:
                break;
            case Node.DOCUMENT_TYPE_NODE:
                break;
            }
        }

        return result;
    }
}