Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dynamic tag name code broke #260

Open
NikkyAI opened this issue Jan 6, 2025 · 1 comment
Open

dynamic tag name code broke #260

NikkyAI opened this issue Jan 6, 2025 · 1 comment

Comments

@NikkyAI
Copy link

NikkyAI commented Jan 6, 2025

used something based on https://github.com/pdvrieze/xmlutil/blob/master/examples/DYNAMIC_TAG_NAMES.md for a while, but the code in there does not work anymore

Exception in thread "main" nl.adaptivity.xmlutil.serialization.XmlParsingException: Invalid XML value at position: null: local name "Test_123" does not match expected "TestElement" (4:14)

this broke with the update t0 0.9.2 (and i have been dragging my feet to update)
how do we apprach this now?

example snippets (no control over the input XML sadly)

<LibraryClosedSections>
    <Section_3 Value="&gt;! Transition"/>
    <Section_6 Value="&gt;Fractal"/>
</LibraryClosedSections>
<QueueWindows>
	<Queue1 properties=""...>
        <Presets>
            <Preset Name="foo"/>
            <Preset Name="bar"/>
        </Presets>
     </Queue1>

	<Queue2 properties=""...>
        <Presets>
            <Preset Name="foo"/>
            <Preset Name="bar"/>
        </Presets>
     </Queue2>
</QueueWindows>

and we want to parse this into a structure like this (although if implementation forces a slightly different structure on us we can live with it)

@Serializable(with = LibraryClosedSectionsSerializer::class)
data class LibraryClosedSections(
    val sections: List<LibraryClosedSection>
) {
    @Serializable
    data class LibraryClosedSection(
        val index: Int,
        @XmlSerialName("Value")
        val value: String
    )
}

@Serializable(with = QueueWindowsSerializer::class)
data class QueueWindows(
    val queues: List<Queue>
) {
    @Serializable
    data class Queue(
        val index: Int,
        @SerialName("Name")
        val name: String,
        // additional attributes omitted for brevity.. its just more of the same
        @XmlElement
        @SerialName("Presets")
        val presetsContainer: Presets,
    ) {
        val presets get() = presetsContainer.presets
        @Serializable
        data class Presets(
            @XmlElement
            @SerialName("Presets")
            val presets: List<Preset>,
        ) {
            @Serializable
            @XmlSerialName("Preset")
            data class Preset(
                @SerialName("Name")
                val name: String,
                @SerialName("Id")
                val id: Int,
                @SerialName("Type")
                val type: String? = null,
                @SerialName("Comments")
                val comments: String? = null,
                // additional attributes omitted for brevity.. its just more of the same
            )
        }
    }
}

this now breaks with

Invalid XML value at position: null: local name "Section_3" does not match expected "LibraryClosedSection" (1:925)

how do we approach this kind of (awful) XML?
.. and i hope the dynamic tagname example can be updated

@pdvrieze
Copy link
Owner

pdvrieze commented Jan 6, 2025

I've just updated the example (also in master), and added a unit test (so it doesn't just break unnoticed). There were some underlying fixes/changes in the parser that the dynamic reader was not updated to handle. I've also updated the code to use the new XmlSerializer interface (that extends KSerializer) – this interface makes xml specific serialization easier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants