Here you can find the source of getContent(Node n, String path)
Parameter | Description |
---|---|
n | a parameter |
path | a parameter |
public static String getContent(Node n, String path)
//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; } }