Documentation
¶
Overview ¶
Package tinydom 实现了一个简单的XML的DOM树构造工具.
Index ¶
- Variables
- func EscapeAttribute(w io.Writer, s []byte) error
- func EscapeText(w io.Writer, s []byte) error
- func SaveDocument(doc XMLDocument, writer io.Writer, options PrintOptions) error
- func SaveDocumentToFile(doc XMLDocument, name string, options PrintOptions) error
- func Version() string
- type DefaultVisitor
- func (v *DefaultVisitor) VisitComment(c XMLComment) bool
- func (v *DefaultVisitor) VisitDirective(d XMLDirective) bool
- func (v *DefaultVisitor) VisitEnterDocument(doc XMLDocument) bool
- func (v *DefaultVisitor) VisitEnterElement(elem XMLElement) bool
- func (v *DefaultVisitor) VisitExitDocument(doc XMLDocument) bool
- func (v *DefaultVisitor) VisitExitElement(elem XMLElement) bool
- func (v *DefaultVisitor) VisitProcInst(pi XMLProcInst) bool
- func (v *DefaultVisitor) VisitText(text XMLText) bool
- type PrintOptions
- type XMLAttribute
- type XMLComment
- type XMLDirective
- type XMLDocument
- type XMLElement
- type XMLHandle
- type XMLNode
- type XMLProcInst
- type XMLText
- type XMLVisitor
Constants ¶
This section is empty.
Variables ¶
var ( // PrintPretty 预制的打印选项,采用4个空格缩进 PrintPretty = PrintOptions{Indent: []byte(" "), TextWrapWidth: 200} // PrintStream 流式打印选项,不缩进,不换行,节省流量 PrintStream = PrintOptions{} )
Functions ¶
func EscapeAttribute ¶
EscapeAttribute 对XMLElement中的属性值进行转义,常用于自定义文档输出格式
func SaveDocument ¶
func SaveDocument(doc XMLDocument, writer io.Writer, options PrintOptions) error
SaveDocumentToFile Print the xml-dom objects to the writer.
func SaveDocumentToFile ¶
func SaveDocumentToFile(doc XMLDocument, name string, options PrintOptions) error
SaveDocumentToFile Print the xml-dom objects to the file.
Types ¶
type DefaultVisitor ¶
type DefaultVisitor struct {
EnterDocument func(XMLDocument) bool
ExitDocument func(XMLDocument) bool
EnterElement func(XMLElement) bool
ExitElement func(XMLElement) bool
ProcInst func(XMLProcInst) bool
Text func(XMLText) bool
Comment func(XMLComment) bool
Directive func(XMLDirective) bool
}
DefaultVisitor 这个类的目的是简化编写定制扫描的visitor,使得我们不需要定制XMLVisitor的所有接口
func (*DefaultVisitor) VisitComment ¶
func (v *DefaultVisitor) VisitComment(c XMLComment) bool
VisitComment is the default implement of XMLVisitor
func (*DefaultVisitor) VisitDirective ¶
func (v *DefaultVisitor) VisitDirective(d XMLDirective) bool
VisitDirective is the default implement of XMLVisitor
func (*DefaultVisitor) VisitEnterDocument ¶
func (v *DefaultVisitor) VisitEnterDocument(doc XMLDocument) bool
VisitEnterDocument is the default implement of XMLVisitor
func (*DefaultVisitor) VisitEnterElement ¶
func (v *DefaultVisitor) VisitEnterElement(elem XMLElement) bool
VisitEnterElement is the default implement of XMLVisitor
func (*DefaultVisitor) VisitExitDocument ¶
func (v *DefaultVisitor) VisitExitDocument(doc XMLDocument) bool
VisitExitDocument is the default implement of XMLVisitor
func (*DefaultVisitor) VisitExitElement ¶
func (v *DefaultVisitor) VisitExitElement(elem XMLElement) bool
VisitExitElement is the default implement of XMLVisitor
func (*DefaultVisitor) VisitProcInst ¶
func (v *DefaultVisitor) VisitProcInst(pi XMLProcInst) bool
VisitProcInst is the default implement of XMLVisitor
func (*DefaultVisitor) VisitText ¶
func (v *DefaultVisitor) VisitText(text XMLText) bool
VisitText is the default implement of XMLVisitor
type PrintOptions ¶
type PrintOptions struct {
Indent []byte // 缩进前缀,只允许填写tab或者空白,如果Indent长度为0表示折行但是不缩进,如果Indent为null表示不折行
TextWrapWidth int // 超过多长才强制换行
}
PrintOptions 打印选项,用于NewSimplePrinter函数,用于控制输出的XML内容的样式
type XMLComment ¶
XMLComment 提供了对注释的封装
type XMLDirective ¶
type XMLDirective interface {
XMLNode
}
XMLDirective 用于表达`<!`与`>`之间的部分,一般为DTD
func NewDirective ¶
func NewDirective(directive string) XMLDirective
NewDirective 创建一个新的XMLDirective对象
type XMLDocument ¶
type XMLDocument interface {
XMLNode
}
XMLDocument 用于表达一个XML文档,这是整个XML文档的根
func LoadDocument ¶
func LoadDocument(rd io.Reader) (XMLDocument, error)
LoadDocument 从rd流中读取XML码流并构建成XMLDocument对象
func LoadDocumentFromFile ¶
func LoadDocumentFromFile(name string) (XMLDocument, error)
type XMLElement ¶
type XMLElement interface {
XMLNode
Name() string
SetName(name string)
FindAttribute(name string) XMLAttribute
ForeachAttribute(callback func(attribute XMLAttribute) int) int
AttributeCount() int
Attribute(name string, def string) string
SetAttribute(name string, value string) XMLAttribute
DeleteAttribute(name string) XMLAttribute
ClearAttributes()
Text() string
SetText(text string)
}
XMLElement 提供了访问XML基本节点元素的能力
Name、SetName其实是Value和SetValue的别名,目的是为了使得接口更加符合直观理解。
Text、SetText的作用是设置<node>与</node>之间的文字,虽然文字都是有XMLText对象来承载的,但是通常来说直接在XMLElement中访问会更加方便。
FindAttribute和ForeachAttribute分别用于查找特定的XML节点的属性和遍历XML属性列表。
Attribute、SetAttribute、DeleteAttribute用于读取和删除属性。
type XMLHandle ¶
type XMLHandle interface {
Parent() XMLHandle
FirstChild() XMLHandle
LastChild() XMLHandle
Prev() XMLHandle
Next() XMLHandle
FirstChildElement(name string) XMLHandle
LastChildElement(name string) XMLHandle
PrevElement(name string) XMLHandle
NextElement(name string) XMLHandle
ToNode() XMLNode
ToElement() XMLElement
ToText() XMLText
ToComment() XMLComment
ToDocument() XMLDocument
ToProcInst() XMLProcInst
ToDirective() XMLDirective
}
XMLHandle XML文档处理器,其主要
type XMLNode ¶
type XMLNode interface {
ToElement() XMLElement
ToText() XMLText
ToComment() XMLComment
ToDocument() XMLDocument
ToProcInst() XMLProcInst
ToDirective() XMLDirective
Value() string
SetValue(newValue string)
Document() XMLDocument
NoChildren() bool
Parent() XMLNode
FirstChild() XMLNode
LastChild() XMLNode
Prev() XMLNode
Next() XMLNode
FirstChildElement(name string) XMLElement
LastChildElement(name string) XMLElement
PrevElement(name string) XMLElement
NextElement(name string) XMLElement
InsertBack(node XMLNode) XMLNode
InsertFront(node XMLNode) XMLNode
InsertEndChild(node XMLNode) XMLNode
InsertFirstChild(node XMLNode) XMLNode
InsertElementBack(name string) XMLElement
InsertElementFront(name string) XMLElement
InsertElementEndChild(name string) XMLElement
InsertElementFirstChild(name string) XMLElement
DeleteChildren()
DeleteChild(node XMLNode)
Split() XMLNode
Accept(visitor XMLVisitor) bool
// contains filtered or unexported methods
}
XMLNode 定义了XML所有节点的基础设施,提供了基本的元素遍历、增删等操作,也提供了逆向转换能力.
type XMLProcInst ¶
XMLProcInst 常用于表达XML处理指令,类似:<?xml version="1.0" encoding="UTF-8"?>
func NewProcInst ¶
func NewProcInst(target string, inst string) XMLProcInst
NewProcInst 创建一个新的XMLProcInst对象
type XMLText ¶
XMLText 提供了对XML元素间文本的封装
type XMLVisitor ¶
type XMLVisitor interface {
VisitEnterDocument(XMLDocument) bool
VisitExitDocument(XMLDocument) bool
VisitEnterElement(XMLElement) bool
VisitExitElement(XMLElement) bool
VisitProcInst(XMLProcInst) bool
VisitText(XMLText) bool
VisitComment(XMLComment) bool
VisitDirective(XMLDirective) bool
}
XMLVisitor XML文档访问器,常用于遍历文档或者格式化输出XML文档
func NewSimplePrinter ¶
func NewSimplePrinter(writer io.Writer, options PrintOptions) XMLVisitor
NewSimplePrinter 创建一个简单XML文档输出函数
Source Files
¶
- tinydom.go