title: Java XML - Convert characters to entities
body: <p>I have the following XML:</p>
  
  <pre><code>&lt;root&gt;&lt;super-head&gt;Text ​☆ and "more" ♥?&lt;/super-head&gt;&lt;/root&gt;
  </code></pre>
  
  <p>And some entitys (Actually over 400 pieces):</p>
  
  <pre><code>☆ = &amp;star;
  ♥ = &amp;heart;
  " = &amp;quot;
  ? = &amp;quest;
  - = &amp;hyphen;
  </code></pre>
  
  <p>Now i want to replace all characters in the list with their entity. Initially I tried to do this using a regular expression, but it doesn't work. So I assume that Java or XSLT (I can only use 1.0 here) must be used.</p>
  
  <p>In Java i tried the following:</p>
  
  <pre><code>public void replaceStringForNode(Node node, Map&lt;String, String&gt; map) {
      // replace for all attributes
      NamedNodeMap attributes = node.getAttributes();
      for (int i = 0, l = attributes.getLength(); i &lt; l; i++) {
          Node attr = attributes.item(i);
          String content = attr.getNodeValue();
          for (Entry&lt;String, String&gt; entry : map.entrySet()) {
              content = content.replace(entry.getKey(), entry.getValue());
          }
          attr.setNodeValue(content);
      }
  
      // check all child nodes
      NodeList nodeList = node.getChildNodes();
      for (int i = 0; i &lt; nodeList.getLength(); i++) {
          Node currentNode = nodeList.item(i);
          int type = currentNode.getNodeType();
          if (type == Node.ELEMENT_NODE) {
              this.replaceStringForNode(currentNode, map);
          } else if (type == Node.TEXT_NODE) {
              String content = currentNode.setNodeValue();
              for (Entry&lt;String, String&gt; entry : map.entrySet()) {
                  content = content.replace(entry.getKey(), entry.getValue());
              }
              currentNode.setNodeValue(content);;
          }
      }
  }
  </code></pre>
  
  <p>but in this case i will get the following xml (with escaped <code>&amp;</code> characters):</p>
  
  <pre><code>&lt;root&gt;&lt;super-head&gt;Text ​&amp;amp;star; and &amp;amp;qout;more&amp;amp;qout; &amp;amp;heart;&amp;amp;quest;&lt;/super-head&gt;&lt;/root&gt;
  </code></pre>
  
  <p>How can i convert it the best way or fix my issue?</p>