pdvrieze / xmlutil

XML Serialization library for Kotlin
https://pdvrieze.github.io/xmlutil/
Apache License 2.0
378 stars 31 forks source link

How do I ignore an entire tag? #243

Open ravenfeld opened 1 week ago

ravenfeld commented 1 week ago

Hello, I'm trying to make a GPX file parser but I'm getting stuck because some sites send this style of tag. At the moment I don't need the information in the extension but it is still read and so the mtb:scale line crashes.

Is it possible for the line to ignore the inside of a tag?

   <trkpt lat="45.148454" lon="5.888473">
        <ele>1205.75</ele>
        <extensions>
          <gpxtpx:TrackPointExtension>
            <gpxtpx:Extensions>
              <reversedirection>yes</reversedirection>
              <highway>path</highway>
              <mtb:scale>2</mtb:scale>
            </gpxtpx:Extensions>
          </gpxtpx:TrackPointExtension>
        </extensions>
      </trkpt>
ravenfeld commented 1 week ago

Perhaps the easiest thing to do is to enter the isNamespaceAware parameters?

private constructor() : this(XmlPullParserFactory.newInstance().apply { isNamespaceAware = true }.newPullParser())

pdvrieze commented 1 week ago

So you mean that you have one file format with path trkpt/extensions/gpxtpx:TrackPointExtension/mtb:scale and one with trkpt/gpxtpx:TrackPointExtension/mtb:scale. Those are different (maybe compatible) file/xml formats. There are some directions you could go into:

ravenfeld commented 1 week ago

I want to parse GPX files, but I don't know who exported the files and depending on the site there is data added in the extension. I don't want to read them, but this could cause a crash.

pdvrieze commented 1 week ago

@ravenfeld You can configure your policy to handle unknown tags/attributes (either by mapping them to a field, ignore them, log them, or throw an exception). That should deal with crashes. Alternatively it is possible to have @XmlValue List<Element> and XmlOtherAttributes to capture the unknown children in a generic way (rather than ignoring them). You can also use either Element or CompactFragment as type of an "extension" child and that will just contain the content of that tag without caring about the specifics.

What neither option does is give you access to data inside the extension in a typesafe way.