How to resolved Illegal character Error

  • We are working a desktop application in JAVA and while dealing with XML file
    we are getting this Exception :

    net.sf.saxon.trans.XPathException: Illegal
    character in opaque part at index 2:
    net.sf.saxon.event.Emitter.makeWriter( at
    net.sf.saxon.event.XMLEmitter.openDocument( at
    net.sf.saxon.event.XMLEmitter.startElement( at
    n.event.UncommittedSerializer.startElement( at
    net.sf.saxon.event.NamespaceReducer.startElement( at
    net.sf.saxon.dom.DOMSender.outputElement( at
    net.sf.saxon.dom.DOMSender.walkNode( at
    net.sf.saxon.dom.DOMSender.send( at
    net.sf.saxon.dom.DOMObjectModel.sendSource( at
    net.sf.saxon.event.Sender.send( at

    we are not using saxon JAR any where in code. It automatically get added to
    the class path and throws this exception. How can resolved this exception.
    Can some how either resolved this exception or do not allow this jar to be
    added in class path.

  • Michael Kay
    Michael Kay

    The most likely explanation for the error (but this is a fairly wild guess) is
    that you are using a Windows filename containing a backslash in an API where a
    URI is required: for example result.setSystemId("c:\temp\temp.xml") which
    should be result.setSystemId("file:///c:/temp/temp.xml"). A URI never uses a
    backslash, always a forwards slash. Some products let you get away with this,
    but Saxon generally sticks to the specifications.

    As to why Saxon is being loaded, it seems likely you are using the JAXP
    factory method "new TransformerFactory()" and the JAXP code has found Saxon
    somewhere as the first choice of transformation engine to be loaded. Setting
    the system property jaxp.debug="1" may shed light on the search it is

  • Thanks for response.

    The initialization of StreamResult with File was throwing this

    StreamResult result = new StreamResult(new File(sFilePath));

    and if replaced File with OutputStream then it did not throw such

    StreamResult result = new StreamResult(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(sFilePath), "UTF-8")));

    Can you please help to know the reason ?

    Thanks once again.

  • Michael Kay
    Michael Kay

    I'm surprised: I thought that (except for very early versions of the code) the
    StreamResult(File) constructor would always generate a valid URI. What is in
    sFilePath, and what Java VM are you using?

    Your replaced code using a FileOutputStream doesn't set the SystemId at all.
    For a Result object, you can usually get away with this, but it should be
    avoided for a Source object - it means the base URI of the XML document isn't
    known, which can cause problems when resolving relative URI references.

  • sFilePath is path of the XML from which we are dealing with. We bundled our
    java application and make available to user for download over internet. User
    download the application and installed. So the path where user has been
    installed the application is the current path i.e


    and we append

    "config" + System.getProperty("file.separator") + "Product_Name.xml";

    to the current path.

    File file = new File("./"+ System.getProperty("file.separator")+"config" + System.getProperty("file.separator") + "Product_Name.xml")

    is the sFilePath

    It looks like : C:\<Product_Name>.\config\<Product_Name>.xml

    and we are using Java 1.6.

  • Again if we replaced

    StreamResult result = new StreamResult(new File(sFilePath));


    StreamResult result = new StreamResult();

    then it did not throw exception.
    what may be the reason ?

  • Michael Kay
    Michael Kay

    I'm not able to reproduce this problem from the information given. I've looked
    at the source code of the StreamResult class in the JDK and it's hard to see
    how it can store an invalid URI in the systemId property. (Note that this is
    not Saxon code.) When I do

    StreamResult sr = new StreamResult(new File(".\a\b.xml"));

    then I get something like:


    which is a perfectly legal URI, and works fine with Saxon.

    It would be useful to see what System.err.println(result.getSystemId()) gives
    in your case.

  • System.err.println(result.getSystemId())

    gives C:\<Product_Name>.\config\<Product_Name>.xml

    And I am not sure but if you would like to reproduce then please add
    saxon9he.jar to your project library in which you are trying to reproduce.

  • Michael Kay
    Michael Kay

    Well, C:\<Product_Name>.\config\<Product_Name>.xml is certainly an invalid
    URI - not only are the backslashes illegal, but so are the angle brackets. I
    can't see how that could be the value of getSystemId() after calling new
    StreamResult(new File(...)). I could wash my hands of this because this isn't
    Saxon code, but I'm still intrigued. Could you please try to construct a
    repro: that is, a free-standing piece of Java code, ideally in the form of a
    JUnit test, that demonstrates this? It only needs to contain construction of a
    StreamResult followed by displaying the result of getSystemId().