Java XML NodeList getContent(NodeList nl, String path)

Here you can find the source of getContent(NodeList nl, String path)

Description

Retrieve the text content of a specified tag

License

Apache License

Parameter

Parameter Description
nl the node list where to search for the tag
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

Declaration

public static String getContent(NodeList nl, String path) 

Method Source Code

//package com.java2s;
/*/*ww  w.j  ava  2 s  .  co  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. findNode(String id, NodeList orgs)
  2. findNodeByName(NodeList nodeList, String name)
  3. getAllNodes(NodeList nodeList, List nodes)
  4. getArray(NodeList nodeList)
  5. getArray(NodeList nodeList)
  6. getCurrentListPosition(Node refNode, NodeList list)
  7. getDeepNode(String name[], NodeList nodes)
  8. getDSNameListInScriptText(String scriptText, NodeList tableList)
  9. getElementById(NodeList nodeList, String id)