com.squeakysand.commons.xml.XPathHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of squeakysand-commons Show documentation
Show all versions of squeakysand-commons Show documentation
Classes, interfaces and enums that assist with everyday Java development tasks.
The newest version!
/*
* Copyright 2010-2012 Craig S. Dickson (http://craigsdickson.com)
*
* 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.
*/
package com.squeakysand.commons.xml;
import java.io.InputStream;
import javax.xml.namespace.NamespaceContext;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.InputSource;
/**
* Allows client to easily work with the javax.xml.xpath classes for basic use cases.
*
* @author Craig S. Dickson
*/
public class XPathHelper {
private static final Logger LOG = LoggerFactory.getLogger(XPathHelper.class);
/**
* Evaluates an XPath expression against an XML stream and returns the {@link String} result. If the source XML uses
* namespaces, then the XPath expression must also use namespaces.
*
* @param xmlStream the XML to process.
* @param xpathExpression the XPath expression that must evaluate to a {@link String} result.
* @return the result of the XPath expression.
* @throws XmlProcessingException if there is an issue reading the stream or evaluating the XPath expression.
*/
public static String evaluate(InputStream xmlStream, String xpathExpression) throws XmlProcessingException {
LOG.trace("xmlStream:{}, xpathExpression:{}", xmlStream, xpathExpression);
return evaluate(xmlStream, xpathExpression, null);
}
/**
* Evaluates an XPath expression against an XML stream using the provided context and returns the {@link String}
* result. If the source XML uses the default namespace, then the XPath expression needs to also use the default
* namespace, for example /:project refers to the root node called "project", which is in the default
* namespace.
*
* @param xmlStream the XML to process.
* @param xpathExpression the XPath expression that must evaluate to a {@link String} result.
* @param namespaceContext the context to use, useful when the source document or XPath expression use the default
* namespace, among other uses.
* @return the result of the XPath expression.
* @throws XmlProcessingException if there is an issue reading the stream or evaluating the XPath expression
*/
public static String evaluate(InputStream xmlStream, String xpathExpression, NamespaceContext namespaceContext) throws XmlProcessingException {
if (LOG.isTraceEnabled()) {
LOG.trace("xmlStream:{}, xpathExpression:{}, namespaceContext:{}", new Object[] {xmlStream, xpathExpression, namespaceContext});
}
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
xpath.setNamespaceContext(namespaceContext);
InputSource xmlSource = new InputSource(xmlStream);
Object result = null;
try {
result = xpath.evaluate(xpathExpression, xmlSource);
LOG.debug("result is {}", result == null ? "null" : result);
} catch (XPathExpressionException e) {
LOG.error(e.getMessage(), e);
throw new XmlProcessingException(e);
}
return (String) result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy