Example usage for org.w3c.dom Element getLastChild

List of usage examples for org.w3c.dom Element getLastChild

Introduction

In this page you can find the example usage for org.w3c.dom Element getLastChild.

Prototype

public Node getLastChild();

Source Link

Document

The last child of this node.

Usage

From source file:Main.java

public static Element getLastChildElement(Element elem) {
    if (elem == null) {
        return null;
    }/*from   w ww. ja va  2s  . c  o  m*/

    for (Node n = elem.getLastChild(); n != null; n = n.getPreviousSibling()) {
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) n;
        }
    }

    return null;
}

From source file:Main.java

@SuppressWarnings({ "ChainOfInstanceofChecks" })
private static void formatElementEnd(/*@Nonnull*/Document document, /*@Nonnull*/Element element, /*@Nonnull*/
        String formatString) {//from  w  w w .  ja v a2  s. co m
    Node lastChild = element.getLastChild();

    if (lastChild != null) {
        if (lastChild instanceof Element) {
            element.appendChild(document.createTextNode(formatString));
        } else if (lastChild instanceof Text) {
            Text textNode = (Text) lastChild;
            String text = textNode.getWholeText();

            if (hasOnlyTextSiblings(textNode)) {
                String trimmedText = text.trim();
                if (!trimmedText.equals(text)) {
                    textNode.replaceWholeText(trimmedText);
                }
            } else {
                if (!formatString.equals(text)) {
                    textNode.replaceWholeText(trimRight(text) + formatString);
                }
            }
        }
    }
}

From source file:DOMEdit.java

public static void addComment(Document doc) {
        Element root = doc.getDocumentElement();
        Element folks = (Element) root.getLastChild();
        Comment comment = doc.createComment("Text of the comment");
        root.insertBefore(comment, folks);
    }/*  w w  w  .j av a  2 s  . c  om*/

From source file:DOMEdit.java

public static void addProcessingInstruction(Document doc) {
        Element root = doc.getDocumentElement();
        Element folks = (Element) root.getLastChild();
        ProcessingInstruction pi;
        pi = (ProcessingInstruction) doc.createProcessingInstruction("validate", "phone=\"lookup\"");
        root.insertBefore(pi, folks);//from w  ww .ja  v a 2 s . c  o  m
    }

From source file:Main.java

public static String getNodeValue(final NodeList nodes, final String strNodeName,
        final String strDefaultValue) {
    for (int i = 0; i < nodes.getLength(); ++i) {
        final Node node = nodes.item(i);

        if (node instanceof Element) {
            final Element elem = (Element) node;

            if (elem.getNodeName().equals(strNodeName)) {
                return elem.getLastChild().getTextContent().trim();
            }//from  w  ww  . j a v a2  s.c o m
        }
    } // end for

    return strDefaultValue;
}

From source file:Main.java

public static String getValue(final NodeList nodes, final String strAttrName, final String strAttrValue) {
    for (int i = 0; i < nodes.getLength(); ++i) {
        final Node node = nodes.item(i);

        if (node instanceof Element) {
            final Element elem = (Element) node;

            if (strAttrValue.equals(elem.getAttribute(strAttrName))) {
                return elem.getLastChild().getTextContent().trim();
            }/*from w  w w  .j a v  a 2  s. co m*/
        }
    } // end for

    return null;
}

From source file:DOMEdit.java

public static void addCDATA(Document doc) {
        Element root = doc.getDocumentElement();
        Element place = (Element) root.getFirstChild();
        Element directions = (Element) place.getLastChild();
        String dirtext = "cdData.";
        CDATASection dirdata = doc.createCDATASection(dirtext);
        directions.replaceChild(dirdata, directions.getFirstChild());
    }/*from w ww.  j a v a  2 s  . co m*/

From source file:DOMEdit.java

public static void modifyingTextbyCuttingandPasting(Document doc) {
        Element root = doc.getDocumentElement();
        Element place = (Element) root.getFirstChild();
        Text name = (Text) place.getFirstChild().getFirstChild();
        Text directions = (Text) place.getLastChild().getFirstChild();

        //    name.deleteData(offset,count);
        name.deleteData(2, 3);//w w w. j  a v  a 2s  . co  m

        //String bridge = directions.substringData(offset,count);
        String bridge = directions.substringData(2, 3);
        name.appendData(bridge);
    }

From source file:DOMEdit.java

public static void editTextbyInsertionandReplacement(Document doc) {
        Element root = doc.getDocumentElement();
        Element place = (Element) root.getFirstChild();
        Text name = (Text) place.getFirstChild().getFirstChild();
        Text directions = (Text) place.getLastChild().getFirstChild();

        // Inserting the word "black"
        //    name.insertData(offset," black");
        name.insertData(3, " black");

        // Replace "left" with "right"
        //directions.replaceData(offset,count,"right");
        directions.replaceData(3, 3, "right");
    }//from w w  w. ja  va 2  s  . c  om

From source file:Main.java

public static void addHook(String hookName, String interfaceName, String identity, boolean superOverride,
        String superOverrideClass) {
    try {/*from   w  w  w .j av a2s  .  c  om*/
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBldr = dFactory.newDocumentBuilder();
        Document doc = dBldr.parse(new File("hooks.xml"));

        Element hooks = doc.getDocumentElement();

        Element hook = doc.createElement("hook");
        hook.setAttribute("name", hookName);

        Element eIdentity = doc.createElement("identity");
        eIdentity.appendChild(doc.createTextNode(identity));
        hook.appendChild(eIdentity);

        Element eInterface = doc.createElement("interface");
        eInterface.appendChild(doc.createTextNode(interfaceName));
        hook.appendChild(eInterface);

        Element eSuper = doc.createElement("super");
        if (superOverride) {
            eSuper.appendChild(doc.createTextNode(superOverrideClass));
        } else {
            eSuper.appendChild(doc.createTextNode("null"));
        }
        hook.appendChild(eSuper);

        hooks.insertBefore(hook, hooks.getLastChild());
        write(doc);
    } catch (SAXException | IOException | ParserConfigurationException e) {
        e.printStackTrace();
    }
}