jpx icon indicating copy to clipboard operation
jpx copied to clipboard

Error when writing content with extensions

Open helmut8080 opened this issue 2 months ago • 3 comments

It seems that as soon as I have something like

<extensions><system-message></system-message></extensions>

for example in <trk>...</trk> I get "javax.xml.stream.XMLStreamException: Trying to declare prefix "xmlns" (illegal as per NS 1.1 #4)".

See files below "WithoutExtension.txt" (can not upload ".gpx") when read and write again works. "WithExtension.txt" raises error.

GPX.Reader r = GPX.Reader.of(Version.V11, Mode.LENIENT); GPX gpxWO = r.read(Path.of("WithoutExtension.txt")); GPX.write(gpxWO, Path.of("rewriteWoE.gpx")); GPX gpxW = r.read(Path.of("WithExtension.txt")); GPX.write(gpxW, Path.of("rewriteWE.gpx")); //Error: javax.xml.stream.XMLStreamException: Trying to declare prefix "xmlns" (illegal as per NS 1.1 #4)

WithExtension.txt WithoutExtension.txt

Thanks.

helmut8080 avatar Oct 05 '25 10:10 helmut8080

Hi! I am not able to reproduce the error. I tried it on Linux with Java 21 and the current GPX version 3.2.1. Are you trying the code on Android? Android has a different SAX implementation then the normal Java SE version.

jenetics avatar Oct 06 '25 18:10 jenetics

Is the extensions element correct? The child element should be prefixed with the namespace, right?

<extensions>
    <gpxtpx:system-message></gpxtpx:system-message>
</extensions>

jenetics avatar Oct 06 '25 18:10 jenetics

Hi!

Thanks for the very swift response!

The extension content was just copied from somewhere. Using below fragment does not remove the error.

<extensions>
    <gpxtpx:system-message></gpxtpx:system-message>
</extensions>

I tried it in three different environments all times in eclipse development environment (GPX version 3.2.1): Eclipse 24.09: OpenJDK 22.0.2 (Apache Xalan) Eclipse 25.09: OpenJDK 17.0.10 (Apache Xalan) Eclipse 25.09: OpenJDK 25 (Apache Xalan)

All seem to use "Woodstox XML processor" (package com.ctc.wstx.sw)

In all cases this is the part throwing the error (first "else if (prefix.equals(sPrefixXmlns))"):

        /* 25-Sep-2004, TSa: Let's check that "xml" and "xmlns" are not
         *     (re-)defined to any other value, nor that value they 
         *     are bound to are bound to other prefixes.
         */
        /* 01-Apr-2005, TSa: And let's not leave it optional: such
         *   bindings should never succeed.
         */
        // ... perhaps it really should be optional though?
        {
            if (prefix.equals(sPrefixXml)) { // prefix "xml"
                if (!uri.equals(XMLConstants.XML_NS_URI)) {
                    throwOutputError(ErrorConsts.ERR_NS_REDECL_XML, uri);
                }
            } else if (prefix.equals(sPrefixXmlns)) { // prefix "xmlns"
                if (!uri.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
                    throwOutputError(ErrorConsts.ERR_NS_REDECL_XMLNS, uri);
                }
            } else {
                // Neither of prefixes.. but how about URIs?
                if (uri.equals(XMLConstants.XML_NS_URI)) {
                    throwOutputError(ErrorConsts.ERR_NS_REDECL_XML_URI, prefix);
                } else if (uri.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
                    throwOutputError(ErrorConsts.ERR_NS_REDECL_XMLNS_URI, prefix);
                }
            }

GPX 3.2.1

java.io.IOException: javax.xml.stream.XMLStreamException: javax.xml.transform.TransformerException: org.xml.sax.SAXException: javax.xml.stream.XMLStreamException: Trying to declare prefix "xmlns" (illegal as per NS 1.1 #4)
javax.xml.stream.XMLStreamException: Trying to declare prefix "xmlns" (illegal as per NS 1.1 #4)
	at io.jenetics.jpx.GPX$Writer.write(GPX.java:1508)
	at io.jenetics.jpx.GPX$Writer.write(GPX.java:1528)
	at io.jenetics.jpx.GPX$Writer.write(GPX.java:1544)
	at io.jenetics.jpx.GPX.write(GPX.java:2085)

Thanks!

helmut8080 avatar Oct 07 '25 17:10 helmut8080