-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathxml_adv_test.go
64 lines (54 loc) · 1.31 KB
/
xml_adv_test.go
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package skill
import (
"fmt"
"os"
"testing"
xj "github.com/basgys/goxml2json"
"github.com/buger/jsonparser"
)
type Person struct {
Name string `xml:"name"`
}
func TestParse(t *testing.T) {
xmlFile, err := os.Open("testxml.xml")
if err != nil {
fmt.Println(err)
return
}
defer xmlFile.Close()
json, err := xj.Convert(xmlFile)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(json.String())
//err = jsonparser.ObjectEach(json.Bytes(), objectEach,"xbrl")
//if err != nil {
// fmt.Println(err)
// return
//}
// 營業收入
var revenue string
_, err = jsonparser.ArrayEach(json.Bytes(), func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
revenue, err = jsonparser.GetString(value, "#content")
fmt.Println(revenue)
}, "xbrl", "Revenue")
if err != nil {
fmt.Println(err)
return
}
}
func parse(b []byte) {
err := jsonparser.ObjectEach(b, objectEach, "xbrl")
if err != nil {
fmt.Println(err)
return
}
}
func objectEach(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
fmt.Printf("[Key]: [%s]\n Value: '%s'\n Type: %s\n", string(key), string(value), dataType)
return nil
}
func arrayEach(value []byte, dataType jsonparser.ValueType, offset int, err error) {
fmt.Printf("Array Value: '%s'\n Type: %s\n", string(value), dataType)
}