2013-04-17 59 views
0

我试图解析XML文件,面对其中一个PF标签元素如下错误在解析XML的Java

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

当我试图解析使用JAXB和阅读的内容被chaged以作为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); 
} 

回答

0

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

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

应该解决它。