Freestyle xml parser with golang

fxml - FreeStyle XML Parser

godoc

This package provides a simple parser which reads a XML document and output a tree structure, which does not need a pre-defined struct, hence the name "FreeStyle".

See godoc for more information.

Similar Resources

🧑‍💻 Go XML generator without Structs™

exml 🧑‍💻 Go XML generator without Structs™ Package exml allows XML documents to be generated without the usage of structs or maps. It is not intende

Nov 15, 2022

wikipedia-jsonl is a CLI that converts Wikipedia dump XML to JSON Lines format.

wikipedia-jsonl wikipedia-jsonl is a CLI that converts Wikipedia dump XML to JSON Lines format. How to use At first, download the XML dump from Wikime

Dec 26, 2022

Sqly - An easy-to-use extension for sqlx, base on xml files and named query/exec

sqly An easy-to-use extension for sqlx ,base on xml files and named query/exec t

Jun 12, 2022

TOML parser for Golang with reflection.

THIS PROJECT IS UNMAINTAINED The last commit to this repo before writing this message occurred over two years ago. While it was never my intention to

Dec 30, 2022

User agent string parser in golang

User agent parsing useragent is a library written in golang to parse user agent strings. Usage First install the library with: go get xojoc.pw/userage

Aug 2, 2021

A simple json parser built using golang

jsonparser A simple json parser built using golang Installation: go get -u githu

Dec 29, 2021

A NMEA parser library in pure Go

go-nmea This is a NMEA library for the Go programming language (Golang). Features Parse individual NMEA 0183 sentences Support for sentences with NMEA

Dec 20, 2022

A shell parser, formatter, and interpreter with bash support; includes shfmt

sh A shell parser, formatter, and interpreter. Supports POSIX Shell, Bash, and mksh. Requires Go 1.14 or later. Quick start To parse shell scripts, in

Dec 29, 2022

A simple CSS parser and inliner in Go

douceur A simple CSS parser and inliner in Golang. Parser is vaguely inspired by CSS Syntax Module Level 3 and corresponding JS parser. Inliner only p

