Java XML Node Path getContent(Node n, String path)

Here you can find the source of getContent(Node n, String path)

Description

Get the text at specified path starting from the specified node

License

Apache License

Parameter

Parameter Description
n a parameter
path a parameter

Declaration

public static String getContent(Node n, String path) 

Method Source Code

//package com.java2s;
/*//from   ww  w .  j a v  a  2s  .  c  o  m
Copyright 2014-2015 Intecs Spa
    
Licensed 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.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**
     * Retrieve the text content of a specified tag
     * @param nl the node list where to search for the tag
     * @param path the path in the form nodeName1/nodeName2/.../nodeNameN.
     *        At each level the first occurrence of the node matching the path 
     *        is considered (i.e. [0] as Xpath)
     * @return the text content or null if the path cannot be matched
     */
    public static String getContent(NodeList nl, String path) {
        if ((nl == null) || (path == null))
            return null;
        if (nl.getLength() == 0)
            return null;
        String[] names = path.split("/");
        if (names.length == 0)
            return null;

        return getContent(nl, names, 0);
    }

    /**
     * Get the text at specified path starting from the specified node
     * @param n
     * @param path
     * @return
     */
    public static String getContent(Node n, String path) {
        if (n == null)
            return null;
        return getContent(n.getChildNodes(), path);
    }

    /**
     * Search for the content specified by the tag names array starting from
     * the specified position 
     * @param nl
     * @param names
     * @param pos
     * @return
     */
    private static String getContent(NodeList nl, String[] names, int pos) {
        if ((nl == null) || (names.length - pos == 0))
            return null;
        if (nl.getLength() == 0)
            return null;

        for (int i = 0; i < nl.getLength(); i++) {
            if (nl.item(i).getNodeName().compareTo(names[pos]) == 0) {

                if (names.length - pos == 1) {
                    return nl.item(i).getTextContent();
                } else {
                    return getContent(nl.item(i).getChildNodes(), names, pos + 1);
                }
            }
        }
        return null;
    }
}

Related

  1. createAndAppendNode(Node root, String path)
  2. extractNodes(Node node, String path)
  3. extractPath(Node node, String[] path)
  4. extractPaths(Node node, String[] path)
  5. getCompletePathForANode(Node objNode)
  6. getDescendant(Node node, String path)
  7. getElementViaPath(Node node, String path)
  8. getFullPath(Node node)
  9. getIndividualPath(Node individual)