-
Notifications
You must be signed in to change notification settings - Fork 175
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
RemoveChildAt sometimes dosent work #86
Comments
I think the problem is that removing elements modifies the indices of the remaining elements. Have you tried deleting in reverse order? That would probably work, because the indices of your removed elements won't be changing as you iterate. |
If you look at my code then you can see that i am prepending elements to the deletion list And if look at my log output it also counts backwards while deleting. (first it detects the rules then deletes them in reverse order)
In my explanation i also State that it first tries to delete the third then the first and lastly the zeroth element. |
So i have made a simplified version without the loop.
So when i runt this updated code now i get the same output as before. |
Ok thanks for the further testing. Looks like a probable bug. I'll take a look today. |
Ah, so the problem is that the To do what you're trying to do, you'll need to process the child elements of the filter := doc.FindElement("pfsense/filter")
rules := filter.SelectElements("rule")
filter.RemoveChildAt(rules[3].Index())
filter.RemoveChildAt(rules[1].Index())
filter.RemoveChildAt(rules[0].Index()) (Note that I'm not doing any bounds checking on the |
Oh, interesting. I didn't even think that Children could be anything other than elements. Well, thank you. |
Hi, i have been trying to add and delete rules from a pfsense XML config. Adding has been working great but deleting using RemoveChildAt doesn't work sometimes.
Here is my example code:
And this is the output i get:
What this code should do is go through all rule elements in the filter element and see if that rule has a "created" element with a child "username" element with the text "testUser". if this is true then write down the index of the rule into a slice . This part works fine As it detected all 3 rules with the indexes 0,1,3.
Then it tries to delete the rules by index. The loop works fine as it first tries to delete index 3 then 1 and 0.
But when i then loop over all rules again the last rule element is still there.
The text was updated successfully, but these errors were encountered: