Java XPath Create getXmlString(Node inXml, String xpath)

Here you can find the source of getXmlString(Node inXml, String xpath)

Description

get Xml String

License

Apache License

Declaration

public static String getXmlString(Node inXml, String xpath) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.xpath.XPath;

public class Main {
    private static XPath xPath;

    public static String getXmlString(Node inXml, String xpath) {
        if (!xpath.contains("/")) {
            NodeList elements = ((Element) inXml).getElementsByTagName(xpath);
            if (elements.getLength() == 1) {
                return elements.item(0).getTextContent();
            }//  w ww.  j  av a 2s  . co m
        }

        try {
            return xPath.evaluate(xpath, inXml);
        } catch (Exception ex) {
            throw new RuntimeException("Could not run xpath: " + xpath, ex);
        }
    }
}

Related

  1. getXPath()
  2. getXPath()
  3. getXpath()
  4. getXPath()