redundent / kotlin-xml-builder

A lightweight type safe builder to build xml documents in Kotlin
Apache License 2.0
151 stars 17 forks source link

[Question] Returned XML from a function is getting encoded. How can we stop this? #32

Closed H2RockyRoad closed 4 years ago

H2RockyRoad commented 4 years ago

We have implemented the Kotlin XML Builder. We are running into an issue when we need to loop 1 to n times on a xml section. We are calling a function to return what we need: We created a kotlin function called createFeature, but when we get a response and attempt to use it, the greater than and less than symbols are encoded (i.e. ">" becomes >)

Our code looks like this: xml("Root"){ "FeatureList { myFunction(myParameters) }

fun myFunction (myParameters: Parameter): Node { return xml("Feature") { "Code" { -myParameters.code} } }

We confirm the output of myFunction is

code

But when we take that result and output it under , we get the encoding:

>Root<>Feature<Featurecode etc, etc,

Any ideas?

UPDATE: We noticed when this posted it took the encoding and actually turned it into greater than and less than symbols. Hopefully you know what we are talking about.

H2RockyRoad commented 4 years ago

We did some investigating and it appears it is the function escapeValue that takes our xml and turns it into the escape codes for "<" and ">" symbols. How can we avoid "re-escaping" xml?

redundent commented 4 years ago

From what I can gather, it sounds like you are appending children to the document as text elements. That's why they are being escaped.

If you could re-paste your example code using the "insert code" button, it will help me understand what the exact issue is.

H2RockyRoad commented 4 years ago

Is there a way to append children not as text elements?

Here's the code re-paste "FeatureList" { val parentFeatures = featureList?.filter { feature -> feature.size == 1 } parentFeatures?.forEach { feature -> var featureXml = createFeatureXml(feature) -featureXml } }

fun createFeatureXml(feature): Node { return xml("Feature") { "Code" { -feature.code } "ValueList" { "Value" { -(feature.value ?: "") } } } }

Thanks

redundent commented 4 years ago

You can use addNode to append children.

"FeatureList" {
    val parentFeatures = featureList?.filter { feature -> feature.size == 1 }
    parentFeatures?.forEach { feature ->
        var featureXml = createFeatureXml(feature)
        addNode(featureXml)
    }
}
H2RockyRoad commented 4 years ago

Awesome, Thank you!