File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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
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
 
Consider Paul's rocket mass heater.
 
subject: How to determine the encoding of an XML string?
 
Similar Threads
Determine Character Set of XMl file using Java
difference between ASCII and binary files
Exception in XML Parsing
Which files have the extension .jspa
Setting encoding in web.xml