title: How to use variables in XPath?
body: <p>I am using Xpath and Java. </p>
  
  <p>The XML got plenty of <code>OBJECT_TYPES</code> and every object type has properties and parameters.
  And each property and parameter got elements.</p>
  
  <p>How do I do the following from my XML file.
  I wanna know how to select with the XPATH string expression all property elements depending on whats the name of the <code>OBJECT_TYPE</code> string. The object type string name depends on what name the user selects from the list.</p>
  
  <p>How can I do that?</p>
  
  <p>Should be something like :</p>
  
  <pre><code>String expression = "/getObjType()/prop/*"; 
  </code></pre>
  
  <p>But the <code>getObjectType</code> is a method so I cant use it in  a string expression.</p>
  
  <p>XML looks something like this: </p>
  
  <pre><code>&lt;type&gt;
    &lt;OBJECT_TYPE&gt;SiteData&lt;/OBJECT_TYPE&gt; 
    &lt;prop&gt;
      &lt;DESCRIPTION&gt;Site parameters&lt;/DESCRIPTION&gt; 
      &lt;PARENT&gt;NULL&lt;/PARENT&gt; 
      &lt;VIRTUAL&gt;0&lt;/VIRTUAL&gt; 
      &lt;VISIBLE&gt;1&lt;/VISIBLE&gt; 
      &lt;PICTURE&gt;NULL&lt;/PICTURE&gt; 
      &lt;HELP&gt;10008&lt;/HELP&gt; 
      &lt;MIN_NO&gt;1&lt;/MIN_NO&gt; 
      &lt;MAX_NO&gt;1&lt;/MAX_NO&gt; 
      &lt;NAME_FORMAT&gt;NULL&lt;/NAME_FORMAT&gt; 
    &lt;/prop&gt;
    &lt;param&gt;
      &lt;PARAMETER&gt;blabla&lt;/PARAMETER&gt; 
      &lt;DATA_TYPE&gt;INTEGER&lt;/DATA_TYPE&gt; 
      &lt;DESCRIPTION&gt;blaba&lt;/DESCRIPTION&gt; 
      &lt;MIN_NO&gt;1&lt;/MIN_NO&gt; 
      &lt;MAX_NO&gt;1&lt;/MAX_NO&gt; 
      &lt;ORDER1&gt;1&lt;/ORDER1&gt; 
      &lt;NESTED&gt;0&lt;/NESTED&gt; 
      &lt;DEFAULT1&gt;NULL&lt;/DEFAULT1&gt; 
      &lt;FORMAT&gt;0:16382&lt;/FORMAT&gt; 
    &lt;/param&gt;
    &lt;OBJECT_TYPE&gt;Data&lt;/OBJECT_TYPE&gt; 
    &lt;prop&gt;
      &lt;DESCRIPTION&gt;Site parameters&lt;/DESCRIPTION&gt; 
      &lt;PARENT&gt;NULL&lt;/PARENT&gt; 
      &lt;VIRTUAL&gt;0&lt;/VIRTUAL&gt; 
      &lt;VISIBLE&gt;1&lt;/VISIBLE&gt; 
      &lt;PICTURE&gt;NULL&lt;/PICTURE&gt; 
      &lt;HELP&gt;10008&lt;/HELP&gt; 
      &lt;MIN_NO&gt;1&lt;/MIN_NO&gt; 
      &lt;MAX_NO&gt;1&lt;/MAX_NO&gt; 
      &lt;NAME_FORMAT&gt;NULL&lt;/NAME_FORMAT&gt; 
    &lt;/prop&gt;
    &lt;param&gt;
      &lt;PARAMETER&gt;gmgm&lt;/PARAMETER&gt; 
      &lt;DATA_TYPE&gt;INTEGER&lt;/DATA_TYPE&gt; 
      &lt;DESCRIPTION&gt;babla&lt;/DESCRIPTION&gt; 
      &lt;MIN_NO&gt;1&lt;/MIN_NO&gt; 
      &lt;MAX_NO&gt;1&lt;/MAX_NO&gt; 
      &lt;ORDER1&gt;1&lt;/ORDER1&gt; 
      &lt;NESTED&gt;0&lt;/NESTED&gt; 
      &lt;DEFAULT1&gt;NULL&lt;/DEFAULT1&gt; 
      &lt;FORMAT&gt;0:16382&lt;/FORMAT&gt; 
    &lt;/param&gt;
  &lt;/type&gt;
  </code></pre>
  
  <p>So depending on whats the name of the Object_type I wanna get thoose properties and I have list 122 object types so I have to use a varible to pick which one the user selects.</p>
  
  <pre><code> public class PropXMLParsing {
  
      static PropXMLParsing instance = null;
  
      private List&lt;String&gt; list = new ArrayList&lt;String&gt;();
      ObjType obj = new ObjType();
  
      public static PropXMLParsing getInstance() {
  
          if (instance == null) {
  
              instance = new PropXMLParsing();
              try {
                  instance.ParserForObjectTypes();
              } catch (SAXException e) {
  
                  e.printStackTrace();
              } catch (IOException e) {
  
                  e.printStackTrace();
              } catch (ParserConfigurationException e) {
  
                  e.printStackTrace();
              }
  
          }
  
          return instance;
  
      }
  
      public void ParserForObjectTypes() throws SAXException, IOException,
              ParserConfigurationException {
  
          try {
              FileInputStream file = new FileInputStream(new File(
                      "xmlFiles/CoreDatamodel.xml"));
  
              DocumentBuilderFactory builderFactory = DocumentBuilderFactory
                      .newInstance();
  
              builderFactory.setNamespaceAware(true);
              DocumentBuilder builder = builderFactory.newDocumentBuilder();
  
              Document xmlDocument = builder.parse(file);
  
              XPath xp = XPathFactory.newInstance().newXPath();
              final Map&lt;String, Object&gt; vars = new HashMap&lt;String, Object&gt;();
              xp.setXPathVariableResolver(new XPathVariableResolver() {
                  public Object resolveVariable(QName name) {
                      return vars.get(name.getLocalPart());
                  }
              });
  
              XPathExpression expr = xp
                      .compile("/type/OBJECT_TYPE[. = $type]/following-sibling::prop[1]");
  
              vars.put("type", obj.getObjectType());
              NodeList objectProps = (NodeList) expr.evaluate(xmlDocument,
                      XPathConstants.NODESET);
              System.out.println(objectProps);
  
              for (int i = 0; i &lt; objectProps.getLength(); i++) {
  
                  System.out.println(objectProps.item(i).getFirstChild()
                          .getNodeValue());
                  list.add(objectProps.item(i).getFirstChild().getNodeValue());
  
              }
  
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          } catch (SAXException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          } catch (ParserConfigurationException e) {
              e.printStackTrace();
          } catch (XPathExpressionException e) {
              e.printStackTrace();
          }
      }
  
      public String convertListToString() {
  
          StringBuilder sb = new StringBuilder();
          if (list.size() &gt; 0) {
              sb.append(list.get(0));
              for (int i = 1; i &lt; list.size(); i++) {
                  sb.append(list.get(i));
              }
          }
          return sb.toString();
      }
  
  }
  </code></pre>
  
  <p>Second solution I have tried that aint working neither not printing out anything in the console. </p>
  
  <pre><code>public void ParserForObjectTypes() throws SAXException, IOException,
              ParserConfigurationException {
  
          try {
              FileInputStream file = new FileInputStream(new File(
                      "xmlFiles/CoreDatamodel.xml"));
  
              DocumentBuilderFactory builderFactory = DocumentBuilderFactory
                      .newInstance();
  
              DocumentBuilder builder = builderFactory.newDocumentBuilder();
  
              Document xmlDocument = builder.parse(file);
  
              XPath xPath = XPathFactory.newInstance().newXPath();
              NodeList nodeList = (NodeList) xPath.compile(
                      "//OBJECT_TYPE[text() = '" + obj.getObjectType()
                              + "']/following-sibling::prop[1]/*").evaluate(
                      xmlDocument, XPathConstants.NODESET);
  
              for (int i = 0; i &lt; nodeList.getLength(); i++) {
                  System.out.println(nodeList.item(i).getNodeName() + " = "
                          + nodeList.item(i).getTextContent());
              }
  
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          } catch (SAXException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          } catch (ParserConfigurationException e) {
              e.printStackTrace();
          } catch (XPathExpressionException e) {
              e.printStackTrace();
          }
      }
  </code></pre>