xmlvalue

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2019 License: BSD-3-Clause Imports: 6 Imported by: 0

README

xmlvalue

Build Status Coverage Status Go Report Card GoDoc Latest

xmlvalue is a Golang package for XML parsing. It is used in situations those Go structures cannot achieve. Most of the usages are as well as jsonvalue.

License

License

Documentation

Overview

Package xmlvalue is for XML parsing. It is used in situations those Go structures cannot achieve well. Most of the usages are quite simular as jsonvalue. (https://github.com/Andrew-M-C/go.jsonvalue)

This package is particularly useful in following situations:

1. Rapidly create an XML document with mutiple level.

2. Parsing XML documents with volatile structures.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Opt

type Opt struct {
	// Indent defines the indent string when marshaling. If indent string is empty, new line will not be added when marshaling.
	Indent string
}

Opt provide optional preferances when marshaling the value

type S

type S struct {
	// contains filtered or unexported fields
}

S structures is a internal structure for SetXxx functions

func (*S) At

func (set *S) At(params ...interface{}) error

At sets string to specified path. For detail usage and examples, pleas refer to Set() or Add() functions

type V

type V struct {
	// contains filtered or unexported fields
}

V represents an XML value

func New

func New(name string) *V

New returns a initialized XML value with given name

func Unmarshal

func Unmarshal(b []byte) (*V, error)

Unmarshal parses a given data and returns an xmlvalue object

func UnmarshalString

func UnmarshalString(s string) (*V, error)

UnmarshalString is the same as Unmarshal(), but takes string as parameter

func (*V) AddInt

func (x *V) AddInt(i int) *S

AddInt starts adding a integer to specified path

func (*V) AddString

func (x *V) AddString(s string) *S

AddString starts adding a string to specified path

Example
package main

import (
	"fmt"

	xmlvalue "github.com/Andrew-M-C/go.xmlvalue"
)

func main() {
	x := xmlvalue.New("xml")
	x.AddString("Hello, world!").At("data", "message")
	x.AddString("Hello, world again!").At("data", "message")
	fmt.Println(x.MustMarshalString(xmlvalue.Opt{Indent: "  "}))
}
Output:

<xml>
  <data>
    <message>Hello, world!</message>
    <message>Hello, world again!</message>
  </data>
</xml>

func (*V) Get

func (x *V) Get(params ...interface{}) (*V, error)

Get returns child in xml value

func (*V) GetAttr

func (x *V) GetAttr(a string, defaultValue ...string) (attr string, exist bool)

GetAttr read attribute. If a default value given, the default attribute will be set and returned when the attribute does not exist.

func (*V) GetInt

func (x *V) GetInt(params ...interface{}) (int, error)

GetInt returns represented integer of text in specified xml value

func (*V) GetString

func (x *V) GetString(params ...interface{}) (string, error)

GetString returns child text in xml value

func (*V) Marshal

func (x *V) Marshal(opt ...Opt) ([]byte, error)

Marshal convert xml values into data in UTF-8 encoding. It only returns error when this XML value is nil or not generated by New() function.

func (*V) MarshalString

func (x *V) MarshalString(opt ...Opt) (string, error)

MarshalString is same as Marshal(), but returns string instead

func (*V) MustMarshalString

func (x *V) MustMarshalString(opt ...Opt) string

MustMarshalString is same as MarshalString(), but panics if error occured.

func (*V) Name

func (x *V) Name() string

Name returns the name of this XML element

func (*V) SetAttr

func (x *V) SetAttr(a, v string)

SetAttr sets attribute

func (*V) SetInt

func (x *V) SetInt(i int) *S

SetInt starts setting a integer to specified path

func (*V) SetString

func (x *V) SetString(s string) *S

SetString starts setting a string to specified path

Example
package main

import (
	"fmt"

	xmlvalue "github.com/Andrew-M-C/go.xmlvalue"
)

func main() {
	x := xmlvalue.New("xml")
	x.SetString("Hello, world!").At("data", "message")
	fmt.Println(x.MustMarshalString(xmlvalue.Opt{Indent: "  "}))
}
Output:

<xml>
  <data>
    <message>Hello, world!</message>
  </data>
</xml>

func (*V) SetText

func (x *V) SetText(t interface{}) error

SetText set raw text of this XML element. Both string and []byte type are accepted

Example
package main

import (
	"fmt"

	xmlvalue "github.com/Andrew-M-C/go.xmlvalue"
)

func main() {
	x := xmlvalue.New("xml")
	x.SetText("Hello, XML!")
	fmt.Println(x.MustMarshalString())
}
Output:

<xml>Hello, XML!</xml>

func (*V) Text

func (x *V) Text() string

Text returns the text string of this XML element

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL