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)
| 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. |
| 338 | func (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. |