etree icon indicating copy to clipboard operation
etree copied to clipboard

Add doctype read

Open nacimgoura opened this issue 4 years ago • 1 comments

Hello, how to read doctype in xml file ?

For example :

<!DOCTYPE article PUBLIC "my doctype of doom" "mydoctype.dtd">
<xpath>
    <to>
        <my>
            <infos>trezaq</infos>
        </my>
    </to>
</xpath>

nacimgoura avatar Feb 09 '22 15:02 nacimgoura

Hello, here my proposal:

package main

import (
	"fmt"

	"github.com/beevik/etree"
)

func main() {
	xml := `<!DOCTYPE article PUBLIC "my doctype of doom" "mydoctype.dtd">
	<xpath>
		<to>
			<my>
				<infos>trezaq</infos>
			</my>
		</to>
	</xpath>`

	doc := etree.NewDocument()
	if err := doc.ReadFromString(xml); err != nil {
		panic(err)
	}

	children := doc.Child
	for _, child := range children {
		if dt, ok := child.(*etree.Directive); ok {
			fmt.Print(dt.Data)
		}
	}
}

giesan avatar Jun 21 '22 21:06 giesan