xmltree/xml_test.go

112 lines
1.7 KiB
Go
Raw Permalink Normal View History

2019-11-29 10:30:20 +00:00
package graph
import (
"io/ioutil"
"log"
"os"
"testing"
"github.com/lestrrat-go/libxml2"
"github.com/lestrrat-go/libxml2/types"
"github.com/lestrrat-go/libxml2/xpath"
"github.com/pkg/errors"
)
func checkRune(c rune, i int) {
}
func TestLibxml(t *testing.T) {
f, err := os.Open("1.xml")
if err != nil {
panic(err)
}
data, err := ioutil.ReadAll(f)
if err != nil {
panic(err)
}
doc, err := libxml2.Parse(data)
if err != nil {
panic(err)
}
root, err := doc.DocumentElement()
if err != nil {
panic(err)
}
cxt, err := xpath.NewContext(root)
if err != nil {
panic(err)
}
result, err := cxt.Find("//diagram")
if err != nil {
panic(err)
}
diagrams := result.NodeIter()
var mdiagram types.Node
for diagrams.Next() {
diagram := diagrams.Node()
result, err = diagram.Find("./mxGraphModel/root")
if err != nil {
panic(err)
}
rootiter := result.NodeIter()
if rootiter.Next() {
root := rootiter.Node()
// id 冲突
if mdiagram == nil {
mdiagram = root
} else {
cells, err := mdiagram.ChildNodes()
errors.Cause(err)
// t.Error(cells)
first := cells.First()
for first != nil {
err = mdiagram.AddChild(first)
if err != nil {
log.Println(err)
}
first, err = first.NextSibling()
if err != nil {
log.Println(err)
}
}
}
}
}
t.Error(mdiagram)
// ndoc := dom.CreateDocument()
// doc.CreateElement("")
// t.Error(spew.Sprint(result.NodeList()))
}
func TestXML(t *testing.T) {
// f, err := os.Open("1.xml")
// if err != nil {
// panic(err)
// }
// buf, err := ioutil.ReadAll(f)
// if err != nil {
// panic(err)
// }
// data := []rune(string(buf))
}