Uploaded image for project: 'Xerces2-J'
  1. Xerces2-J
  2. XERCESJ-988

getEncoding() returns null

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Incomplete
    • 2.6.2
    • None
    • SAX
    • None

    Description

      Hi,

      I'm having problems determining the encoding declared
      in the XML document using SAX. I've followed the
      instructions I've found in the FAQ to retrieve the
      encoding, but I always get 'null' as encoding. The
      code below illustrates my problem:

      import java.io.StringReader;
      import java.lang.reflect.Method;
      import org.apache.xerces.parsers.SAXParser;
      import org.xml.sax.InputSource;
      import org.xml.sax.Locator;
      import org.xml.sax.helpers.DefaultHandler;

      public class XercesTest {

      public static void main(String[] args) throws
      Exception {
      String xml = "<?xml version=\"1.0\"
      encoding=\"UTF-8\"?>\n<root/>";
      SAXParser parser = new SAXParser();
      InputSource source = new InputSource(new
      StringReader(xml));
      parser.setContentHandler(new DefaultHandler()

      { private Locator locator; public void setDocumentLocator(Locator loc) { this.locator = loc; }

      public void startDocument()

      { System.out.println("encoding: " + getEncoding(locator)); }

      public void endDocument()

      { System.out.println("encoding: " + getEncoding(locator)); }

      private String getEncoding(Locator loc) {
      String encoding = null;
      Method getEncoding = null;
      try {
      getEncoding =

      loc.getClass().getMethod("getEncoding", new
      Class[]{});
      if(getEncoding != null)

      { encoding = (String)getEncoding.invoke(locator, null); }

      } catch (Exception e) {
      }
      return encoding;
      }
      });
      parser.parse(source);
      }

      }

      The output of the code above is:
      encoding: null
      encoding: null

      Maarten

      Attachments

        Activity

          People

            Unassigned Unassigned
            maartenc Maarten Coene
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: