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

Parse children of a node as generic Element #237

Open
TristanCaronUnity opened this issue Sep 8, 2024 · 5 comments
Open

Parse children of a node as generic Element #237

TristanCaronUnity opened this issue Sep 8, 2024 · 5 comments

Comments

@TristanCaronUnity
Copy link

Hello,

Consider the following:

<Extensions>
    <Extension type="AdVerifications">
        <AdVerifications>
            <Verification vendor="Something">
                <JavaScriptResource apiFramework="omid" browserOptional="true">
                    <![CDATA[https://google.com/video.js]]>
                </JavaScriptResource>
                <VerificationParameters>
                    <![CDATA[{"key":"21649"}]]>
                </VerificationParameters>
            </Verification>
        </AdVerifications>
    </Extension>
</Extensions>
@Serializable
class Base {
    var extensions: Extensions? = null

    @Serializable
    class Extensions {
        var extension: List<Extension>? = null

        @Serializable
        class Extension {
            var type: String? = null

            val value: List<Element> = emptyList()
        }
    }
}

Is it possible to have the children to be set in the List<Element> property?

Currently, I get an error saying:

UnknownXmlFieldException: Could not find a field for name (Base.Extensions.Extension) Extension/AdVerifications (Element)
  candidates: type (Attribute), value (Element) at position
@TristanCaronUnity
Copy link
Author

Issue solved.

@Serializable
class Base {
    var extensions: Extensions? = null

    @Serializable
    class Extensions {
        var extension: List<Extension>? = null

        @Serializable
        @XmlIgnoreWhitespace
        class Extension {
            var type: String? = null

            @XmlValue
            val value: List<Element> = emptyList()
        }
    }
}

Please, accept my apologies for the noise. Hopefully it will help someone else in their journey.

@TristanCaronUnity
Copy link
Author

Solved this issue too fast...

It does compile and give me access to the first child. But going I lose the structure for the children, and their attributes.

@pdvrieze
Copy link
Owner

@TristanCaronUnity What do you mean you loose the structure? It should give you a list of the outermost elements (AdVerifications) and then you should be able to use the DOM interface to get the contents.

@TristanCaronUnity
Copy link
Author

TristanCaronUnity commented Sep 10, 2024

The first element is correct, the tag name is preserved; I can do something like value[0].getTagName() or get an attribute. But its first child will be a Text element, instead of being Verification like in my example. It has no attribute, and the content returns



https://google.com/video.js


{"key":"21649"}


Maybe it is expected behaviour.

P.S.: I unblocked my self and did something like this

@Serializable
class Extension(@XmlPolyChildren([".AdVerifications"]) val value: List<@Polymorphic Any>, val type: String? = null) {
    companion object {
        fun module(): SerializersModule {
            return SerializersModule {
                polymorphic(Any::class, AdVerifications::class, AdVerifications.serializer())
            }
        }
    }
}

@pdvrieze
Copy link
Owner

I've added a test to the current dev version. It works correctly there. You want to note though that it the parsing includes whitespace text. The @XmlIgnoreWhitespace annotation only works for the Extension tag itself. For the dev branch I've tweaked the code that this annotation is not needed when the list child is not text (e.g. primitives) or mixed (dom Node) as output type.

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