Documentation
¶
Index ¶
- func FiltersOf(xpaths ...string) (*option.Filters, error)
- func NewFilters(xpaths ...string) ([]*option.Filter, error)
- type Attribute
- type AttributeSelector
- type AttributesChangesSize
- type Buffer
- type ComparisonToken
- type DOM
- type Document
- type Element
- func (e *Element) AddAttribute(key string, value string)
- func (e *Element) AddElement(value string)
- func (e *Element) Attribute(attribute string) (*Attribute, bool)
- func (e *Element) InsertAfter(element string)
- func (e *Element) InsertBefore(element string)
- func (e *Element) ReplaceWith(newElement string)
- func (e *Element) SetAttribute(key, value string)
- func (e *Element) SetValue(value string)
- func (e *Element) Value() string
- type ElementsChangesSize
- type Iterator
- type Selector
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Attribute ¶
type Attribute struct {
// contains filtered or unexported fields
}
Attribute represents XML element attribute
type AttributeSelector ¶
type AttributeSelector struct { Name string Value string Compare ComparisonToken }
AttributeSelector matches Element by Attribute name and value
type AttributesChangesSize ¶
type AttributesChangesSize int
type Buffer ¶
type Buffer struct {
// contains filtered or unexported fields
}
Buffer hold the current XML value
type ComparisonToken ¶
type ComparisonToken string
const ( EQ ComparisonToken = "=" NEQ ComparisonToken = "!=" )
type DOM ¶
type DOM struct {
// contains filtered or unexported fields
}
DOM represents DOM structure
func New ¶
New creates new VirtualDOM
Example ¶
package main import ( "fmt" "github.com/viant/dm/option" "github.com/viant/dm/xml" "strings" ) func main() { template := ` <?xml version="1.0" encoding="UTF-8"?> <foo test="true"> <id>1</id> <name>foo name</name> <address> <street>abc</street> <zip-code>123456</zip-code> <country> <id>1</id> <name>def</name> </country> </address> <quantity>123</quantity> <price>50.5</price> <type>fType</type> </foo>` filters := option.NewFilters( option.NewFilter("foo", "test"), option.NewFilter("id"), option.NewFilter("name"), option.NewFilter("address"), ) vdom, err := xml.New(template, filters) if err != nil { fmt.Println(err) return } dom := vdom.Document() elem, ok := dom.SelectFirst(xml.Selector{Name: "foo"}, xml.Selector{Name: "id"}) if ok { elem.SetValue("10") } elem, ok = dom.SelectFirst(xml.Selector{Name: "foo"}, xml.Selector{Name: "address"}) if ok { elem.SetValue("") elem.AddElement("<new-elem>New element value</new-elem>") } elemIt := dom.Select(xml.Selector{Name: "foo", Attributes: []xml.AttributeSelector{{Name: "test", Value: "true"}}}) for elemIt.Has() { elem, _ := elemIt.Next() elem.AddAttribute("attr1", "value1") attribute, ok := elem.Attribute("test") if !ok { continue } attribute.Set(strings.ToUpper(attribute.Value())) } result := dom.Render() fmt.Println(result) }
Output: <?xml version="1.0" encoding="UTF-8"?> <foo test="TRUE" attr1="value1"> <id>10</id> <name>foo name</name> <address><new-elem>New element value</new-elem></address> <quantity>123</quantity> <price>50.5</price> <type>fType</type> </foo>
type Document ¶
type Document struct {
// contains filtered or unexported fields
}
Document modifies the VirtualDOM
type Element ¶
type Element struct {
// contains filtered or unexported fields
}
Element represents XML Element
func (*Element) AddAttribute ¶
AddAttribute adds new Attribute
func (*Element) AddElement ¶
AddElement adds new Element value
func (*Element) InsertAfter ¶
InsertAfter inserts new element after receiver
func (*Element) InsertBefore ¶
InsertBefore inserts new element before receiver
func (*Element) ReplaceWith ¶
func (*Element) SetAttribute ¶
SetAttribute update attribute value, or creates new one if it doesn't exist.
type ElementsChangesSize ¶
type ElementsChangesSize int
type Iterator ¶
type Iterator struct {
// contains filtered or unexported fields
}
Iterator iterates over matching Elements
type Selector ¶
type Selector struct { Name string Attributes []AttributeSelector MatchAny bool }
Selector matches Element by name
func NewSelectors ¶
NewSelectors creates Selectors from xpath
Click to show internal directories.
Click to hide internal directories.