gpt4 book ai didi

java - org.jdom.xpath.Xpath 不返回 UTF-8

转载 作者:行者123 更新时间:2023-11-29 05:39:24 25 4
gpt4 key购买 nike

我有这个 org.jdom.Document,我从中获取一个元素并尝试使用 xpath 输出一个值。问题是 Norwegain 字母显示为 ?而不是 æ ø å。

Element nameNode = (Element) XPath.selectSingleNode( element, "contentdata/name" );
System.out.print(nameNode.getText());
// Produces "S?rbyen"

当我使用

XMLOutputter outputter = new XMLOutputter( Format.getPrettyFormat());
outputter.output( nameNode, System.out );
// Produces "<name>Sørbyen</name>"

那么如何使用 XPath.selectSingleNode() 或 nameNode.getText() 并返回正确的 UTF-8?

更新:事实证明,字符串仅在控制台输出中发生更改,比较 nameNode.getText().equalsIgnoreCase("Sørbyen") 返回 true。

最佳答案

问题不在于 xpath,而在于您尝试验证值的方式。默认情况下,控制台未启用 UTF-8。所以当你使用

System.out.print(nameNode.getText());

它会显示一些奇怪的字符。

如果您使用的是 eclipse,那么您可以通过运行配置 > 通用 -> 编码 -> 从下拉列表中选择 UTF-8 来配置控制台编码设置。

enter image description here

关于java - org.jdom.xpath.Xpath 不返回 UTF-8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18206974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com