Hi!

I am trying to use Xerces to read the XML prolog in order to get the file encoding/version.

Unfortunately, I could not get it to work (the getEncoding() method returns ""). So I guess I must have missed something out despite the fact that I have read the FAQ, the API, as well as previous posts:
<http://xerces.apache.org/xerces2-j/faq-sax.html#faq-6>
<http://www.nabble.com/Accessing-xml-and-doctype-declaration-via-SAX-td15992692.html#a16039477>
<http://java.sun.com/j2se/1.5.0/docs/api/org/xml/sax/ext/Locator2.html#getEncoding() <http://java.sun.com/j2se/1.5.0/docs/api/org/xml/sax/ext/Locator2.html#getEncoding%28%29>>

So in an attempt to be as accurate as possible, here is a code sample:

   private class CustomHandler extends DefaultHandler2 {
Locator2 myLocator = null; public CustomHandler() {
           super();
       }
public void startDocument() { log.debug("Start analysing the XML document"); } public void startElement(String uri, String localName, String qName, Attributes attributes){
           String encoding = myLocator.getEncoding();
           log.debug("encoding: " + encoding);
       }
public void setDocumentLocator(Locator2 aLocator){
           this.myLocator = aLocator;
super.setDocumentLocator(aLocator); } }

   ...

      try{
           //SAX Parser instantiation
SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); //Handler + Locator instantiation
           CustomHandler myHandler = new CustomHandler();
           Locator2 myLocator = new Locator2Impl();
           myHandler.setDocumentLocator(myLocator);
parser.parse(aFile, myHandler); }catch(Throwable e){ log.debug("Error!", e); }
For information, the XML file I am working with looks like:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<root>
   <tag1></tag1>
   <tag2></tag2>
   ...
</root>

I hope someone can help.

Thanks in advance,

--
Olivier DURAND - odur...@clever-age.com
Clever Age  - http://www.clever-age.com
37, Bd des Capucines    -   75002 Paris
Tel:..................+33 1 53 34 66 10
FAX:..................+33 1 53 34 65 20


---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscr...@xerces.apache.org
For additional commands, e-mail: j-users-h...@xerces.apache.org

Reply via email to