lestrrat-go / libxml2

Interface to libxml2, with DOM interface
MIT License
230 stars 56 forks source link

The Walk method issue #35

Closed jzakrzewski closed 9 months ago

jzakrzewski commented 7 years ago

I tried to Walk all the nodes of a Document starting from root, print all node names, and attributes for the Element nodes. There's however a problem:

root.Walk(func(n types.Node) error {
        fmt.Fprintln(os.Stdout, n.NodeName())
        if n.NodeType() == dom.ElementNode { 
            e := n.(types.Element)
            attrs, err := e.Attributes()
            if err != nil {
                return err
            }
            for _, a := range attrs {
                fmt.Fprintln(os.Stdout, "\t"+a.NodeName())
            }
        }
        return nil
    })

panics at runtime with panic: interface conversion: *dom.XMLNode is not types.Element: missing method AppendText. I've also tried e := n.(*dom.Element) only to get panic: interface conversion: types.Node is *dom.XMLNode, not *dom.Element.

I've debugged a bit and it seems that the receiver gets converted to *dom.XMLNode because of the Walk method receiver type. I can work this around by calling doc.Walk... but it won't work if I wanted to start from an arbitrary node.

Sorry I won't fix it myself but I've just started with go and I don't feel like I could do it right.

lestrrat commented 6 years ago

@jzakrzewski I know this is old, but if you still got code that reproduces this, that would be great. It sure sounds like an omission on my part.

malamud commented 6 years ago

Hi @jzakrzewski @lestrrat Maybe it will help to solve this issue or to fix it. I've got the same problem when I tried to walk over nodes panic: interface conversion: *dom.Text not types.Element: missing method AppendText But I've noticed that in test the same method worked. The difference is a content of xml files Walking through this one works fine:

<?xml version="1.0" encoding="UTF-8"?><Data xmlns="http://www.example.com/OTA/2003/05"><Dates start='1987-01-12'/></Data>

This xml causes panic:

<?xml version="1.0" encoding="UTF-8"?>
<Data xmlns="http://www.example.com/OTA/2003/05">
    <Dates start='1987-01-12'/>
</Data>

This helped me XMLParseNoBlanks

d, err := libxml2.ParseString(s, parser.XMLParseNoBlanks)

Or replace new lines and spaces between nodes but it is not an elegant solution.

jzakrzewski commented 6 years ago

@lestrrat damn! I've missed your comment somehow... I'm not sure if I have it. I guess I have implemented something else instead. But yeah, I can check what was it all about.

github-actions[bot] commented 9 months ago

This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] commented 9 months ago

This issue was closed because it has been stalled for 7 days with no activity. This does not mean your issue is rejected, but rather it is done to hide it from the view of the maintains for the time being. Feel free to reopen if you have new comments