Java XML Document to String getStringListFromXPath(Document doc, XPath xpath, String rootNodeExpression, String listExpression)

Here you can find the source of getStringListFromXPath(Document doc, XPath xpath, String rootNodeExpression, String listExpression)

Description

Gets a list of strings from an xml.

License

Open Source License

Parameter

Parameter Description
rootNodeExpression Evaluated on the root of the sqlconfig document to get a single Node.
listExpression Evaluated on the Node returned by rootNodeExpression, gets a NodeList.

Return

A list of the text contents of the NodeList returned by evaluating the listExpression.

Declaration

public static List<String> getStringListFromXPath(Document doc,
        XPath xpath, String rootNodeExpression, String listExpression) 

Method Source Code

//package com.java2s;
/* ***** BEGIN LICENSE BLOCK *****
 *
 * This file is part of Weave./*w ww .  j a  v a 2s  . c om*/
 *
 * The Initial Developer of Weave is the Institute for Visualization
 * and Perception Research at the University of Massachusetts Lowell.
 * Portions created by the Initial Developer are Copyright (C) 2008-2015
 * the Initial Developer. All Rights Reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 * 
 * ***** END LICENSE BLOCK ***** */

import java.util.List;
import java.util.Vector;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**
     * Gets a list of strings from an xml.
     * @param rootNodeExpression Evaluated on the root of the sqlconfig document to get a single Node.
     * @param listExpression Evaluated on the Node returned by rootNodeExpression, gets a NodeList.
     * @return A list of the text contents of the NodeList returned by evaluating the listExpression.
     */
    public static List<String> getStringListFromXPath(Document doc,
            XPath xpath, String rootNodeExpression, String listExpression) {
        synchronized (xpath) {
            try {
                return getStringListFromXPath((Node) xpath.evaluate(
                        rootNodeExpression, doc, XPathConstants.NODE),
                        xpath, listExpression);
            } catch (Exception e) {
                System.err.println("Error evaluating xpath expression: "
                        + rootNodeExpression);
                e.printStackTrace();
            }
            return new Vector<String>();
        }
    }

    /**
     * Gets a list of strings from an xml.
     * @param rootNode The root node to perform the listExpression on. 
     * @param listExpression Evaluated on the rootNode, gets a NodeList.
     * @return A list of the text contents of the nodes returned by evaluating the listExpression.
     */
    public static List<String> getStringListFromXPath(Node rootNode,
            XPath xpath, String listExpression) {
        synchronized (xpath) {
            if (rootNode instanceof Document)
                rootNode = ((Document) rootNode).getDocumentElement();
            Vector<String> result = new Vector<String>();
            try {
                NodeList nodes = (NodeList) xpath.evaluate(listExpression,
                        rootNode, XPathConstants.NODESET);
                for (int i = 0; i < nodes.getLength(); i++) {
                    result.addElement(nodes.item(i).getTextContent());
                }
            } catch (Exception e) {
                System.err.println("Error evaluating xpath expression: "
                        + listExpression);
                e.printStackTrace();
            }
            return result;
        }
    }
}

Related

  1. getStringFromDocument(Document doc)
  2. getStringFromDOM(Document doc)
  3. getStringFromDomDocument(org.w3c.dom.Document doc, org.w3c.dom.Document xslt)
  4. getStringFromXMLDocument(Document doc)
  5. getStringFromXPath(Document doc, XPath xpath, String expression)
  6. getStringRepresentation(DocumentFragment df)
  7. getXml(Document doc)
  8. getXML(Document doc)
  9. getXML(Document pDocument)