Get value from nested tag for XML contain in string
<p>I'm trying to read value from nested tag from an XML and having trouble with it.  </p>

<p>My XML file called <em>NewFile.xml</em>  I'm trying read the value in tag4.</p>

<pre><code>&lt;?xml version="1.0"?&gt;
    &lt;tag1&gt;
        &lt;tag2&gt;
            &lt;tag3&gt;
                &lt;tag4&gt;read me&lt;/tag4&gt;
            &lt;/tag3&gt;
        &lt;/tag2&gt;
    &lt;/tag1&gt;
</code></pre>

<p>Here is a main class called <em>Test.java</em> which reads that XML file and coverts it to string and sends it the the method called lookUpValue in reader class.</p>

<pre><code>public class test {

    public static void main(String [] args) throws IOException, XPathExpressionException
    {
        BufferedReader br = new BufferedReader(new FileReader(new File("NewFile.xml")));
        String xmlString;   
        StringBuilder sb = new StringBuilder();

        while((xmlString=br.readLine())!= null){
            sb.append(xmlString.trim());
        }

        xmlString = sb.toString();

        System.out.print(reader.LookUpValue(xmlString));
        br.close();
    }   
}
</code></pre>

<p>Here is the reader class and the method LookupValue:</p>

<pre><code>import java.io.StringReader;

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

import org.xml.sax.InputSource;

public class reader{

    public static String LookUpValue(String sb) throws XPathExpressionException
    {   
        InputSource source = new InputSource(new StringReader(sb));
        XPath xpath = XPathFactory.newInstance().newXPath();
        Object customer = xpath.evaluate("/tag1/tag2/tag3", source, XPathConstants.NODE);
        String value = xpath.evaluate("tag4", customer);

        return ("Value is: " + value);
    }
}
</code></pre>

<p>My question is how can I read the value inside tag4 right now when I run this program I get the following error. </p>

<pre><code>Exception in thread "main" 
javax.xml.transform.TransformerException: Unable to evaluate expression using this context
    at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:363)
    at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(XPathImpl.java:213)
    at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(XPathImpl.java:275)
    at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(XPathImpl.java:365)
    at reader.LookUpValue(LookUpValue.java:21)
    at test.main(test.java:26)
Caused by: java.lang.RuntimeException: Unable to evaluate expression using this context
    at com.sun.org.apache.xpath.internal.axes.NodeSequence.setRoot(NodeSequence.java:212)
    at com.sun.org.apache.xpath.internal.axes.LocPathIterator.execute(LocPathIterator.java:210)
    at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:335)
    ... 5 more
---------
java.lang.RuntimeException: Unable to evaluate expression using this context
    at com.sun.org.apache.xpath.internal.axes.NodeSequence.setRoot(NodeSequence.java:212)
    at com.sun.org.apache.xpath.internal.axes.LocPathIterator.execute(LocPathIterator.java:210)
    at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:335)
    at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(XPathImpl.java:213)
    at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(XPathImpl.java:275)
    at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(XPathImpl.java:365)
    at reader.LookUpValue(LookUpValue.java:21)
    at test.main(test.java:26)
--------------- linked to ------------------
javax.xml.xpath.XPathExpressionException
    at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(XPathImpl.java:289)
    at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(XPathImpl.java:365)
    at reader.LookUpValue(LookUpValue.java:21)
    at test.main(test.java:26)
Caused by: javax.xml.transform.TransformerException: Unable to evaluate expression using this context
    at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:363)
    at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(XPathImpl.java:213)
    at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(XPathImpl.java:275)
    ... 3 more
Caused by: java.lang.RuntimeException: Unable to evaluate expression using this context
    at com.sun.org.apache.xpath.internal.axes.NodeSequence.setRoot(NodeSequence.java:212)
    at com.sun.org.apache.xpath.internal.axes.LocPathIterator.execute(LocPathIterator.java:210)
    at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:335)
    ... 5 more
</code></pre>