MCPcopy Create free account
hub / github.com/beevik/etree / SetRoot

Method SetRoot

etree.go:338–358  ·  view source on GitHub ↗

SetRoot replaces the document's root element with the element 'e'. If the document already has a root element when this function is called, then the existing root element is unbound from the document. If the element 'e' is part of another document, then it is unbound from the other document.

(e *Element)

Source from the content-addressed store, hash-verified

336// existing root element is unbound from the document. If the element 'e' is
337// part of another document, then it is unbound from the other document.
338func (d *Document) SetRoot(e *Element) {
339 if e.parent != nil {
340 e.parent.RemoveChild(e)
341 }
342
343 // If there is already a root element, replace it.
344 p := &d.Element
345 for i, t := range p.Child {
346 if _, ok := t.(*Element); ok {
347 t.setParent(nil)
348 t.setIndex(-1)
349 p.Child[i] = e
350 e.setParent(p)
351 e.setIndex(i)
352 return
353 }
354 }
355
356 // No existing root element, so add it.
357 p.addChild(e)
358}
359
360// ReadFrom reads XML from the reader 'r' into this document. The function
361// returns the number of bytes read and any error encountered.

Callers 2

TestSetRootFunction · 0.95
NewDocumentWithRootFunction · 0.95

Calls 4

RemoveChildMethod · 0.80
addChildMethod · 0.80
setParentMethod · 0.65
setIndexMethod · 0.65

Tested by 1

TestSetRootFunction · 0.76