jenetics/jpx

Need some help on XMLProvider

RobinChien opened this issue ยท 1 comments

Hi,
I have a question.
I want to add the custom namespace(xmlns:xsi, xsi:schemaLocation, ...) in tag like this but I have no idea.
Can you give me some examples? ๐Ÿ™‡

<?xml version='1.0' encoding='UTF-8'?>
<gpx version="1.1" creator="JPX - https://github.com/jenetics/jpx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.topografix.com/GPX/1/1" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd https://hikingbook.net/xmlschemas/1/1 https://hikingbook.net/xmlschemas/1/1/gpxhb.xsd"
	xmlns:gpxhb="https://hikingbook.net/xmlschemas/1/1">
	<trk>
		<trkseg>
			<trkpt lat="48.2081743" lon="16.3738189">
				<ele>160.0</ele>
			</trkpt>
			<trkpt lat="48.2081743" lon="16.3738189">
				<ele>161.0</ele>
			</trkpt>
			<trkpt lat="48.2081743" lon="16.3738189">
				<ele>162.0</ele>
			</trkpt>
		</trkseg>
	</trk>
</gpx>

In addition, I guess have to use the XMLProvider to do this.
So I override the xmlOutputFactory() and try to set the property like this.
But it will throw the Unable to access unsupported property exception when check().
So I want to ask what is the setProperty actually did? ๐Ÿค”

@AutoService(XMLProvider::class)
class GpxDocumentBuilder : XMLProvider() {
    override fun documentBuilderFactory(): DocumentBuilderFactory {
        val factory: DocumentBuilderFactory = DocumentBuilderFactory.newInstance()
        factory.isValidating = true
        factory.isNamespaceAware = true
        return factory
    }

    override fun xmlOutputFactory(): XMLOutputFactory {
        val factory = XMLOutputFactory.newInstance()
        factory.setProperty("gpxhb", "https://hikingbook.net/xmlschemas/1/1")
        return factory
    }
}

Hi, @RobinChien, it is not possible to set additional namespaces or the schema locations via the XMLProvider. This values must be set when writing the GPX data, which is currently not supported by the library. The question is, why do you want to do this? Maybe you want to avoid the namespaces in the <extensions> tag?

<trkpt lat="37.547868" lon="-77.445529">
    <ele>52.4</ele>
    <time>2018-02-26T18:05:54Z</time>
    <extensions xmlns="http://www.topografix.com/GPX/1/1">
        <power>0</power>
        <gpxtpx:TrackPointExtension xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1">
            <gpxtpx:hr>133</gpxtpx:hr>
            <gpxtpx:cad>0</gpxtpx:cad>
        </gpxtpx:TrackPointExtension>
    </extensions>
</trkpt>

Unfortunately, this is not possible, because JPX is not creating an XML document and writing this document to a file. Instead, the library uses a javax.xml.stream.XMLStreamWriter, for writing the GPX data. As consequence, no namespace optimization is performed.