Java XML Document to String getStringFromXPath(Document doc, XPath xpath, String expression)

Here you can find the source of getStringFromXPath(Document doc, XPath xpath, String expression)

Description

Gets a string from an xml.

License

Open Source License

Parameter

Parameter Description
expression The XPath expression to evaluate, returns a string

Return

The result of evaluating the given XPath expression.

Declaration

public static String getStringFromXPath(Document doc, XPath xpath,
        String expression) 

Method Source Code

//package com.java2s;
/* ***** BEGIN LICENSE BLOCK *****
 *
 * This file is part of Weave.// www.j  a v  a2 s.  c  o m
 *
 * 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 javax.xml.xpath.XPath;

import org.w3c.dom.Document;

public class Main {
    /**
     * Gets a string from an xml.
     * @param expression The XPath expression to evaluate, returns a string
     * @return The result of evaluating the given XPath expression.
     */
    public static String getStringFromXPath(Document doc, XPath xpath,
            String expression) {
        synchronized (xpath) {
            try {
                return xpath.evaluate(expression, doc);
            } catch (Exception e) {
                System.err.println("Error evaluating xpath expression: "
                        + expression);
                e.printStackTrace();
            }
            return "";
        }
    }
}

Related

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