Parsing XML in ElementTree in Python3: iterate over children with dynamic tags



I am trying to use ElementTree to read in values from an XML document, but having some issues. For all the cases I have read values into Python before using ET, I have used a specific tag. In the hypothetical example below, how do I access the string values for s1, s2 etc? I've tried a few different ways, but with no luck, and the examples I've found online (so far) use specific tags. I am trying to end up in the position where I can have one list of the child values from parent Id 'A', another list for the child values of parent 'B' etc. Any thoughts? Thanks,



<document>
<this id="A">
<s1>"Val_1"</s1>
<s2>"Val_2"</s2>
<s3>"Val_3"</s3>
... # more values here
</this>
<this id="B">
<s1>"Val_1"</s1>
<s2>"Val_2"</s2>
<s3>"Val_3"</s3>
<s4>"Val_4"</s4>
</this>
... # continues here
</document>

No comments:

Post a Comment