Skip to content
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

Need an AddElement method #17

Closed
suntong opened this issue Mar 19, 2016 · 11 comments
Closed

Need an AddElement method #17

suntong opened this issue Mar 19, 2016 · 11 comments

Comments

@suntong
Copy link

suntong commented Mar 19, 2016

I need a way to add an etree Element under another etree Element.

Trying to explain in code:

doc := etree.NewDocument()
doc.ReadFromFile("bookstore.xml")
root := doc.SelectElement("bookstore")

Now the root is an etree Element under which are a bunch of <book> XML Elements.

Suppose now I have

docMore.ReadFromString(xmlMoreBooks)

The question is how can I add docMore as new entries under the root etree Element?

I think such feature would be needed by others as well. Please consider adding it.

Thanks

@beevik
Copy link
Owner

beevik commented Mar 21, 2016

I created a new branch with this change. Here is the associated pull request (PR #18).

Please try it out and let me know if it does what you need.

@suntong
Copy link
Author

suntong commented Mar 21, 2016

Thanks a lot Brett!

Could you create a temp tag so that I can pull it from gopkg.in?
Ref http://suntong.github.io/blogs/2016/02/26/godoc-from-branch-code/ for details if you want.

Update, I took a look at the code, and I totally like your new changes. If you create a temp tag, I can do go get from gopkg.in, so as to test the new code on my end.

Thanks

@beevik
Copy link
Owner

beevik commented Mar 21, 2016

It's already got a tag: v0

@suntong
Copy link
Author

suntong commented Mar 21, 2016

sorry, didn't know that. will try this evening...

@suntong
Copy link
Author

suntong commented Mar 22, 2016

Yep, that's exactly what I was hoping for. Thanks!

@suntong suntong closed this as completed Mar 22, 2016
@suntong
Copy link
Author

suntong commented Mar 22, 2016

If element el was already the child of another element, it is removed from its current parent element first

You meant, "If element el was already the child of another element under the same root, it is removed from its current parent element first", right?

I meant,

// To add all book nodes under docA to root
for _, e := range docA.FindElements(".//book") {
    // add (e) under root
    root.AddElement(e)
}

After this loop, the docA will stay the same, without any node being removed, right? provided that docA and root are not under the same root.

@beevik
Copy link
Owner

beevik commented Mar 22, 2016

In that code, docA would be modified, since an element cannot be in two different trees.

To keep docA from being modified, you should do root.AddElement(e.Copy()) instead.

@suntong
Copy link
Author

suntong commented Mar 22, 2016

well, actually I hope not. Check this out,
https://github.com/suntong/lang/blob/master/lang/Go/src/xml/et_example.go#L111

docA is in fact untouched, which I think should be the case.

@beevik
Copy link
Owner

beevik commented Mar 22, 2016

Found the bug. Please see the pull request. (I'll be adding all comments to the PR from now on.)

@suntong
Copy link
Author

suntong commented Mar 22, 2016

Gotya. I'm all good now.

@suntong
Copy link
Author

suntong commented Apr 18, 2016

Hi Brett,

Would you take a look at https://github.com/suntong/lang/blob/master/lang/Go/src/xml/et_example.go please?

That's the code I tested the above feature and fix, and was good with the tag v0 by then. However, it is not working now. I had been checking your commit log trying to figure out what has changed, but wasn't able to figure out myself.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants