Giter Site home page Giter Site logo

xquery's Introduction

XQuery

XQuery is a package to extract data from HTML and XML using XPath selectors.

Installing

go get -u github.com/antchfx/xquery

HTML Query Package

This package use golang html package to parse a HTML document.

Method Descript
LoadURL(url string) *html.Node Loads the HTML document from the specified URL
Find(*html.Node, expr string) []*html.Node Searches all the html.Node that matches the specified XPath expression expr
FindOne(*html.Node, expr string) *html.Node Searches the html.Node and returns a first matched node
FindEach(*html.Node, expr string,cb func(int, *html.Node)) Searches all the matched html.Node and to pass its a callback function cb
OutputHTML(*html.Node) string Returns html format output of this html.Node
InnerText(*html.Node) string Returns text without html tag of this html.Node
package main

import (
    "golang.org/x/net/html"
    "github.com/antchfx/xquery/html"	
)

func main() {
	s:=`<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
</body>
</html>`
	root, err := html.Parse(strings.NewReader(s))
	if err != nil {
		panic(err)
	}
	node := htmlquery.FindOne(root, "//title")
	fmt.Println(htmlquery.InnerText(node))	
}

XML Query Package

This package is similar to HTML query package, its implemented load XML document and parseing.

Method Descript
LoadURL(url string) (*Node, error) Loads the XML document from the specified URL
ParseXML(io.Reader) (*Node, error) Parses the specified io.Reader to the XML document.
Find(*Node, expr string) []*Node Searches all the xmlquery.Node that matches the specified XPath expression expr
FindOne(*Node, expr string) *html.Node Searches the xmlquery.Node and returns a first matched node
FindEach(*Node, expr string,cb func(int, *Node)) Searches all the matched Node and to pass its a callback function cb
SelectElements(name string)[]*Node Finds child elements with the specified element name
SelectElement(name string)*Node Finds child elements with the specified element name
SelectAttr(name string)string Returns the attribute value with the specified attribute name
OutputHTML() string Returns html format output of this node
InnerText() string Returns text without xml element tag of this Node
package main

import (
	"github.com/antchfx/xquery/xml"
)

func main() {
	s:=`<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="cooking">
  <title lang="en">Everyday Italian</title>
  <author>Giada De Laurentiis</author>
  <year>2005</year>
  <price>30.00</price>
</book>
......
</bookstore>`
	root, err := xmlquery.ParseXML(strings.NewReader(s))
	if err != nil {
		panic(err)
	}
	node := xmlquery.FindOne(root, "//book[@category='cooking']")
	fmt.Println(node.InnerText())
}

xquery's People

Contributors

zhengchun avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.