Dec 12, 2022
Comments
  • 900 XML/SVG Test cases, one failed

    900 XML/SVG Test cases, one failed

    I have a file that I ran through the function/example I sent to you on my PR:

    <?xml version="1.0" encoding="utf-8"?>
    <!-- Generator: Adobe Illustrator 23.0.4, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
    <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="24px"
    	 height="24px" viewBox="0 0 24 24" enable-background="new 0 0 24 24" xml:space="preserve">
    <g id="Bounding_Box">
    	<rect fill="none" width="24" height="24"/>
    </g>
    <g id="Icons">
    	<g>
    		<g>
    			<path d="M2.5,19h19v2h-19V19z M19.34,15.85c0.8,0.21,1.62-0.26,1.84-1.06c0.21-0.8-0.26-1.62-1.06-1.84l-5.31-1.42l-2.76-9.02
    				L10.12,2v8.28L5.15,8.95L4.22,6.63L2.77,6.24v5.17L19.34,15.85z"/>
    		</g>
    	</g>
    </g>
    </svg>
    

    When it called Encode on it this was the output:

    <?xml version="1.0" encoding="UTF-8"?><><><!--Generator: Adobe Illustrator 23.0.4, SVG Export Plug-In . SVG Version: 6.00 Build 0)--></><svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="24px" height="24px" viewBox="0 0 24 24" enable-background="new 0 0 24 24" xml:space="preserve"><g id="Bounding_Box"><rect fill="none" width="24" height="24"></rect></g><g id="Icons"><g><g><path d="M2.5,19h19v2h-19V19z M19.34,15.85c0.8,0.21,1.62-0.26,1.84-1.06c0.21-0.8-0.26-1.62-1.06-1.84l-5.31-1.42l-2.76-9.02&#xA;&#x9;&#x9;&#x9;&#x9;L10.12,2v8.28L5.15,8.95L4.22,6.63L2.77,6.24v5.17L19.34,15.85z" fill="#f44336"></path></g></g></g></svg></>
    

    Notice the <> and the </> are invalid XML. I even just did a Parse and then an Encode and got the same results, so its not my function for WalkMutate at all its a bug elsewhere:

    				xt, err := fxml.Parse(f)
    				if err != nil {
    					return
    				}
    
    				var buf2 bytes.Buffer
    				err = xt.Encode(&buf2, true)
    				if err != nil { 
    					return
    				}
    				xmlString2 := buf2.String()
    				session_functions.IsolatedDump(xmlString2)
    

    So as a hack I am just replacing these with nothing and it fixed this one icon until you can get back with me on why you think it's parsing it incorrectly after it encodes it:

    				var buf bytes.Buffer
    				err = newXML.Encode(&buf, true)
    				if err != nil { 
    					return
    				}
    				xmlString := buf.String()
    				xmlString = strings.Replace(strings.Replace(xmlString, "</>", "", -1), "<>", "", -1)
    				data = []byte(xmlString)
    
  • Add a method to walk and mutate changes

    Add a method to walk and mutate changes

    Callers are expected to return x as well as the byte for whether to continue or stop

    Example below can be added to docs later if you want:

    	var filename = "forward_10-24px.svg"
    	f, err := os.Open(filename)
    	if err != nil {
    		return
    	}
    	defer f.Close()
    	xt, err := fxml.Parse(f)
    	if err != nil {
    		return
    	}
    	newXML := xt.WalkMutate(func(ni fxml.XNodInfo, x *fxml.XMLTree) (fxml.XWalkResult, *fxml.XMLTree) {
    		if ni.Path[len(ni.Path)-1] == "path" {
    			addFill := true
    			for idx, _ := range x.Attr {
    				if x.Attr[idx].Name.Local == "fill" && x.Attr[idx].Value == "none" {
    					addFill = false
    				} else if x.Attr[idx].Name.Local == "fill" {
    					x.Attr[idx].Value = "red"
    					addFill = false
    				}
    			}
    			if addFill {
    				x.Attr = append(x.Attr, xml.Attr{
    					Name: xml.Name{
    						Local: "fill",
    					},
    					Value: "red",
    				})
    			}
    		}
    		if ni.Path[len(ni.Path)-1] == "circle" {
    			addFill := true
    			for idx, _ := range x.Attr {
    				if x.Attr[idx].Name.Local == "fill" && x.Attr[idx].Value == "none" {
    					addFill = false
    				} else if x.Attr[idx].Name.Local == "fill" {
    					x.Attr[idx].Value = "red"
    					addFill = false
    				}
    			}
    			if addFill {
    				x.Attr = append(x.Attr, xml.Attr{
    					Name: xml.Name{
    						Local: "fill",
    					},
    					Value: "red",
    				})
    			}
    		}
    
    		if ni.Path[len(ni.Path)-1] == "polygon" {
    			x.Attr = append(x.Attr, xml.Attr{
    				Name: xml.Name{
    					Local: "style",
    				},
    				Value: "fill: red;",
    			})
    		}
    
    		if ni.Path[len(ni.Path)-1] == "rect" {
    			addFill := true
    			for idx, _ := range x.Attr {
    				if x.Attr[idx].Name.Local == "fill" && x.Attr[idx].Value == "none" {
    					addFill = false
    				} else if x.Attr[idx].Name.Local == "fill" {
    					x.Attr[idx].Value = "red"
    					addFill = false
    				}
    			}
    			if addFill {
    				x.Attr = append(x.Attr, xml.Attr{
    					Name: xml.Name{
    						Local: "style",
    					},
    					Value: "fill: red;",
    				})
    			}
    		}
    		return fxml.WRCont, x
    	})
    	newXML.Encode(os.Stdout, true)
    

    Here is the forward svg file:

    <?xml version="1.0" encoding="utf-8"?>
    <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="24px"
    	 height="24px" viewBox="0 0 24 24" enable-background="new 0 0 24 24" xml:space="preserve">
    <g id="Bounding_Box">
    	<rect fill="none" width="24" height="24"/>
    </g>
    <g id="Icons">
    	<g>
    		<path d="M18,13c0,3.31-2.69,6-6,6s-6-2.69-6-6s2.69-6,6-6v4l5-5l-5-5v4c-4.42,0-8,3.58-8,8c0,4.42,3.58,8,8,8s8-3.58,8-8H18z"/>
    		<polygon points="10.86,15.94 10.86,11.67 10.77,11.67 9,12.3 9,12.99 10.01,12.68 10.01,15.94 		"/>
    		<path d="M12.25,13.44v0.74c0,1.9,1.31,1.82,1.44,1.82c0.14,0,1.44,0.09,1.44-1.82v-0.74c0-1.9-1.31-1.82-1.44-1.82
    			C13.55,11.62,12.25,11.53,12.25,13.44z M14.29,13.32v0.97c0,0.77-0.21,1.03-0.59,1.03c-0.38,0-0.6-0.26-0.6-1.03v-0.97
    			c0-0.75,0.22-1.01,0.59-1.01C14.07,12.3,14.29,12.57,14.29,13.32z"/>
    	</g>
    </g>
    </svg>
    
    

    Also added a bug fix for the lower cased utf-8 example.

    Nice job on this. Super useful package for parsing SVGs which I needed to do here.

