Example usage for org.w3c.dom Node PROCESSING_INSTRUCTION_NODE

List of usage examples for org.w3c.dom Node PROCESSING_INSTRUCTION_NODE

Introduction

In this page you can find the example usage for org.w3c.dom Node PROCESSING_INSTRUCTION_NODE.

Prototype

short PROCESSING_INSTRUCTION_NODE

To view the source code for org.w3c.dom Node PROCESSING_INSTRUCTION_NODE.

Click Source Link

Document

The node is a ProcessingInstruction.

Usage

From source file:Main.java

/**
 * @param node/* w ww . j  a  v  a2s. c o  m*/
 */
public static void displayNodeInfo(Node node) {
    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        System.out.println("Document Node ");
        break;
    case Node.ELEMENT_NODE:
        System.out.println("Element Node: " + node.getNodeName());
        break;
    case Node.TEXT_NODE:
        System.out.println("Text Node: " + node.getNodeName());
        break;
    case Node.CDATA_SECTION_NODE:
        System.out.println("CDATA Section Node: ");
        break;
    case Node.COMMENT_NODE:
        System.out.println("Comment Node ");
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        System.out.println("Processing Instruction Node ");
        break;
    case Node.ENTITY_REFERENCE_NODE:
        System.out.println("Entity Reference Node ");
        break;
    case Node.DOCUMENT_TYPE_NODE:
        System.out.println("Document Type Node ");
        break;
    }
}

From source file:Main.java

private static void findAllProcessingIstructions(Node node, String name, List<ProcessingInstruction> result) {
    NodeList nodeList = node.getChildNodes();
    if (nodeList == null) {
        return;//w w w  .ja  v  a2  s  .  com
    }
    for (int i = 0; i < nodeList.getLength(); ++i) {
        Node n = nodeList.item(i);
        if (n.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
            if (name == null || name.length() == 0 || n.getNodeName().equals(name)) {
                result.add((ProcessingInstruction) n);
            }
        }
        findAllProcessingIstructions(n, name, result);
    }
}

From source file:Main.java

/**prints the document tree
 * @param node node to start at/* w ww  .  ja v  a  2s .  co  m*/
 * @param ident amount of indention*/
public static void printTree(Node node, int ident) {
    if (node == null)
        return;
    NodeList children;
    System.out.print("Node: " + node.getNodeName() + " ");
    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        System.out.println("Document Node");
        break;

    case Node.ELEMENT_NODE:
        System.out.println("Element Node");
        break;

    case Node.TEXT_NODE:
        System.out.println("->" + node.getNodeValue().trim() + "<-");
        break;

    case Node.CDATA_SECTION_NODE:
        System.out.println("CData Node");
        break;

    case Node.PROCESSING_INSTRUCTION_NODE:
        System.out.println("Proposing Instruction Node");
        break;

    case Node.ENTITY_REFERENCE_NODE:
        System.out.println("Entity Node");
        break;

    case Node.DOCUMENT_TYPE_NODE:
        System.out.println("Document Node");
        break;

    default:
    }

    for (int j = 0; j < 2 * ident; j++)
        System.out.print(" ");
    System.out.println("It has the following Children");
    children = node.getChildNodes();
    if (children != null) {
        for (int i = 0; i < children.getLength(); i++) {
            for (int j = 0; j < ident; j++)
                System.out.print(" ");
            System.out.print("Child " + ident + "." + i + " = ");
            printNodeType(children.item(i), ident + 1);
        }
        System.out.println();
    }
}

From source file:Main.java

public static String findXslHref(Node node) {
    int type = node.getNodeType();

    if (type == Node.PROCESSING_INSTRUCTION_NODE) {
        String nodeName = node.getNodeName();
        if (nodeName.equalsIgnoreCase("xml-stylesheet")) {
            String nodeValue = node.getNodeValue();
            try {
                int i = nodeValue.indexOf("href=\"");
                int j = nodeValue.indexOf("\"", i + 6);
                String result = nodeValue.substring(i + 6, j);
                return result;
            } catch (StringIndexOutOfBoundsException e) {
                return null;
            }//w  w  w.j  a  v  a  2  s.co m
        }
        return null;
    } else {
        NodeList children = node.getChildNodes();
        int len = children.getLength();
        for (int i = 0; i < len; i++) {
            String result = findXslHref(children.item(i));
            if (result != null)
                return result;
        }
    }
    return null;
}

From source file:Main.java

