以下代码用于解析xml文件.我注意到没有正确解析表情符号字符.在该示例中,输入在结尾处具有一个表情符号(
/view/emoji/693/people/revolving-hearts),该字符在输出中加倍.这是一个已知的错误?
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XmlTest {
public static void main(String[] args) {
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setValidating(false);
File file = new File("c:\\temp\\emoji.xml");
try {
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(file);
NodeList nodes = doc.getElementsByTagName("entry");
Node node = nodes.item(0);
NamedNodeMap map = ((Element)node).getAttributes();
for (int i=0; i
Node n = map.item(i);
System.out.println();
System.out.println(n.getNodeValue());
char[] chars = n.getNodeValue().toCharArray();
for (int j=0; j
System.out.print(chars[j] + ", " + (int)chars[j] + " ");
}
}
} catch (Exception e) {e.printStackTrace(); }
}
}
这是输入emoji.xml:
并输出:
name
n, 110 a, 97 m, 109 e, 101
💞test💞💞
?, 55357 ?, 56478 t, 116 e, 101 s, 115 t, 116 ?, 55357 ?, 56478 ?, 55357 ?, 56478