Java XPath Expression readXmlNodeChildFromFile(String xPath, String path, NamespaceContext nsContext)

Here you can find the source of readXmlNodeChildFromFile(String xPath, String path, NamespaceContext nsContext)

Description

read Xml Node Child From File

License

Open Source License

Declaration

public static String readXmlNodeChildFromFile(String xPath,
            String path, NamespaceContext nsContext) 

Method Source Code

//package com.java2s;
/**/*from   w  w w.j  av  a 2 s .c  o  m*/
 * Copyright (c) 2009, Signavio GmbH
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import java.io.File;
import java.io.FileNotFoundException;

import java.io.IOException;
import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;

import org.xml.sax.SAXException;

public class Main {
    public static String readXmlNodeChildFromFile(String xPath,
            String path, NamespaceContext nsContext) {
        synchronized (path.intern()) {
            File f = new File(path);
            DocumentBuilderFactory xmlFact = DocumentBuilderFactory
                    .newInstance();
            xmlFact.setNamespaceAware(nsContext != null);
            try {
                DocumentBuilder builder = xmlFact.newDocumentBuilder();
                Document doc = builder.parse(f);
                XPath xpath = XPathFactory.newInstance().newXPath();
                if (nsContext != null) {
                    xpath.setNamespaceContext(nsContext);
                }
                String s = xpath.evaluate(xPath, doc);
                return s.trim();
            } catch (XPathExpressionException e) {
                return "";
            } catch (FileNotFoundException e) {
                return "";
            } catch (ParserConfigurationException e) {
                return "";
            } catch (SAXException e) {
                return "";
            } catch (IOException e) {
                return "";
            }
        }
    }
}

Related

  1. merge(XPathExpression expression, InputStream... files)
  2. modelPathExpr()
  3. parseXMLValue(String xml, String xpathExpression)
  4. parseXPath(String expression, NamespaceContext nsContext)
  5. pathExists(XPath xpath, String expression, Object object)
  6. replaceXPathNode(Node replaceThis, Node replaceWith)
  7. singleXPathNode(String expression, Node node)
  8. streamNodes(String xpath, Object node)
  9. string(Node context, String expression)