【问题标题】:Error facing while parsing xml java解析xml java时面临的错误
【发布时间】:2013-04-10 04:23:57
【问题描述】:

我正在尝试解析一个 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文件和jaxbclass映射

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);
}

【问题讨论】:

    标签: xml-parsing jaxb saxparser


    【解决方案1】:

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

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

    应该可以解决它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-29
      • 1970-01-01
      • 2013-03-07
      • 2013-11-03
      • 2011-09-11
      相关资源
      最近更新 更多