获取 XML 文件的编码类型? [英] Getting encoding type of a XML file?

查看:34
本文介绍了获取 XML 文件的编码类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 DocumentBuilder 解析 XML.

I am parsing XML using DocumentBuilder.

XML 的第一行如下:

XML has first line as this:

xml version="1.0" encoding="GBK"

我想获取 XML 的编码类型并使用它.我怎样才能获得"GBK"

I want to get encoding type of the XML and use it. How can I get "GBK"

基本上,我将在我希望保留 encoding="GBK" 的地方再制作一个 XML.

Basically i will be making one more XML where i want encoding="GBK" to be retained.

目前它正在丢失并设置为默认UTF-8

Currently it is getting lost and set to default UTF-8

有许多不同编码的 XML,我需要读取源文件 F 的编码.

There are many XML with different encoding and I need to read encoding of the source fileF.

推荐答案

一种方法是这样的

final XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader( new FileReader( testFile ) );

//running on MS Windows fileEncoding is "CP1251"
String fileEncoding = xmlStreamReader.getEncoding(); 

//the XML declares UTF-8 so encodingFromXMLDeclaration is "UTF-8"
String encodingFromXMLDeclaration = xmlStreamReader.getCharacterEncodingScheme(); 

这篇关于获取 XML 文件的编码类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