I have the following XML:
<root><super-head>Text ☆ and "more" ♥?</super-head></root>
And some entitys (Actually over 400 pieces):
☆ = ☆
♥ = &heart;
" = "
? = ?
- = ‐
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.
In Java i tried the following:
public void replaceStringForNode(Node node, Map<String, String> map) {
// replace for all attributes
NamedNodeMap attributes = node.getAttributes();
for (int i = 0, l = attributes.getLength(); i < l; i++) {
Node attr = attributes.item(i);
String content = attr.getNodeValue();
for (Entry<String, String> 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 < 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<String, String> entry : map.entrySet()) {
content = content.replace(entry.getKey(), entry.getValue());
}
currentNode.setNodeValue(content);;
}
}
}
but in this case i will get the following xml (with escaped &
characters):
<root><super-head>Text &star; and &qout;more&qout; &heart;&quest;</super-head></root>
How can i convert it the best way or fix my issue?