mirror of
https://github.com/fumiama/go-docx.git
synced 2026-06-05 07:40:24 +08:00
66 lines
1.6 KiB
Go
66 lines
1.6 KiB
Go
package docxlib
|
|
|
|
import (
|
|
"encoding/xml"
|
|
"io"
|
|
)
|
|
|
|
const (
|
|
XMLNS_W = `http://schemas.openxmlformats.org/wordprocessingml/2006/main`
|
|
XMLNS_R = `http://schemas.openxmlformats.org/officeDocument/2006/relationships`
|
|
XMLNS_WP = `http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing`
|
|
XMLNS_WP14 = `http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing`
|
|
)
|
|
|
|
func getAtt(atts []xml.Attr, name string) string {
|
|
for _, at := range atts {
|
|
if at.Name.Local == name {
|
|
return at.Value
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
type Body struct {
|
|
XMLName xml.Name `xml:"w:body"`
|
|
Paragraphs []*Paragraph `xml:"w:p,omitempty"`
|
|
}
|
|
|
|
type Document struct {
|
|
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main document"`
|
|
XMLW string `xml:"xmlns:w,attr"` // cannot be unmarshalled in
|
|
XMLR string `xml:"xmlns:r,attr,omitempty"` // cannot be unmarshalled in
|
|
XMLWP string `xml:"xmlns:wp,attr,omitempty"` // cannot be unmarshalled in
|
|
XMLWP14 string `xml:"xmlns:wp14,attr,omitempty"` // cannot be unmarshalled in
|
|
Body *Body
|
|
}
|
|
|
|
func (doc *Document) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
|
if doc.Body == nil {
|
|
doc.Body = &Body{
|
|
Paragraphs: make([]*Paragraph, 0, 64),
|
|
}
|
|
}
|
|
for {
|
|
t, err := d.Token()
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
|
|
switch tt := t.(type) {
|
|
case xml.StartElement:
|
|
switch tt.Name.Local {
|
|
case "p":
|
|
var value Paragraph
|
|
d.DecodeElement(&value, &start)
|
|
doc.Body.Paragraphs = append(doc.Body.Paragraphs, &value)
|
|
default:
|
|
continue
|
|
}
|
|
}
|
|
|
|
}
|
|
return nil
|
|
|
|
}
|