Java XPath Get getStrFromNode(Node xpathnode)

Here you can find the source of getStrFromNode(Node xpathnode)

Description

Method getStrFromNode

License

Apache License

Parameter

Parameter Description
xpathnode a parameter

Return

the string for the node.

Declaration

public static String getStrFromNode(Node xpathnode) 

Method Source Code

//package com.java2s;
/**/* w  w w.j  a  v  a  2 s .  co m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import org.w3c.dom.Attr;

import org.w3c.dom.Node;

import org.w3c.dom.ProcessingInstruction;
import org.w3c.dom.Text;

public class Main {
    /**
     * Method getStrFromNode
     *
     * @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 ((Attr) xpathnode).getNodeValue();
        } else if (xpathnode.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
            return ((ProcessingInstruction) xpathnode).getNodeValue();
        }

        return null;
    }
}

Related

  1. getScrProperties(String componentName)
  2. getSearchHandlerNode(final File solrconfig)
  3. getSharedXPath()
  4. getSpeficValueFromNode(Node n, String xpathExpr)
  5. getStrFromNode(Node xpathnode)
  6. getString(final String xPath, final Object item)
  7. getString(Node node, XPathExpression expr)
  8. getStringByXPath(String xml, String xpathStr)
  9. getStringValue(Node node, XPathExpression expression)