xmlquery

package module
v0.0.0-...-f3845cf Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2017 License: MIT Imports: 8 Imported by: 0

README

xmlquery

Build Status Coverage Status GoDoc Go Report Card

xmlquery is an XML parser thats builds a read/modified DOM and supports XPath feature to extract data from XML documents using XPath expression.

Its depend on xpath package.

Installation

$ go get github.com/antchfx/xmlquery

Examples

package main

import (
	"github.com/antchfx/xmlquery"
)

func main() {
	// Load XML document from file.
	f, err := os.Open(`./examples/test.xml`)
	if err != nil {
		panic(err)
	}
	// Parse XML document.
	doc, err := xmlquery.Parse(f)
	if err != nil{
		panic(err)
	}

	// Selects all the `book` elements.
	for _, n := range xmlquery.Find(doc, "//book") {
		fmt.Printf("%s : %s \n", n.SelectAttr("id"), xmlquery.FindOne(n, "title").InnerText())
	}

	// Selects all the `book` elements that have a "id" attribute
	// with a value of "bk104"
	n := xmlquery.FindOne(doc, "//book[@id='bk104']")
	fmt.Printf("%s \n", n.OutputXML(true))
}
Evaluate an XPath expression

Using Evaluate() to evaluates XPath expressions.

expr, err := xpath.Compile("sum(//book/price)")
if err != nil {
	panic(err)
}
fmt.Printf("total price: %f\n", expr.Evaluate(xmlquery.CreateXPathNavigator(doc)).(float64))	

Documentation

Overview

Package xmlquery provides extract data from XML documents using XPath expression.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FindEach

func FindEach(top *Node, expr string, cb func(int, *Node))

FindEach searches the html.Node and calls functions cb.

Types

type Node

type Node struct {
	Parent, FirstChild, LastChild, PrevSibling, NextSibling *Node

	Type         NodeType
	Data         string
	Prefix       string
	NamespaceURI string
	Attr         []xml.Attr
	// contains filtered or unexported fields
}

A Node consists of a NodeType and some Data (tag name for element nodes, content for text) and are part of a tree of Nodes.

func Find

func Find(top *Node, expr string) []*Node

Find searches the Node that matches by the specified XPath expr.

func FindOne

func FindOne(top *Node, expr string) *Node

FindOne searches the Node that matches by the specified XPath expr, and returns first element of matched.

func LoadURL

func LoadURL(url string) (*Node, error)

LoadURL loads the XML document from the specified URL.

func Parse

func Parse(r io.Reader) (*Node, error)

Parse returns the parse tree for the XML from the given Reader.

func (*Node) InnerText

func (n *Node) InnerText() string

InnerText returns the text between the start and end tags of the object.

func (*Node) OutputXML

func (n *Node) OutputXML(self bool) string

OutputXML returns the text that including tags name.

func (*Node) SelectAttr

func (n *Node) SelectAttr(name string) string

SelectAttr returns the attribute value with the specified name.

func (*Node) SelectElement

func (n *Node) SelectElement(name string) *Node

SelectElement finds child elements with the specified name.

func (*Node) SelectElements

func (n *Node) SelectElements(name string) []*Node

SelectElements finds child elements with the specified name.

type NodeNavigator

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

func CreateXPathNavigator

func CreateXPathNavigator(top *Node) *NodeNavigator

CreateXPathNavigator creates a new xpath.NodeNavigator for the specified html.Node.

func (*NodeNavigator) Copy

func (x *NodeNavigator) Copy() xpath.NodeNavigator

func (*NodeNavigator) Current

func (x *NodeNavigator) Current() *Node

func (*NodeNavigator) LocalName

func (x *NodeNavigator) LocalName() string

func (*NodeNavigator) MoveTo

func (x *NodeNavigator) MoveTo(other xpath.NodeNavigator) bool

func (*NodeNavigator) MoveToChild

func (x *NodeNavigator) MoveToChild() bool

func (*NodeNavigator) MoveToFirst

func (x *NodeNavigator) MoveToFirst() bool

func (*NodeNavigator) MoveToNext

func (x *NodeNavigator) MoveToNext() bool

func (*NodeNavigator) MoveToNextAttribute

func (x *NodeNavigator) MoveToNextAttribute() bool

func (*NodeNavigator) MoveToParent

func (x *NodeNavigator) MoveToParent() bool

func (*NodeNavigator) MoveToPrevious

func (x *NodeNavigator) MoveToPrevious() bool

func (*NodeNavigator) MoveToRoot

func (x *NodeNavigator) MoveToRoot()

func (*NodeNavigator) NodeType

func (x *NodeNavigator) NodeType() xpath.NodeType

func (*NodeNavigator) Prefix

func (x *NodeNavigator) Prefix() string

func (*NodeNavigator) String

func (x *NodeNavigator) String() string

func (*NodeNavigator) Value

func (x *NodeNavigator) Value() string

type NodeType

type NodeType uint

A NodeType is the type of a Node.

const (
	// DocumentNode is a document object that, as the root of the document tree,
	// provides access to the entire XML document.
	DocumentNode NodeType = iota
	// DeclarationNode is the document type declaration, indicated by the following
	// tag (for example, <!DOCTYPE...> ).
	DeclarationNode
	// ElementNode is an element (for example, <item> ).
	ElementNode
	// TextNode is the text content of a node.
	TextNode
	// CommentNode a comment (for example, <!-- my comment --> ).
	CommentNode
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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