diff options
author | Russ Cox <rsc@golang.org> | 2009-10-11 23:51:46 -0700 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2009-10-11 23:51:46 -0700 |
commit | 1ec7f0285bddd3bc1e8534fea6077a8b6f841f15 (patch) | |
tree | 566631aff92d3ed507a84fd04025e3fd62c38b33 /src/pkg/xml/read.go | |
parent | 12b5a55f4a94341ea7e1ad00ffa6f295cb4268d5 (diff) | |
download | golang-1ec7f0285bddd3bc1e8534fea6077a8b6f841f15.tar.gz |
introduce non-strict mode in xml parser,
good enough to parse some html.
in reader, add "comment" tag to collect
comment text.
do not allocate during Unmarshal unless pointer is nil.
R=r
DELTA=441 (416 added, 1 deleted, 24 changed)
OCL=35586
CL=35594
Diffstat (limited to 'src/pkg/xml/read.go')
-rw-r--r-- | src/pkg/xml/read.go | 45 |
1 files changed, 33 insertions, 12 deletions
diff --git a/src/pkg/xml/read.go b/src/pkg/xml/read.go index e2d07b913..3671c4534 100644 --- a/src/pkg/xml/read.go +++ b/src/pkg/xml/read.go @@ -162,14 +162,20 @@ func (p *Parser) unmarshal(val reflect.Value, start *StartElement) os.Error { } if pv, ok := val.(*reflect.PtrValue); ok { - zv := reflect.MakeZero(pv.Type().(*reflect.PtrType).Elem()); - pv.PointTo(zv); - val = zv; + if pv.Get() == 0 { + zv := reflect.MakeZero(pv.Type().(*reflect.PtrType).Elem()); + pv.PointTo(zv); + val = zv; + } else { + val = pv.Elem(); + } } var ( data []byte; saveData reflect.Value; + comment []byte; + saveComment reflect.Value; sv *reflect.StructValue; styp *reflect.StructType; ) @@ -251,7 +257,7 @@ func (p *Parser) unmarshal(val reflect.Value, start *StartElement) os.Error { } // Assign attributes. - // Also, determine whether we need to save character data. + // Also, determine whether we need to save character data or comments. for i, n := 0, typ.NumField(); i < n; i++ { f := typ.Field(i); switch f.Tag { @@ -271,6 +277,11 @@ func (p *Parser) unmarshal(val reflect.Value, start *StartElement) os.Error { } strv.Set(val); + case "comment": + if saveComment == nil { + saveComment = sv.FieldByIndex(f.Index); + } + case "chardata": if saveData == nil { saveData = sv.FieldByIndex(f.Index); @@ -326,17 +337,27 @@ Loop: if saveData != nil { data = bytes.Add(data, t); } + + case Comment: + if saveComment != nil { + comment = bytes.Add(comment, t); + } } } - // Save accumulated character data - if saveData != nil { - switch t := saveData.(type) { - case *reflect.StringValue: - t.Set(string(data)); - case *reflect.SliceValue: - t.Set(reflect.NewValue(data).(*reflect.SliceValue)); - } + // Save accumulated character data and comments + switch t := saveData.(type) { + case *reflect.StringValue: + t.Set(string(data)); + case *reflect.SliceValue: + t.Set(reflect.NewValue(data).(*reflect.SliceValue)); + } + + switch t := saveComment.(type) { + case *reflect.StringValue: + t.Set(string(comment)); + case *reflect.SliceValue: + t.Set(reflect.NewValue(comment).(*reflect.SliceValue)); } return nil; |