title: Unable to evaluate expression in XPath
body: <p>I;m using XPath to parse XML document returned by a URL, when i run my code with given inputs it works but when giving it inputs as a user input it throws an exception.
  <strong>The Code:</strong></p>
  
  <pre><code>    class{
          private String generalQuery =  "//@*";
      method(){
          System.out.println("Enter URL");
          url = scan.nextLine();
          URL oracle = new URL(url);
          InputStream is = oracle.openStream();
  
          org.w3c.dom.Document doc = null;
          DocumentBuilderFactory domFactory;
          DocumentBuilder builder;
  
          try {
              domFactory = DocumentBuilderFactory.newInstance();
              domFactory.setNamespaceAware(true);
              builder = domFactory.newDocumentBuilder();
              doc = builder.parse(is);
          } catch (Exception ex) {
              System.err.println("unable to load XML: " + ex);
          }
  
      Map &lt;String, String&gt; params = new HashMap&lt;String, String&gt; ();
  
              XPathFactory factory = XPathFactory.newInstance();
              XPath xpath = factory.newXPath();
              xpath.setNamespaceContext(new NameSpaces(doc));
              XPathExpression expr = xpath.compile(generalQuery);
              Object result = expr.evaluate(doc, XPathConstants.NODESET); // exception thrown here
  
              NodeList nl = (NodeList) result;
  
              for (int i = 0 ; i &lt; nl.getLength() ; i++){
                  Node n = (Node)nl.item(i);
                  params.put(n.getNodeName(), n.getNodeValue());
              }
  
              return params;
  }
  }
  </code></pre>
  
  <p><strong>The Exception:</strong></p>
  
  <pre><code>javax.xml.transform.TransformerException: Unable to evaluate expression using this context
  </code></pre>
  
  <p><strong>The class NameSpaces :</strong></p>
  
  <pre><code>import java.util.Iterator;
  import javax.xml.XMLConstants;
  import javax.xml.namespace.NamespaceContext;
  import org.w3c.dom.Document;
  
  public class NameSpaces implements NamespaceContext {
      private Document sourceDocument;
  
      public NameSpaces(Document document) {
          sourceDocument = document;
      }
  
      @Override
      public String getNamespaceURI(String prefix) {
          if (prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)) {
              return sourceDocument.lookupNamespaceURI(null);
          } else {
              return sourceDocument.lookupNamespaceURI(prefix);
          }
      }
  
      @Override
      public String getPrefix(String namespaceURI) {
          return sourceDocument.lookupPrefix(namespaceURI);
      }
  
      @Override
      public Iterator&lt;String&gt; getPrefixes(String namespaceURI) {
          return null;
      }
  }
  </code></pre>