在java中获取XML的编码类型 [英] Getting encoding type of a XML in java

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

问题描述

我在java 1.4中使用 DocumentBuilder 解析XML。

XML的第一行为

I am parsing XML using DocumentBuilder in java 1.4.
XML has first line as

xml version="1.0" encoding="GBK"

我想获得XML的编码类型并使用它。如何获得GBK

基本上我将再创建一个XML,我想要 encoding =GBK保留。
目前它正在迷失并设置为默认 UTF-8

有许多不同编码的XML我需要读取编码od源和必要的东西。

I want to get encoding type of the XML and use it. How can I get "GBK"
Basically i will be making one more XML where i want encoding="GBK" to be retained.
Currently it is getting lost and set to default UTF-8
There are many XML with different encoding I need to read encoding od source and so necessary things.

请帮助

推荐答案

一种方法像这样工作

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

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

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