Convert xml and json to go struct

xj2go

Go Report Card Build Status codecov codebeat badge

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 json/xml] [-p sample] [-r result] sample.json

Import it in your code:

import "github.com/wk30/xj2go"

Example

Please see the example file.

package main

import (
	"io/ioutil"
	"log"

	"github.com/wk30/xj2go"
)

func main() {
	xmlfilename := "../testxml/xl/styles.xml"
	xj1 := xj2go.New(xmlfilename, "demoxml", "")
	xj1.XMLToGo()

	b1, err := ioutil.ReadFile(xmlfilename)
	if err != nil {
		log.Fatal(err)
	}

	if err := xj2go.XMLBytesToGo("test", "demoxml2", &b1); err != nil {
		log.Fatal(err)
	}

	jsonfilename := "../testjson/githubAPI.json"
	xj2 := xj2go.New(jsonfilename, "demojson", "sample")
	xj2.JSONToGo()

	b2, err := ioutil.ReadFile(jsonfilename)
	if err != nil {
		log.Fatal(err)
	}

	if err := xj2go.JSONBytesToGo("test", "demojson2", "github", &b2); err != nil {
		log.Fatal(err)
	}
}
Similar Resources

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

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

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

Freestyle xml parser with golang

fxml - FreeStyle XML Parser This package provides a simple parser which reads a XML document and output a tree structure, which does not need a pre-de

Jul 1, 2022

🧑‍💻 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

⚙️ Convert HTML to Markdown. Even works with entire websites and can be extended through rules.

⚙️ Convert HTML to Markdown. Even works with entire websites and can be extended through rules.

html-to-markdown Convert HTML into Markdown with Go. It is using an HTML Parser to avoid the use of regexp as much as possible. That should prevent so

Jan 6, 2023

Convert scanned image PDF file to text annotated PDF file

Convert scanned image PDF file to text annotated PDF file

Jisui (自炊) This tool is PoC (Proof of Concept). Jisui is a helper tool to create e-book. Ordinary the scanned book have not text information, so you c

Dec 11, 2022

Convert Microsoft Word Document to Markdown

Convert Microsoft Word Document to Markdown

docx2md Convert Microsoft Word Document to Markdown Usage $ docx2md NewDocument.docx Installation $ go get github.com/mattn/docx2md Supported Styles

Jan 4, 2023
Comments
  • Not able to install this tool.

    Not able to install this tool.

    Hi,

    When I try to install this tool, I am not able to do so.. I even cloned this code and tried running the sample file but same issue occurred. Let me know the solution for it

    go get -u -v github.com/wk30/xj2go/cmd/...

    verifying github.com/wk30/[email protected]/go.mod: checksum mismatch downloaded: h1:WLVuEQ21+AMkIXTaAfOTIIpJ+cYmXdi5ocziIfKhjVg= go.sum: h1:AzoaAEzQwR7h97hUbt9kPWYG+jwD7GbxS9C1YOxYbYA=

    SECURITY ERROR This download does NOT match an earlier download recorded in go.sum. The bits may have been replaced on the origin server, or an attacker may have intercepted the download attempt.

    For more information, see 'go help module-auth'.

  • conversion error

    conversion error

    test.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <DomainDNSGetListResult Domain="domain.com" IsUsingOurDNS="true">
      <Nameserver>dns1.domain.com</Nameserver>
      <Nameserver>dns2.domain.com</Nameserver>
    </DomainDNSGetListResult>
    
    func main() {
    	xj1 := xj2go.New("test.xml", "demoxml", "")
    	xj1.XMLToGo()
    }
    

    out

    package demoxml
    
    type DomainDNSGetListResult struct {
    	IsUsingOurDNS string `xml:"IsUsingOurDNS,attr"`
    	Nameserver    string `xml:"Nameserver"`
    	Domain        string `xml:"Domain,attr"`
    }
    type Nameserver struct {
    	string `xml:""`
    }
    
  • The node has attr have not text content.

    The node has attr have not text content.

    I have a xml like this:

    <A>
        <B>some text...</B>
        <C ID="1">some text...</C>
        <D ID="2">
            <E>some text...</E>
        </D>
    </A>
    

    the result:

    type A struct {
    	B string `xml:"B"`
    	C C      `xml:"C,attr"`
    	D D      `xml:"D,attr"`
    }
    type C struct {
    	ID string `xml:"ID,attr"`
    }
    type D struct {
    	ID string `xml:"ID,attr"`
    	E  string `xml:"E"`
    }
    
    

    Node "C" have attribute "ID" and some text content, but the struct "C" only has attribute "ID" and have not text content.

  • Setup Airbrake for your Go application

    Setup Airbrake for your Go application

    Example Usage

    To include Airbrake in your GoLang application you'll need to import the Gobrake code, available at https://github.com/airbrake/gobrake (You can find your project ID and API KEY with your project's settings):

    package main
    
    import (
        "errors"
    
        "github.com/airbrake/gobrake"
    )
    
    
    var airbrake = gobrake.NewNotifier(<Your project ID>, "<Your project API KEY>")
    
    
    func init() {
        airbrake.AddFilter(func(notice *gobrake.Notice) *gobrake.Notice {
            notice.Context["environment"] = "production"
            return notice
        })
    }
    
    func main() {
        defer airbrake.Close()
        defer airbrake.NotifyOnPanic()
    
        airbrake.Notify(errors.New("operation failed"), nil)
    }
    

    Check out our official GitHub repo for info on advanced features like ignoring errors and setting error severity.

Match regex group into go struct using struct tags and automatic parsing

regroup Simple library to match regex expression named groups into go struct using struct tags and automatic parsing Installing go get github.com/oris

Nov 5, 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
omniparser: a native Golang ETL streaming parser and transform library for CSV, JSON, XML, EDI, text, etc.
omniparser: a native Golang ETL streaming parser and transform library for CSV, JSON, XML, EDI, text, etc.

omniparser Omniparser is a native Golang ETL parser that ingests input data of various formats (CSV, txt, fixed length/width, XML, EDI/X12/EDIFACT, JS

Jan 4, 2023
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
Easily to convert JSON data to Markdown Table

Easily to convert JSON data to Markdown Table

Oct 28, 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
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
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