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:Main.java

public static String getRequestNameFull(String xml)
        throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new StringReader(xml)));

    NodeList nodeList = document.getDocumentElement().getChildNodes();
    Node reqName = document.getElementsByTagName("request-name").item(0);
    System.out.println(reqName.getTextContent());

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        System.out.println(i + "--" + node);
        if (node instanceof Element) {
            NodeList childNodes = node.getChildNodes();
            for (int j = 0; j < childNodes.getLength(); j++) {
                Node cNode = childNodes.item(j);
                System.out.println(i + "--" + j + "--" + cNode);
                if (cNode instanceof Element) {
                    String content = cNode.getLastChild().getTextContent().trim();
                    System.out.println(i + "--" + j + "--" + content);
                }//from ww w. ja  v a 2s .c o  m
            }
        }
    }
    /*
     * Do the parsing for reqname
     */
    return reqName.getTextContent();
}

From source file:Main.java

public static String getArg(final NodeList args, final String strName, final String strDefaultValue) {
    for (int i = 0; i < args.getLength(); ++i) {
        final Node node = args.item(i);

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

            if (arg.getAttribute("name").equals(strName)) {
                return node.getLastChild().getTextContent().trim();
            }/*from w  ww.ja va2  s.c  o m*/
        }
    } // end for

    return strDefaultValue;
}

From source file:marytts.tools.voiceimport.AllophonesExtractor.java

/**
 * //www.  j  a  v a  2s . c o  m
 * @param basename
 * @throws IOException
 */
public void generateAllophonesFile(String basename) throws IOException, MaryConfigurationException {
    Locale localVoice = MaryUtils.string2locale(locale);
    String xmlLocale = MaryUtils.locale2xmllang(localVoice);
    String inputDir = db.getProp(db.TEXTDIR);
    String outputDir = promptAllophonesDir.getAbsolutePath();
    String fullFileName = inputDir + File.separator + basename + db.getProp(db.TEXTEXT);

    // this string controls whether style attributes are inserted into the prompt_allophones ("" -> disabled):
    String style = "";
    if (styleDefinition != null) {
        style = getStyleFromStyleDefinition(basename);
    }

    File textFile = new File(fullFileName);
    String text = FileUtils.readFileToString(textFile, "UTF-8");

    // First, test if there is a corresponding .rawmaryxml file in textdir:
    File rawmaryxmlFile = new File(
            db.getProp(db.MARYXMLDIR) + File.separator + basename + db.getProp(db.MARYXMLEXT));
    if (rawmaryxmlFile.exists()) {
        if (style.isEmpty()) {
            // just pass through the raw file:
            text = FileUtils.readFileToString(rawmaryxmlFile, "UTF-8");
        } else {
            // parse the .rawmaryxml file:
            Document document = null;
            try {
                document = DomUtils.parseDocument(rawmaryxmlFile);
            } catch (Exception e) {
                throw new IOException("Error parsing RAWMARYXML file: " + rawmaryxmlFile.getName(), e);
            }

            // get the <maryxml> node:
            Node maryXmlNode = document.getDocumentElement();
            Node firstMaryXmlChild = maryXmlNode.getFirstChild();
            Node lastMaryXmlChild = maryXmlNode.getLastChild();
            // wrap the <maryxml>'s content in new <prosody> element...
            Element topLevelProsody = DomUtils.encloseNodesWithNewElement(firstMaryXmlChild, lastMaryXmlChild,
                    MaryXML.PROSODY);
            // ...and set its style attribute:
            topLevelProsody.setAttribute("style", style);

            // convert the document to the text string: 
            text = DomUtils.document2String(document);
        }
    } else {
        String prosodyOpeningTag = "";
        String prosodyClosingTag = "";
        if (!style.isEmpty()) {
            prosodyOpeningTag = String.format("<%s style=\"%s\">\n", MaryXML.PROSODY, style);
            prosodyClosingTag = String.format("</%s>\n", MaryXML.PROSODY);
        }
        text = getMaryXMLHeaderWithInitialBoundary(xmlLocale) + prosodyOpeningTag + text + prosodyClosingTag
                + "</maryxml>";
    }

    OutputStream os = new BufferedOutputStream(new FileOutputStream(new File(outputDir, basename + featsExt)));
    MaryHttpClient maryClient = getMaryClient();
    maryClient.process(text, maryInputType, maryOutputType, db.getProp(db.LOCALE), null, null, os);
    os.flush();
    os.close();
}

From source file:com.gargoylesoftware.htmlunit.html.impl.SimpleRange.java

/**
 * {@inheritDoc}//  w w  w. j ava 2 s .  c  o m
 */
@Override
public void selectNodeContents(final Node node) throws RangeException, DOMException {
    startContainer_ = node.getFirstChild();
    startOffset_ = 0;
    endContainer_ = node.getLastChild();
    endOffset_ = getMaxOffset(node.getLastChild());
}

From source file:com.trifork.batchcopy.client.BatchCopyClient.java