private static String mergeTextContent(final NodeList nodes) {
    StringBuilder buf = new StringBuilder();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node n = nodes.item(i);//from   ww  w.java2 s.  c  o  m
        final String text;

        switch (n.getNodeType()) {
        case Node.COMMENT_NODE:
        case Node.PROCESSING_INSTRUCTION_NODE:
            // ignore comments when merging
            text = null;
            break;
        default:
            text = getTextContent(n);
            break;
        }

        if (text != null) {
            buf.append(text);
        }
    }
    return buf.toString();
}

From source file:Main.java

public static String getTextContent(final Node node) {
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
    case Node.ATTRIBUTE_NODE:
    case Node.ENTITY_NODE:
    case Node.ENTITY_REFERENCE_NODE:
    case Node.DOCUMENT_FRAGMENT_NODE:
        return mergeTextContent(node.getChildNodes());
    case Node.TEXT_NODE:
    case Node.CDATA_SECTION_NODE:
    case Node.COMMENT_NODE:
    case Node.PROCESSING_INSTRUCTION_NODE:
        return node.getNodeValue();
    case Node.DOCUMENT_NODE:
    case Node.DOCUMENT_TYPE_NODE:
    case Node.NOTATION_NODE:
    default:/*from ww w. j  av  a2  s  .  co m*/
        return null;
    }
}

From source file:Main.java

private static String getValue(Node node, short nodeType) {
    switch (nodeType) {
    case Node.ELEMENT_NODE:
        return ((Element) node).getTagName();

    case Node.TEXT_NODE:
        return ((Text) node).getData();

    case Node.PROCESSING_INSTRUCTION_NODE:
        return ((ProcessingInstruction) node).getData();

    default:/*from w  ww .j a v a  2 s  .  c  o m*/
        return "";
    }
}

From source file:Main.java

public static boolean setNodeValue(Node domNode, String string) {
    if (domNode == null)
        return false;

    short nodeType = domNode.getNodeType();

    switch (nodeType) {
    case Node.ELEMENT_NODE: {
        setElementText((Element) domNode, string);
        break;/*w w  w .jav  a  2  s.c  o m*/
    }
    case Node.ATTRIBUTE_NODE:
    case Node.TEXT_NODE: {
        domNode.setNodeValue(string);
        break;
    }
    case Node.PROCESSING_INSTRUCTION_NODE: {
        ((ProcessingInstruction) domNode).setData(string);
        break;
    }
    case Node.CDATA_SECTION_NODE: {
        ((CDATASection) domNode).setData(string);
        break;
    }
    default: {
        return false;
    }
    }

    return true;
}

From source file:MainClass.java

public static void print(Node node, OutputStream os) {
    PrintStream ps = new PrintStream(os);
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        ps.print("<" + node.getNodeName());

        NamedNodeMap map = node.getAttributes();
        for (int i = 0; i < map.getLength(); i++) {
            ps.print(" " + map.item(i).getNodeName() + "=\"" + map.item(i).getNodeValue() + "\"");
        }/*from   w  ww .j  a  v a 2  s .c  o m*/
        ps.println(">");
        return;
    case Node.ATTRIBUTE_NODE:
        ps.println(node.getNodeName() + "=\"" + node.getNodeValue() + "\"");
        return;
    case Node.TEXT_NODE:
        ps.println(node.getNodeValue());
        return;
    case Node.CDATA_SECTION_NODE:
        ps.println(node.getNodeValue());
        return;
    case Node.PROCESSING_INSTRUCTION_NODE:
        ps.println(node.getNodeValue());
        return;
    case Node.DOCUMENT_NODE:
    case Node.DOCUMENT_FRAGMENT_NODE:
        ps.println(node.getNodeName() + "=" + node.getNodeValue());
        return;
    }
}

From source file:Main.java

/**
 * Method getStrFromNode//www  .ja v a  2  s .  c o m
 *
 * @param xpathnode
 * @return the string for the node.
 */
public static String getStrFromNode(Node xpathnode) {
    if (xpathnode.getNodeType() == Node.TEXT_NODE) {
        // we iterate over all siblings of the context node because eventually,
        // the text is "polluted" with pi's or comments
        StringBuilder sb = new StringBuilder();

        for (Node currentSibling = xpathnode.getParentNode()
                .getFirstChild(); currentSibling != null; currentSibling = currentSibling.getNextSibling()) {
            if (currentSibling.getNodeType() == Node.TEXT_NODE) {
                sb.append(((Text) currentSibling).getData());
            }
        }

        return sb.toString();
    } else if (xpathnode.getNodeType() == Node.ATTRIBUTE_NODE) {
        return xpathnode.getNodeValue();
    } else if (xpathnode.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
        return xpathnode.getNodeValue();
    }

    return null;
}