title: Escape '<' and '>' sign while using XPath in Java
body: <p>I'm using the Java XPath API to modify and work with XML nodes. Reading in the document using DocumentBuilder and writing back to the document using the Transformer API.</p>
  <pre><code>try {
              DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
              DocumentBuilder b = f.newDocumentBuilder();
              String pathToFile = ReadConfigFile.readPathToSampleFactsFile();
              
              Document doc = b.parse(new File(pathToFile));   
              XPath xPath = XPathFactory.newInstance().newXPath();
              Node maxSubjects = (Node) xPath.compile(&quot;//*[contains(@name, 'max_subjects')]&quot;).evaluate(doc, XPathConstants.NODE);
              maxSubjects.setTextContent(&quot;&lt;xml&gt;&quot;);       
  
              Transformer tf = TransformerFactory.newInstance().newTransformer();
              tf.setOutputProperty(OutputKeys.INDENT, &quot;yes&quot;);
              tf.setOutputProperty(OutputKeys.METHOD, &quot;xml&quot;);
              tf.setOutputProperty(&quot;{http://xml.apache.org/xslt}indent-amount&quot;, &quot;5&quot;);
                      
  
              DOMSource domSource = new DOMSource(doc);
              String fileName = BuildXML.buildXMLFileName(simPar)+&quot;.facts&quot;;
              StreamResult sr = new StreamResult(new File(fileName));
              tf.transform(domSource, sr);
  }
  
  </code></pre>
  <p>I would like to set the text content of a node with some XML tag. I have used an example here for the <strong>maxSubjects</strong> node. I am not able to sucessfully escape the lesser-than and greater-than symbols though.</p>
  <p>I tried escaping the lesser-than symbol with &amp;lt; and with the Unicode representation. However, it is not properly represented in the XML generated.
  <em>The &lt; get represented as &amp;lt; in the generated XML</em></p>