{"id":13562247,"url":"https://github.com/beevik/etree","last_synced_at":"2025-05-14T11:07:53.445Z","repository":{"id":43223877,"uuid":"10702097","full_name":"beevik/etree","owner":"beevik","description":"parse and generate XML easily in go","archived":false,"fork":false,"pushed_at":"2025-04-15T03:01:57.000Z","size":316,"stargazers_count":1587,"open_issues_count":1,"forks_count":183,"subscribers_count":23,"default_branch":"main","last_synced_at":"2025-05-07T10:52:34.841Z","etag":null,"topics":["dom","etree","go","path","xml","xml-parser","xpath"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/beevik.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2013-06-15T04:28:59.000Z","updated_at":"2025-05-06T18:29:25.000Z","dependencies_parsed_at":"2025-02-25T12:10:30.184Z","dependency_job_id":"c9a4e827-b1d0-42b8-9b20-ffba08853d83","html_url":"https://github.com/beevik/etree","commit_stats":{"total_commits":186,"total_committers":19,"mean_commits":9.789473684210526,"dds":0.489247311827957,"last_synced_commit":"4fe8b8ce88c0404f65308fbe0bca4ecb845826c5"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beevik%2Fetree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beevik%2Fetree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beevik%2Fetree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beevik%2Fetree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/beevik","download_url":"https://codeload.github.com/beevik/etree/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254129479,"owners_count":22019628,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dom","etree","go","path","xml","xml-parser","xpath"],"created_at":"2024-08-01T13:01:06.236Z","updated_at":"2025-05-14T11:07:53.400Z","avatar_url":"https://github.com/beevik.png","language":"Go","readme":"[![GoDoc](https://godoc.org/github.com/beevik/etree?status.svg)](https://godoc.org/github.com/beevik/etree)\n[![Go](https://github.com/beevik/etree/actions/workflows/go.yml/badge.svg)](https://github.com/beevik/etree/actions/workflows/go.yml)\n\netree\n=====\n\nThe etree package is a lightweight, pure go package that expresses XML in\nthe form of an element tree.  Its design was inspired by the Python\n[ElementTree](http://docs.python.org/2/library/xml.etree.elementtree.html)\nmodule.\n\nSome of the package's capabilities and features:\n\n* Represents XML documents as trees of elements for easy traversal.\n* Imports, serializes, modifies or creates XML documents from scratch.\n* Writes and reads XML to/from files, byte slices, strings and io interfaces.\n* Performs simple or complex searches with lightweight XPath-like query APIs.\n* Auto-indents XML using spaces or tabs for better readability.\n* Implemented in pure go; depends only on standard go libraries.\n* Built on top of the go [encoding/xml](http://golang.org/pkg/encoding/xml)\n  package.\n\n### Creating an XML document\n\nThe following example creates an XML document from scratch using the etree\npackage and outputs its indented contents to stdout.\n```go\ndoc := etree.NewDocument()\ndoc.CreateProcInst(\"xml\", `version=\"1.0\" encoding=\"UTF-8\"`)\ndoc.CreateProcInst(\"xml-stylesheet\", `type=\"text/xsl\" href=\"style.xsl\"`)\n\npeople := doc.CreateElement(\"People\")\npeople.CreateComment(\"These are all known people\")\n\njon := people.CreateElement(\"Person\")\njon.CreateAttr(\"name\", \"Jon\")\n\nsally := people.CreateElement(\"Person\")\nsally.CreateAttr(\"name\", \"Sally\")\n\ndoc.Indent(2)\ndoc.WriteTo(os.Stdout)\n```\n\nOutput:\n```xml\n\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\n\u003c?xml-stylesheet type=\"text/xsl\" href=\"style.xsl\"?\u003e\n\u003cPeople\u003e\n  \u003c!--These are all known people--\u003e\n  \u003cPerson name=\"Jon\"/\u003e\n  \u003cPerson name=\"Sally\"/\u003e\n\u003c/People\u003e\n```\n\n### Reading an XML file\n\nSuppose you have a file on disk called `bookstore.xml` containing the\nfollowing data:\n\n```xml\n\u003cbookstore xmlns:p=\"urn:schemas-books-com:prices\"\u003e\n\n  \u003cbook category=\"COOKING\"\u003e\n    \u003ctitle lang=\"en\"\u003eEveryday Italian\u003c/title\u003e\n    \u003cauthor\u003eGiada De Laurentiis\u003c/author\u003e\n    \u003cyear\u003e2005\u003c/year\u003e\n    \u003cp:price\u003e30.00\u003c/p:price\u003e\n  \u003c/book\u003e\n\n  \u003cbook category=\"CHILDREN\"\u003e\n    \u003ctitle lang=\"en\"\u003eHarry Potter\u003c/title\u003e\n    \u003cauthor\u003eJ K. Rowling\u003c/author\u003e\n    \u003cyear\u003e2005\u003c/year\u003e\n    \u003cp:price\u003e29.99\u003c/p:price\u003e\n  \u003c/book\u003e\n\n  \u003cbook category=\"WEB\"\u003e\n    \u003ctitle lang=\"en\"\u003eXQuery Kick Start\u003c/title\u003e\n    \u003cauthor\u003eJames McGovern\u003c/author\u003e\n    \u003cauthor\u003ePer Bothner\u003c/author\u003e\n    \u003cauthor\u003eKurt Cagle\u003c/author\u003e\n    \u003cauthor\u003eJames Linn\u003c/author\u003e\n    \u003cauthor\u003eVaidyanathan Nagarajan\u003c/author\u003e\n    \u003cyear\u003e2003\u003c/year\u003e\n    \u003cp:price\u003e49.99\u003c/p:price\u003e\n  \u003c/book\u003e\n\n  \u003cbook category=\"WEB\"\u003e\n    \u003ctitle lang=\"en\"\u003eLearning XML\u003c/title\u003e\n    \u003cauthor\u003eErik T. Ray\u003c/author\u003e\n    \u003cyear\u003e2003\u003c/year\u003e\n    \u003cp:price\u003e39.95\u003c/p:price\u003e\n  \u003c/book\u003e\n\n\u003c/bookstore\u003e\n```\n\nThis code reads the file's contents into an etree document.\n```go\ndoc := etree.NewDocument()\nif err := doc.ReadFromFile(\"bookstore.xml\"); err != nil {\n    panic(err)\n}\n```\n\nYou can also read XML from a string, a byte slice, or an `io.Reader`.\n\n### Processing elements and attributes\n\nThis example illustrates several ways to access elements and attributes using\netree selection queries.\n```go\nroot := doc.SelectElement(\"bookstore\")\nfmt.Println(\"ROOT element:\", root.Tag)\n\nfor _, book := range root.SelectElements(\"book\") {\n    fmt.Println(\"CHILD element:\", book.Tag)\n    if title := book.SelectElement(\"title\"); title != nil {\n        lang := title.SelectAttrValue(\"lang\", \"unknown\")\n        fmt.Printf(\"  TITLE: %s (%s)\\n\", title.Text(), lang)\n    }\n    for _, attr := range book.Attr {\n        fmt.Printf(\"  ATTR: %s=%s\\n\", attr.Key, attr.Value)\n    }\n}\n```\nOutput:\n```\nROOT element: bookstore\nCHILD element: book\n  TITLE: Everyday Italian (en)\n  ATTR: category=COOKING\nCHILD element: book\n  TITLE: Harry Potter (en)\n  ATTR: category=CHILDREN\nCHILD element: book\n  TITLE: XQuery Kick Start (en)\n  ATTR: category=WEB\nCHILD element: book\n  TITLE: Learning XML (en)\n  ATTR: category=WEB\n```\n\n### Path queries\n\nThis example uses etree's path functions to select all book titles that fall\ninto the category of 'WEB'.  The double-slash prefix in the path causes the\nsearch for book elements to occur recursively; book elements may appear at any\nlevel of the XML hierarchy.\n```go\nfor _, t := range doc.FindElements(\"//book[@category='WEB']/title\") {\n    fmt.Println(\"Title:\", t.Text())\n}\n```\n\nOutput:\n```\nTitle: XQuery Kick Start\nTitle: Learning XML\n```\n\nThis example finds the first book element under the root bookstore element and\noutputs the tag and text of each of its child elements.\n```go\nfor _, e := range doc.FindElements(\"./bookstore/book[1]/*\") {\n    fmt.Printf(\"%s: %s\\n\", e.Tag, e.Text())\n}\n```\n\nOutput:\n```\ntitle: Everyday Italian\nauthor: Giada De Laurentiis\nyear: 2005\nprice: 30.00\n```\n\nThis example finds all books with a price of 49.99 and outputs their titles.\n```go\npath := etree.MustCompilePath(\"./bookstore/book[p:price='49.99']/title\")\nfor _, e := range doc.FindElementsPath(path) {\n    fmt.Println(e.Text())\n}\n```\n\nOutput:\n```\nXQuery Kick Start\n```\n\nNote that this example uses the FindElementsPath function, which takes as an\nargument a pre-compiled path object. Use precompiled paths when you plan to\nsearch with the same path more than once.\n\n### Other features\n\nThese are just a few examples of the things the etree package can do. See the\n[documentation](http://godoc.org/github.com/beevik/etree) for a complete\ndescription of its capabilities.\n\n### Contributing\n\nThis project accepts contributions. Just fork the repo and submit a pull\nrequest!\n","funding_links":[],"categories":["Misc","开源类库","Repositories","Go","Open source library"],"sub_categories":["文本处理","Word Processing"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeevik%2Fetree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbeevik%2Fetree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeevik%2Fetree/lists"}