Example usage for org.w3c.dom Node getLastChild

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

Introduction

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

Prototype

public Node getLastChild();

Source Link

Document

The last child of this node.

Usage

From source file:org.etudes.component.app.melete.SubSectionUtilImpl.java

public String moveAllDownSection(String sectionsSeqXML, String section_id) throws MeleteException {
    try {//from   w  w  w. ja v  a 2  s .co  m
        org.w3c.dom.Document subSectionW3CDOM = Xml.readDocumentFromString(sectionsSeqXML);
        org.w3c.dom.Element root = subSectionW3CDOM.getDocumentElement();
        org.w3c.dom.Element moveDownThisElement = subSectionW3CDOM.getElementById(section_id);
        org.w3c.dom.Node cloneMoveElement = moveDownThisElement.cloneNode(true);
        org.w3c.dom.Node afterElementParent = moveDownThisElement.getParentNode();
        org.w3c.dom.Node LastChildOfafterElementParent = afterElementParent.getLastChild();
        org.w3c.dom.Node cloneLastChildElement = LastChildOfafterElementParent.cloneNode(true);

        if (!LastChildOfafterElementParent.equals(moveDownThisElement)) {
            afterElementParent.replaceChild(cloneMoveElement, LastChildOfafterElementParent);
            org.w3c.dom.Node newLastChild = afterElementParent.getLastChild();
            afterElementParent.insertBefore(cloneLastChildElement, newLastChild);
            afterElementParent.removeChild(moveDownThisElement);
        }
        return writeDocumentToString(subSectionW3CDOM);
    } catch (Exception ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("some other error on moving down subsections xml string" + ex.toString());
            ex.printStackTrace();
        }
        throw new MeleteException("move_down_fail");
    }
}

From source file:org.olat.core.util.openxml.OpenXMLDocument.java

/**
 * Return the paragraph if and only if it's the last element. Return
 * null if not found./*w w  w. java 2  s . c o m*/
 * @return
 */
private Element getCurrentParagraph() {
    Element paragraphEl = null;
    Node currentNode = getCursor();
    if (currentNode != null && currentNode.getLastChild() != null
            && "w:p".equals(currentNode.getLastChild().getNodeName())) {
        paragraphEl = (Element) currentNode.getLastChild();
    }
    return paragraphEl;
}

From source file:org.openhab.binding.owserver.internal.OWServerBinding.java

String getVariable(String response, String romId, String name) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    // Get the DOM Builder
    DocumentBuilder builder = null;
    try {//  ww  w  .j  a  v a2s . c o  m
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        logger.error("Error parsing OWServer XML response " + e.getMessage());
    }

    // Load and Parse the XML document
    // document contains the complete XML as a Tree.
    Document document = null;
    try {
        InputSource is = new InputSource(new StringReader(response));
        document = builder.parse(is);
    } catch (SAXException e) {
        logger.error("Error reading OWServer XML response " + e.getMessage());
    } catch (IOException e) {
        logger.error("Error reading OWServer XML response " + e.getMessage());
    }

    // Iterating through the nodes and extracting the data.
    NodeList nodeList = document.getDocumentElement().getChildNodes();

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeName().startsWith("owd_")) {
            boolean romMatch = false;
            NodeList childNodes = node.getChildNodes();
            for (int j = 0; j < childNodes.getLength(); j++) {
                Node cNode = childNodes.item(j);

                // Identifying the child tag of employee encountered.
                if (cNode instanceof Element) {
                    String content = cNode.getLastChild().getTextContent().trim();
                    if (cNode.getNodeName().equals("ROMId") & content.equals(romId)) {
                        romMatch = true;
                    }

                    String nname = cNode.getNodeName();
                    if (nname.equals(name) & romMatch == true) {
                        return content;
                    }
                }
            }
        }
    }

    return null;
}

From source file:tufts.vue.ds.XMLIngest.java

