Documentation
¶
Overview ¶
Package xmlctx provides a namespace-aware XML decoder for Go.
The standard encoding/xml package can decode namespaced XML, but it cannot properly match namespace-aware struct tags because it resolves prefixes to full URIs in StartElement.Name.Space, while struct tags use prefixes.
This package solves this problem by allowing you to specify a namespace context that maps prefixes (used in struct tags) to their full namespace URIs. The decoder then matches XML elements based on their namespace URI, regardless of what prefix is used in the actual XML document.
Note: This package is for decoding/unmarshaling XML only. For marshaling structs to XML, use the standard encoding/xml package.
Example usage:
type Person struct {
Name string `xml:"name"`
Email string `xml:"addr:email"`
}
decoder := xmlctx.NewDecoder(
bytes.NewReader(xmlData),
xmlctx.WithNamespaces(map[string]string{
"": "http://example.com/user",
"addr": "http://example.com/address",
}),
)
err := decoder.Decode(&person)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder wraps xml.Decoder with namespace context awareness
func NewDecoder ¶
NewDecoder creates a new namespace-aware decoder
type Option ¶
type Option func(*Decoder)
Option is a functional option for configuring the Decoder
func WithNamespaces ¶
WithNamespaces sets the namespace mappings for the decoder The map keys are prefixes used in Go struct tags (e.g., "ns1", "ns2", "") The map values are the full namespace URIs (e.g., "http://example.com/schema/profile")