drmohundro / SWXMLHash

Simple XML parsing in Swift
MIT License
1.4k stars 203 forks source link

Child-Elements has empty Array #255

Closed freakontour closed 2 years ago

freakontour commented 2 years ago

Describe the bug I am trying to read an XML File. I can access to "actuatorDescription" and "groupPara", but if I want to go deeper in the structure I am always getting a empty array back. I didn't understand where is the failure. Can you please explain me what is happened.

I am calling different Print-Function to reach the Data but only these two are working: print(try xml["actuatorDescription"].all) print(try xml["actuatorDescription"]["groupPara"].all)

When I am going deeper with following prints the results are an empty array try xml["actuatorDescription"]["groupPara"]["paraList"].children

XML-File

File-Structure from Debugger: image 327-MB_01.txt

drmohundro commented 2 years ago

Hi @freakontour, good question. I think I know what is going on... as far as I can tell, the groupPara element is always structure like this:

Is that right? Maybe some of those elements are optional. Either way, I think you'd need to enumerate those values by looping each groupPara element and then grabbing the paraList off of it.

So something like this:

        for groupPara in xml["actuatorDescription"]["groupPara"].all {
            let paraList = groupPara["paraList"]
            for child in paraList.children {
                print(child.element?.name)
            }
        }