private String extractLastToken(Node atomFeedNode) {
    // <atom:feed ..
    //   <atom:id> ..
    //   <atom:updated> ..
    //   <atom:title> ..
    //   <atom:author> ..
    //   <atom:entry> ..
    //    .../*from  w  ww .j a  v a 2s .  c om*/
    //   <atom:entry>
    //     <atom:id>tag:nsi.dk,2011:doseringsforslag/dosageunit/v1/13709460140000000001</atom:id>
    //                                                             |- Sidste del af ovenstende ID er det offset vi skal sende med i nste request
    //     ...
    //   </atom:entry>

    NodeList childNodes = atomFeedNode.getLastChild().getChildNodes();
    for (int i = 0; i < childNodes.getLength(); ++i) {
        Node currentChild = childNodes.item(i);
        if (currentChild.getLocalName().equals("id")) {
            String completeId = currentChild.getTextContent();
            return completeId.substring(completeId.lastIndexOf("/") + 1);
        }
    }
    return null;
}

From source file:DomPrintUtil.java

private Node getVisibleLastChild(Node target) {
    if (!entitiyReferenceExpansion && Node.ENTITY_REFERENCE_NODE == target.getNodeType()) {
        return null;
    }//from   www.ja  v  a  2  s .  co m
    Node tmpN = target.getLastChild();
    if (null == tmpN) {
        return null;
    }

    switch (eval(tmpN)) {
    case NodeFilter.FILTER_ACCEPT:
        return tmpN;
    case NodeFilter.FILTER_SKIP:
        Node tmpN2 = getVisibleLastChild(tmpN);
        if (null != tmpN2) {
            return tmpN2;
        }
        // case NodeFilter.FILTER_REJECT:
    default:
        return getVisiblePreviousSibling(tmpN, target);
    }
}

From source file:com.zimbra.common.util.QuotedTextUtil.java

/**
 * Removes all subsequent siblings of the given node, and then does the same
 * for its parent. The effect is that all nodes that come after the given
 * node in a depth-first traversal of the DOM will be removed.
 *
 * @param node// w  ww .  j  a v a  2  s  .c  om
 * @param clipNode if true, also remove the node
 */
private void prune(Node node, boolean clipNode) {
    Node tempNode = null;
    if (node != null && node.getParentNode() != null) {
        tempNode = node.getParentNode();

        // clip all subsequent nodes
        while (tempNode.getLastChild() != null && tempNode.getLastChild() != node) {
            tempNode.removeChild(tempNode.getLastChild());
        }
        // clip the node if asked
        if (clipNode && tempNode.getLastChild() != null && tempNode.getLastChild() == node) {
            tempNode.removeChild(tempNode.getLastChild());
        }

        String nodeName = tempNode.getNodeName() != null ? tempNode.getNodeName() : "";

        if (!nodeName.equals("body") && !nodeName.equals("html")) {
            prune(tempNode, false);
        }
    }

}

From source file:org.easyrec.service.core.impl.ProfileServiceImpl.java

/**
 * Inserts a new element and value into an XML Document at the position given in xPathExpression
 * relative to the Node given in startNode.
 *
 * @param doc             the Document in which the Element is inserted
 * @param startNode       the Node in the Document used as start point for the XPath Expression
 * @param xPathExpression the XPath from the startNode to the new Element
 * @param value           the value of the new Element
 *//*from  w  w  w . j av a2  s . co  m*/
private Node insertElement(Document doc, Node startNode, String xPathExpression, String value) {

    if (!"".equals(xPathExpression)) {
        String[] xPathTokens = xPathExpression.split("/");
        for (String tag : xPathTokens) {
            if (!"".equals(tag)) {
                Element el = doc.createElement(tag);
                startNode.appendChild(el);
                startNode = startNode.getLastChild();
            }
        }
        if (value != null)
            startNode.setTextContent(value);
    }
    return startNode;
}

From source file:org.energy_home.jemma.ah.internal.greenathome.GreenathomeAppliance.java

private double[] getValuesFromSOAPResponse(SOAPMessage soapResponse) throws Exception {
    NodeList list = soapResponse.getSOAPPart().getElementsByTagName("values");
    if (list != null) {
        forecast_debug += "---found values";
        list = list.item(0).getChildNodes();
        double[] frc_values = new double[24];
        for (int i = 0; i < 24; i++) {
            Node node = list.item(i);
            Node value = node.getLastChild();
            frc_values[i] = Double.parseDouble(value.getTextContent());
            //System.out.println("node name: "+value.getNodeName()+" value: "+value.getTextContent());
        }//from ww  w.  j a v a 2  s.  c o m
        return frc_values;
    } else {
        forecast_debug += "---not found values";
        return null;
    }
}

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

public String moveDownSection(String sectionsSeqXML, String section_id) throws MeleteException {
    try {/*from w w w .j  a  v a 2s  .  c om*/
        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 afterElementParent = moveDownThisElement.getParentNode();
        if (!afterElementParent.getLastChild().equals(moveDownThisElement)) {
            org.w3c.dom.Node afterElement = moveDownThisElement.getNextSibling();
            org.w3c.dom.Node cloneafterElement = afterElement.cloneNode(true);
            afterElementParent.insertBefore(cloneafterElement, moveDownThisElement);
            afterElementParent.removeChild(afterElement);
        }
        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");
    }
}