Closed irajhedayati closed 1 year ago
rootTag isn't used for reading, only writing. But yes that is your error, that you have two different elements with the same name, and rowTag of 'item' is ambiguous.
I see; thanks for the response!
Wouldn't it also be helpful to leverage rootTag
in reading? The file may have a structure with different ROWs in different root tags. The same mindset that introduced rootTag
in writing can also apply to reading.
Say that if I write a set of book records in an XML file using spark-xml, it has the following format because I asked for it
<books>
<book></book>
<book></book>
</books>
But I can't provide the root tag when I'm reading the same file using the same library.
It's not impossible, just more complicated to support (the first phase is to detect the relevant row elements without parsing XML for efficiency, so, this gets hard to handle, looking for nested tags). In contrast I think it's pretty unusual to have the same tags used at different levels to mean different things
I agree with the unusual part. The client is sending this file to us, and we don't have control over it. It may get very complex when parsing a file. I appreciate you elaborating on why this feature is not implemented. However, I see that there are different use cases that include rootTag
in reading.
e.g.
do you think that might come into the roadmap any time soon?
I don't think those are actually use cases for rootTag in reading.
I don't think rootTag is the right idea here anyway; imagine you have <item>
somewhere besides the root. Or it's triply-nested. To actually resolve this you'd need some path syntax or something and a new tag.
No roadmap here, I don't think anyone would work on it anyway.
You can accept that it will read the parent <item>
tag, which means it'll read a lot more into the resulting schema than you want, but, if it's not huge then maybe that's fine, you take the overhead and ignore the rest and pull out the actual item
array field you care about.
Is it feasible to transform the XML to remove this ambiguity before parsing?
Thanks for explaining. I went with a hack 😆
if (conf.fileFormatAttributes.topRoot == conf.fileFormatAttributes.rowRoot) {
original
.select(explode(col(conf.fileFormatAttributes.rowRoot)))
.select(col("col.*"))
} else
original
Sample file
Code to read:
Output:
So, it sees the root
item
tag as the row tag and parses one row where it has a single field ofitem
, which is an array.Expected:
this file is actually two records contained in the
rootTag
ofitem
.Having the same name as
rootTag
androwTag
seems to be a problem