我解析這個XML文件:的XPath產生混亂的輸出,而不是Unicode字符
<?xml version="1.0" encoding="UTF-8"?>
<tests>
<test category="Русский"/>
<test category="ελληνικά"/>
<test category="中文"/>
<test category="English"/>
</tests>
主類是:
import java.io.File;
import java.io.FileInputStream;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class TestUnicode {
public static void main(String[] args) throws Exception {
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression lolwhy = xpath.compile("//test");
final InputSource inputSource =
new InputSource(
new FileInputStream(
new File("sample.xml")));
NodeList parent = (NodeList) lolwhy.evaluate(
inputSource,
XPathConstants.NODESET);
System.out.println(parent.getLength());
for (int i = 0; i < parent.getLength(); i++) {
System.out.println(parent.item(i).getAttributes().
getNamedItem("category").getNodeValue());
}
}
}
,輸出是:
4 ??????? ???????? ?? English
什麼時我在這裏做錯了嗎?
編輯:沒關係,這個問題涉及到hebrew appears as question marks in netbeans和解決方案是這樣的:Setting the default Java character encoding?
您的Java控制檯不明白髮送給它的文本的編碼。嘗試將輸出寫入文本文件並閱讀。 – 2011-06-05 13:50:43