我有一个包含中文内容的 XML 文件。但是在显示时我得到了问号。有人可以调查一下这个问题吗?
我的 book.xml :
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<book>
<person>
<first>密码</first>
<last>Pai</last>
<age>22</age>
</person>
</book>
我的代码是:
public static void main (String argv []){
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("book.xml"));
String strDoc=getStringFromDocument(doc);
System.out.println(strDoc);
}
public static String getStringFromDocument(Document doc) {
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
trans.transform(source, result);
String xmlString = sw.toString();
return xmlString.toString();
}
之后我得到??
:
<?xml version="1.0" encoding="UTF-8"?>
<book>
<person>
<first>??</first>
<last>Pai</last>
<age>22</age>
</person>