aspose file tools*
The moose likes Java in General and the fly likes How to determine the encoding of an XML string? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to determine the encoding of an XML string?" Watch "How to determine the encoding of an XML string?" New topic
Author

How to determine the encoding of an XML string?

James Adams
Ranch Hand

Joined: Sep 12, 2003
Posts: 188
Is there an elegant way to determine the original encoding of some XML? I could parse the first line for the "encoding=", but I wonder if there is another way to go about this which is more elegant. Something like

Document document = saxBuilder.build(xmlFile);
String encoding = document.getEncoding();

(except no such mechanism exists in JDOM)


Thanks in advance for any suggestions!


--James
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
In Dom4J (the XML parser of my choice), you'd simply do

document.getXMLEncoding()


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to determine the encoding of an XML string?