File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes encoding attribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "encoding attribute" Watch "encoding attribute" New topic

encoding attribute

Kalpesh Soni
Ranch Hand

Joined: Jan 02, 2001
Posts: 312
DOM level 3 specs add new methods in Document interface to get standalone and encoding attributes from xml declaration tag
how is it possible in SAX ?

Test 094, IBM WID 6.0 cert
SCJP 1.2
SCBCD 1.3 Beta
SCWCD 1.4 Beta

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Kalpesh,
I don't think this possibility exists in SAX right now and that it will be planned for future releases...
The question is really why would you need such feature to exist in SAX? Why would you need to access the standalone or encoding attributes of the XML prologue when parsing a XML file?
If you absolutely need to doing this without DOM level3, you may open the document yourself and parse the xml decl. This shouldn't be too hard as the xml decl grammar is very simple. The only tricky bit is managing to read with the correct encoding as caracters are 1 or 2 bytes wide (for the UTF-16 familly of charsets). Now, if you now that your XML documents are not using wide charsets, you can assume you're always having 1 byte per char.
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
OK, I found a way of doing this is you're using Xerces as your favourite SAX parser.
Please note that the following code is based on the actual implementation of Xerces and will not work if you're using a different parser.

The contents of the xmlDeclTest.xml file is as follows:

As expected, when running the XMLDeclSAXParser class, the output gives:

Hope this helps
I agree. Here's the link:
subject: encoding attribute
It's not a secret anymore!