7
7
"errors"
8
8
"io"
9
9
"unicode/utf8"
10
+ "container/list"
10
11
)
11
12
12
13
// XMLAttribute 是一个元素的属性的接口.
@@ -513,7 +514,8 @@ type xmlElementImpl struct {
513
514
xmlNodeImpl
514
515
515
516
// rootAttribute XMLAttribute
516
- attributes map [string ]XMLAttribute
517
+ attrlist * list.List
518
+ attrsmap map [string ]* list.Element
517
519
}
518
520
519
521
func (e * xmlElementImpl ) ToElement () XMLElement {
@@ -542,63 +544,49 @@ func (e *xmlElementImpl) SetName(name string) {
542
544
}
543
545
544
546
func (e * xmlElementImpl ) FindAttribute (name string ) XMLAttribute {
545
- if nil == e .attributes {
546
- return nil
547
- }
548
-
549
- attr , ok := e .attributes [name ]
547
+ elem , ok := e .attrsmap [name ]
550
548
if ! ok {
551
549
return nil
552
550
}
553
551
554
- return attr
552
+ return elem . Value .( * xmlAttributeImpl )
555
553
}
556
554
557
555
func (e * xmlElementImpl ) AttributeCount () int {
558
- if nil == e .attributes {
559
- return 0
560
- }
561
- return len (e .attributes )
556
+ return len (e .attrsmap )
562
557
}
563
558
564
559
func (e * xmlElementImpl ) Attribute (name string , def string ) string {
565
- if nil == e .attributes {
566
- return def
567
- }
568
-
569
- attr , ok := e .attributes [name ]
560
+ attr , ok := e .attrsmap [name ]
570
561
if ! ok {
571
562
return def
572
563
}
573
564
574
- return attr .Value ()
565
+ return attr .Value .( * xmlAttributeImpl ). Value ()
575
566
}
576
567
577
568
func (e * xmlElementImpl ) SetAttribute (name string , value string ) XMLAttribute {
578
- if nil == e .attributes {
579
- e .attributes = make (map [string ]XMLAttribute )
580
- attr := newAttribute (name , value )
581
- e .attributes [name ] = attr
582
- return attr
583
- }
584
-
585
- attr , ok := e .attributes [name ]
569
+ elem , ok := e .attrsmap [name ]
586
570
if ok {
587
- attr .SetValue (value )
588
- return attr
571
+ elem . Value .( * xmlAttributeImpl ) .SetValue (value )
572
+ return elem . Value .( * xmlAttributeImpl )
589
573
}
590
574
591
- attr = newAttribute (name , value )
592
- e .attributes [name ] = attr
575
+ attr : = newAttribute (name , value )
576
+ e .attrsmap [name ] = e . attrlist . PushBack ( attr )
593
577
return attr
594
578
}
595
579
596
580
func (e * xmlElementImpl ) DeleteAttribute (name string ) XMLAttribute {
597
- attr := e .FindAttribute ( name )
598
- if nil == attr {
581
+ elem , ok := e .attrsmap [ name ]
582
+ if ! ok {
599
583
return nil
600
584
}
601
- delete (e .attributes , name )
585
+
586
+ attr := elem .Value .(* xmlAttributeImpl )
587
+
588
+ e .attrlist .Remove (elem )
589
+ delete (e .attrsmap , name )
602
590
return attr
603
591
}
604
592
@@ -620,12 +608,8 @@ func (e *xmlElementImpl) SetText(inText string) {
620
608
}
621
609
622
610
func (e * xmlElementImpl ) ForeachAttribute (callback func (attribute XMLAttribute ) int ) int {
623
- if nil == e .attributes {
624
- return 0
625
- }
626
-
627
- for _ , value := range e .attributes {
628
- if ret := callback (value ); 0 != ret {
611
+ for elem := e .attrlist .Front (); nil != elem ; elem = elem .Next () {
612
+ if ret := callback (elem .Value .(* xmlAttributeImpl )); 0 != ret {
629
613
return ret
630
614
}
631
615
}
@@ -634,7 +618,8 @@ func (e *xmlElementImpl) ForeachAttribute(callback func(attribute XMLAttribute)
634
618
}
635
619
636
620
func (e * xmlElementImpl ) ClearAttributes () {
637
- e .attributes = nil
621
+ e .attrlist = list .New ()
622
+ e .attrsmap = make (map [string ]* list.Element )
638
623
}
639
624
640
625
// ------------------------------------------------------------------
@@ -762,7 +747,8 @@ func NewElement(name string) XMLElement {
762
747
node := new (xmlElementImpl )
763
748
node .implobj = node
764
749
node .value = name
765
- node .attributes = make (map [string ]XMLAttribute )
750
+ node .attrsmap = make (map [string ]* list.Element )
751
+ node .attrlist = list .New ()
766
752
return node
767
753
}
768
754
@@ -785,7 +771,7 @@ func NewDirective(directive string) XMLDirective {
785
771
786
772
// newAttribute 创建一个新的XMLAttribute对象.
787
773
// name和value分别用于指定属性的名称和值
788
- func newAttribute (name string , value string ) XMLAttribute {
774
+ func newAttribute (name string , value string ) * xmlAttributeImpl {
789
775
attr := new (xmlAttributeImpl )
790
776
attr .name = name
791
777
attr .value = value
0 commit comments