private static void scanNode(final XmlSchema schema, final org.w3c.dom.Node node, final int type,
        final String parentPath, final String parentName, final String nodeName, final String value) {
    final boolean isAttribute = (type == Node.ATTRIBUTE_NODE);
    final boolean isMergedText = FOLD_TEXT && isText(type);
    final boolean hasAttributes = (!isAttribute && node != null && node.hasAttributes());
    Node firstChild = null, lastChild = null;

    if (node != null) {
        firstChild = node.getFirstChild();
        lastChild = node.getLastChild();
    }//from   w w w  . ja va  2  s  .com

    final String XMLName;

    if (isAttribute)
        XMLName = parentName + ATTR_SEPARATOR + nodeName;
    else
        XMLName = nodeName;

    final String fullName;

    if (parentPath != null) { // should only be null first time in at the top root
        if (isMergedText)
            fullName = parentPath;
        else if (isAttribute)
            fullName = parentPath + ATTR_SEPARATOR + nodeName;
        else
            fullName = parentPath + '.' + nodeName;
    } else {
        fullName = nodeName;
    }

    if (type == Node.ELEMENT_NODE)
        schema.trackNodeOpen(fullName);

    if (depth < REPORT_THRESH) {
        if (depth < REPORT_THRESH - 1) {
            if (type == Node.TEXT_NODE)
                eoutln(String.format("node(%s) {%s} (len=%d)", getNodeType(type), fullName, value.length()));
            else
                eoutln(String.format("NODE(%s) {%s} %.192s", getNodeType(type), fullName, node,
                        Util.tags(firstChild)));
        }
        //eoutln("NODE: " + type + " name=" + name + " " + Util.tags(n) + " firstChild=" + Util.tags(firstChild));
        //System.err.println(name);
        else if (XML_DEBUG)
            System.err.print(".");
    }

    if (hasAttributes && ATTRIBUTES_IMMEDIATE)
        scanAttributes(schema, fullName, nodeName, node.getAttributes());

    String outputValue = null;

    if (value != null) {
        outputValue = value.trim();
        if (outputValue.length() > 0) {
            schema.trackFieldValuePair(fullName, outputValue);
        } else
            outputValue = null;
    }

    final NodeList children = node == null ? null : node.getChildNodes();
    final boolean DO_TAG;

    if (isMergedText) {
        DO_TAG = false;
    } else if (outputValue == null && node != null) {
        if (!node.hasChildNodes()) {
            DO_TAG = false;
        } else if (children.getLength() == 1 && isText(firstChild)
                && firstChild.getNodeValue().trim().length() == 0) {
            DO_TAG = false;
        } else
            DO_TAG = true;

        // if (!DO_TAG) ioutln("<!-- empty: " + nodeName + " -->");
    } else
        DO_TAG = true;

    boolean closeOnSameLine = false;

    if (DO_TAG) {

        iout("<");
        out(XMLName);
        //if (node.hasChildNodes()) out(" children=" + node.getChildNodes().getLength() + " first=" + node.getFirstChild());
        out(">");

        if (firstChild == null || (isText(firstChild) && firstChild == lastChild)) {
            //                 if (firstChild != null && firstChild.getNodeType() == Node.CDATA_SECTION_NODE)
            //                     ;
            //                 else
            closeOnSameLine = true;
        } else if (XML_OUTPUT)
            System.out.print('\n');

        if (FOLD_TEXT && (type != Node.ELEMENT_NODE && type != Node.ATTRIBUTE_NODE)) {
            final String err = "UNHANDLED TYPE=" + type + "; " + nodeName;
            outln("<" + err + ">");
            errout(err);
        }
    }

    if (outputValue != null) {
        if (type == Node.CDATA_SECTION_NODE) {
            out("<![CDATA[");
            out(outputValue);
            out("]]>");
        } else {
            out(XMLEntityEncode(outputValue));
        }
    }

    if (!isAttribute && node != null) {

        // god knows why, but attributes have themselves as children?  (or is that
        // the #text entry?)  Anyway, if we allow this for an attribute dump, the
        // value of the attribute will literally appear twice in the output,
        // back-to-back as one string.

        depth++;

        if (FOLD_KEYS || schema.isXMLKeyFold()) {

            scanFoldedChildren(schema, children, fullName, nodeName);

        } else {

            for (int i = 0; i < children.getLength(); i++)
                scanNode(schema, children.item(i), fullName, nodeName);
        }

        depth--;

    }

    if (DO_TAG) {

        if (closeOnSameLine)
            outln("</" + XMLName + ">");
        else
            ioutln("</" + XMLName + ">");
    }

    if (type == Node.ELEMENT_NODE)
        schema.trackNodeClose(fullName);

    if (hasAttributes && !ATTRIBUTES_IMMEDIATE)
        scanAttributes(schema, fullName, nodeName, node.getAttributes());

    //iout("children: " + Util.tags(n.getChildNodes()));
}