blob: bffc417e916040a623ec8854aa3e6dcaa2d9a503 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
package ingest
import (
"encoding/xml"
"io"
"os"
)
type Page struct {
Title string `xml:"title"`
Text string `xml:"revision>text"`
Id int `xml:"id"`
}
func IngestWikiDump(filePath string, jobQueue chan<- Page) error {
file, err := os.Open(filePath)
if err != nil {
return err
}
defer file.Close()
// 3. Attach the streaming XML decoder
decoder := xml.NewDecoder(file)
for {
t, err := decoder.Token()
if err == io.EOF {
break
}
if err != nil {
return err
}
// Look for the opening <page> tag
switch se := t.(type) {
case xml.StartElement:
if se.Name.Local == "page" {
var p Page
// Decode the entire page element into the struct
if err := decoder.DecodeElement(&p, &se); err == nil {
// Push to your worker pool channel
jobQueue <- p
}
}
}
}
return nil
}
|