@@ -752,13 +752,38 @@ func (e *Element) findTermCharDataIndex(start int) int {
752
752
}
753
753
754
754
// CreateElement creates a new element with the specified tag (i.e., name) and
755
- // adds it as the last child token of this element. The tag may include a
755
+ // adds it as the last child token of the element e . The tag may include a
756
756
// prefix followed by a colon.
757
757
func (e * Element ) CreateElement (tag string ) * Element {
758
758
space , stag := spaceDecompose (tag )
759
759
return newElement (space , stag , e )
760
760
}
761
761
762
+ // CreateContinuation is a continuation-passing interface for building
763
+ // XML documents in a more nested manner. See the description of its
764
+ // use in the Element Create function.
765
+ type CreateContinuation func (e * Element )
766
+
767
+ // Create creates a new element with the specified tag (i.e., name) and adds
768
+ // it as the last child token of the element e. The tag may include a prefix
769
+ // followed by a colon. After creating the element, Create calls the
770
+ // continuation function to perform additional actions on the created child
771
+ // element.
772
+ //
773
+ // This method of creating elements is useful when building nested XML
774
+ // document from code. For example:
775
+ //
776
+ // doc.Create("organization", func(e *Element) {
777
+ // e.Create("person", func(e *Element) {
778
+ // e.CreateAttr("name", "Mary")
779
+ // e.CreateAttr("age", "30")
780
+ // e.CreateAttr("hair", "brown")
781
+ // })
782
+ // })
783
+ func (e * Element ) Create (tag string , f CreateContinuation ) {
784
+ f (e .CreateElement (tag ))
785
+ }
786
+
762
787
// AddChild adds the token 't' as the last child of the element. If token 't'
763
788
// was already the child of another element, it is first removed from its
764
789
// parent element.
0 commit comments