Related tags
Quick and simple parser for PFSense XML configuration files, good for auditing firewall rules

pfcfg-parser version 0.0.1 : 13 January 2022 A quick and simple parser for PFSense XML configuration files to generate a plain text file of the main c

Jan 13, 2022
xmlquery is Golang XPath package for XML query.

xmlquery Overview xmlquery is an XPath query package for XML documents, allowing you to extract data or evaluate from XML documents with an XPath expr

Jan 1, 2023
This package provides Go (golang) types and helper functions to do some basic but useful things with mxGraph diagrams in XML, which is most famously used by app.diagrams.net, the new name of draw.io.

Go Draw - Golang MX This package provides types and helper functions to do some basic but useful things with mxGraph diagrams in XML, which is most fa

Aug 30, 2022
Decode / encode XML to/from map[string]interface{} (or JSON); extract values with dot-notation paths and wildcards. Replaces x2j and j2x packages.

mxj - to/from maps, XML and JSON Decode/encode XML to/from map[string]interface{} (or JSON) values, and extract/modify values from maps by key or key-

Dec 29, 2022
Convert xml and json to go struct

xj2go The goal is to convert xml or json file to go struct file. Usage Download and install it: $ go get -u -v github.com/wk30/xj2go/cmd/... $ xj [-t

Oct 23, 2022
parse and generate XML easily in go

etree The etree package is a lightweight, pure go package that expresses XML in the form of an element tree. Its design was inspired by the Python Ele

Dec 19, 2022
'go test' runner with output optimized for humans, JUnit XML for CI integration, and a summary of the test results.
'go test' runner with output optimized for humans, JUnit XML for CI integration, and a summary of the test results.

gotestsum gotestsum runs tests using go test --json, prints formatted test output, and a summary of the test run. It is designed to work well for both

Dec 28, 2022
Go XML sitemap and sitemap index generator

Install go get github.com/turk/go-sitemap Example for sitemapindex func () main(c *gin.Context) { s := sitemap.NewSitemapIndex(c.Writer, true)

Jun 29, 2022
Extraction politique de conformité : xlsx (fichier de suivi) -> xml (format AlgoSec)

go_policyExtractor Extraction politique de conformité : xlsx (fichier de suivi) -> xml (format AlgoSec). Le programme suivant se base sur les intitulé

Nov 4, 2021
axmlfmt is an opinionated formatter for Android XML resources

axmlfmt axmlfmt is an opinionated formatter for Android XML resources. It takes XML that looks like <?xml version="1.0" encoding="utf-8"?> <LinearLayo

May 14, 2022