0

我正在尝试解析一个 Xml 文件,其中一个 pf 标签元素如下

126.5�105�15-4�45�-16MnCr5-1 

当我尝试使用 jaxb 解析并读取 xml 时,内容被更改为如下

126.5 �— 105 �— 15-4�—45°-16MnCr5-1

我不知道为什么我默认也使用 unicode 格式检查它采用 cp1252 格式而不是 UTF-8 格式,即使我在 xml kinldy 帮助中指定了这里是读取传递的 xml 的方法是属性 xml 文件和jaxb类映射

protected T readXml(File xmlFile, Class<?> clazz) throws JAXBException, IOException, XMLStreamException {
  JAXBContext jaxbContext = null;
  Unmarshaller unmarshaller = null;
  jaxbContext = JAXBContext.newInstance(clazz);
  unmarshaller = jaxbContext.createUnmarshaller();
  XMLInputFactory factory = XMLInputFactory.newInstance();
  XMLStreamReader streamReader = null;
  streamReader = factory.createXMLStreamReader(new FileReader(xmlFile));
  return (T) unmarshaller.unmarshal(streamReader);
}
4

1 回答 1

0

在从文件中读取内容时尝试传递编码。

streamReader = factory.createXMLStreamReader(new FileReader(xmlFile),"UTF-8");

那应该解决它。

于 2013-04-17T12:17:06.650 回答