antchfx / xmlquery

xmlquery is Golang XPath package for XML query.
https://github.com/antchfx/xpath
MIT License
444 stars 89 forks source link

better XML output formatting. #57

Closed james-lawrence closed 1 year ago

james-lawrence commented 3 years ago

better configurable formatting of XML output. just opening in case its wanted. didn't integrate it with prexisting methods.

coveralls commented 3 years ago

Coverage Status

Coverage decreased (-3.3%) to 89.271% when pulling 8cd59754b8731a52a11d27a03da8420fa616adef on james-lawrence:enh/formatter into e73954f0f504eaf97f73ad62a4c52419e304b7bd on antchfx:master.

zhengchun commented 3 years ago

Good work. I have tested the code. I found DeclarationNode node will missing its attribute values when called Format()

for example:

xml := `
    <?xml version="1.0"?>
    <root></root>
        `
doc, _:= xmlquery.Parse(strings.NewReader(xml))
fmt.Println(xmlquery.Format(doc))

output:

<?xml?><root></root>

It should output attribute values if they have. I guess the OutputXML() method can integrate with your Format() method, like this:

func (n *Node) OutputXML(self bool) string {
    var buf bytes.Buffer
    var f formatter
    if self {       
        f.Output(&buf, n)
    } else {
        for n := n.FirstChild; n != nil; n = n.NextSibling {
            f.Output(&buf, n)
        }
    }
    return buf.String()
}
james-lawrence commented 3 years ago

yeah its easy to integrate just didn't want to go the extra step if the code wasn't wanted. I'll clean up this PR soon including fixing the declaration. (didn't care for my usecase because the declaration isn't really useful)