Giter Club home page Giter Club logo

xj2s's Introduction

DISCONTINUED!!

XJ2S: A small tool for Golang to generate Golang struct from a xml/json file

The generated struct can be used for a Unmarshal() function from a encoding/xml or encoding/json package

Notice:

This tool is for xml/json data parsing,not for modifing xml/json data struct.

If you want to modify a xml/json data,My recommandation is to use github.com/clbanning/mxj package,xj2sis using this package for backend.

Installation

you should setup your $GOPATH properely
go get github.com/wicast/xj2s/cmd/...

And a executable called xmljson2struct will be appear in your $GOPATH/bin directory.

Usage

Usage: xmljson2struct [-flags] [file]
Flags:
  -f string
    	Parse from a file given a name
  -n	Generate structs whit nesting style
	notice:json haven't implement the > style yet,so only nesting style is working for json
  -root string
    	For struct root name when using json,Default is JsonRoot (default "JsonRoot")
  -t string
    	Type to parse
	avaliable type:xml,json (default "xml")

XML Parsing Example

$ curl -s https://github.com/timeline|xmljson2struct 
type Feed struct {
	TitleEntry	[]Title	`xml:"entry>title"`
	Email	[]string	`xml:"entry>author>email"`
	Id	string	`xml:"id"`
	Title	string	`xml:"title"`
	Content	[]Content	`xml:"entry>content"`
	Published	[]string	`xml:"entry>published"`
	Updated	string	`xml:"updated"`
	IdEntry	[]string	`xml:"entry>id"`
	Thumbnail	[]Thumbnail	`xml:"entry>thumbnail"`
	Xmlns	string	`xml:"xmlns,attr"`
	UpdatedEntry	[]string	`xml:"entry>updated"`
	Name	[]string	`xml:"entry>author>name"`
	Media	string	`xml:"media,attr"`
	Link	[]Link	`xml:"link"`
	LinkEntry	[]Link	`xml:"entry>link"`
	Uri	[]string	`xml:"entry>author>uri"`
	Lang	string	`xml:"lang,attr"`
}

type Link struct {
	Type	string	`xml:"type,attr"`
	Rel	string	`xml:"rel,attr"`
	Href	string	`xml:"href,attr"`
}
type Content struct {
	Type	string	`xml:"type,attr"`
	Text	string	`xml:",chardata"`
}
type LinkEntry struct {
	Type	string	`xml:"type,attr"`
	Rel	string	`xml:"rel,attr"`
	Href	string	`xml:"href,attr"`
}
type TitleEntry struct {
	Type	string	`xml:"type,attr"`
	Text	string	`xml:",chardata"`
}
type Thumbnail struct {
	Url	string	`xml:"url,attr"`
	Height	string	`xml:"height,attr"`
	Width	string	`xml:"width,attr"`
}

The programe will use as less struct as possible,no nesting struct.The first entry is the Root struct.In the Root struct,every field present a useful node in xml.
The rest struct is used for those having attributes.

JSON Parsing Example

Not implemented yet.

TODO

  • XML conflict issue #2.
  • JSON/XML nesting style.
  • Rust support?

xj2s's People

Contributors

wicast avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

xj2s's Issues

Bugs and invalid structs

Hey wicast,

Thanks for the good idea for this tool.
However, I want to parse this XML. I use the command xmljson2struct.exe -f "C:\XXX\com.redhat.rhsa-all.xml" > "C:\XXX\struct.txt", which returns

type Oval_definitions struct {
	Definition	[]Definition	`xml:"definitions>definition"`
	Advisory	[]Advisory	`xml:"definitions>definition>metadata>advisory"`
	Platform	[]string	`xml:"definitions>definition>metadata>affected>platform"`
	Description	[]string	`xml:"definitions>definition>metadata>description"`
	CriteriaCriteriaDefinitionDefinitions	[][]Criteria	`xml:"definitions>definition>criteria>criteria"`
	CriterionCriteriaCriteriaCriteriaDefinitionDefinitions	[][][][]Criterion	`xml:"definitions>definition>criteria>criteria>criteria>criterion"`
	CriterionCriteriaCriteriaDefinitionDefinitions	[][][]Criterion	`xml:"definitions>definition>criteria>criteria>criterion"`
	State	[]State	`xml:"tests>rpminfo_test>state"`
	Xsi	string	`xml:"xsi,attr"`
	Schema_version	string	`xml:"generator>schema_version"`
	Version	[]Version	`xml:"states>rpminfo_state>version"`
	CriteriaCriteriaCriteriaDefinitionDefinitions	[][][]Criteria	`xml:"definitions>definition>criteria>criteria>criteria"`
	CriteriaCriteriaCriteriaCriteriaDefinitionDefinitions	[][][][]Criteria	`xml:"definitions>definition>criteria>criteria>criteria>criteria"`
	CriterionCriteriaCriteriaCriteriaCriteriaDefinitionDefinitions	[][][][][]Criterion	`xml:"definitions>definition>criteria>criteria>criteria>criteria>criterion"`
	Object	[]Object	`xml:"tests>rpminfo_test>object"`
	Red-Def	string	`xml:"red-def,attr"`
	Severity	[]string	`xml:"definitions>definition>metadata>advisory>severity"`
	Reference	[][]Reference	`xml:"definitions>definition>metadata>reference"`
	Timestamp	string	`xml:"generator>timestamp"`
	Name	[]string	`xml:"objects>rpminfo_object>name"`
	Evr	[]Evr	`xml:"states>rpminfo_state>evr"`
	Affected	[]Affected	`xml:"definitions>definition>metadata>affected"`
	Criteria	[]Criteria	`xml:"definitions>definition>criteria"`
	Rpminfo_state	[]Rpminfo_state	`xml:"states>rpminfo_state"`
	Cve	[][]Cve	`xml:"definitions>definition>metadata>advisory>cve"`
	Affected_cpe_list	[]string	`xml:"definitions>definition>metadata>advisory>affected_cpe_list"`
	Xmlns	string	`xml:"xmlns,attr"`
	Product_name	string	`xml:"generator>product_name"`
	Product_version	string	`xml:"generator>product_version"`
	SchemaLocation	string	`xml:"schemaLocation,attr"`
	Updated	[]Updated	`xml:"definitions>definition>metadata>advisory>updated"`
	Criterion	[][]Criterion	`xml:"definitions>definition>criteria>criterion"`
	Unix-Def	string	`xml:"unix-def,attr"`
	Rights	[]string	`xml:"definitions>definition>metadata>advisory>rights"`
	Issued	[]Issued	`xml:"definitions>definition>metadata>advisory>issued"`
	Cpe	[]string	`xml:"definitions>definition>metadata>advisory>affected_cpe_list>cpe"`
	Title	[]string	`xml:"definitions>definition>metadata>title"`
	Oval	string	`xml:"oval,attr"`
	Rpminfo_object	[]Rpminfo_object	`xml:"objects>rpminfo_object"`
	Signature_keyid	[]Signature_keyid	`xml:"states>rpminfo_state>signature_keyid"`
	Bugzilla	[][]Bugzilla	`xml:"definitions>definition>metadata>advisory>bugzilla"`
	Rpminfo_test	[]Rpminfo_test	`xml:"tests>rpminfo_test"`
	Content_version	string	`xml:"generator>content_version"`
}

type Advisory struct {
	From	string	`xml:"from,attr"`
}
type CriteriaCriteriaCriteriaCriteriaDefinitionDefinitions struct {
	Operator	string	`xml:"operator,attr"`
}
type Rpminfo_state struct {
	Id	string	`xml:"id,attr"`
	Version	string	`xml:"version,attr"`
}
type Signature_keyid struct {
	Text	string	`xml:",chardata"`
	Operation	string	`xml:"operation,attr"`
}
type Issued struct {
	Date	string	`xml:"date,attr"`
}
type Reference struct {
	Ref_id	string	`xml:"ref_id,attr"`
	Ref_url	string	`xml:"ref_url,attr"`
	Source	string	`xml:"source,attr"`
}
type Criteria struct {
	Operator	string	`xml:"operator,attr"`
}
type CriterionCriteriaCriteriaDefinitionDefinitions struct {
	Comment	string	`xml:"comment,attr"`
	Test_ref	string	`xml:"test_ref,attr"`
}
type Evr struct {
	Datatype	string	`xml:"datatype,attr"`
	Operation	string	`xml:"operation,attr"`
	Text	string	`xml:",chardata"`
}
type Updated struct {
	Date	string	`xml:"date,attr"`
}
type Bugzilla struct {
	Href	string	`xml:"href,attr"`
	Id	string	`xml:"id,attr"`
	Text	string	`xml:",chardata"`
}
type CriterionCriteriaCriteriaCriteriaDefinitionDefinitions struct {
	Comment	string	`xml:"comment,attr"`
	Test_ref	string	`xml:"test_ref,attr"`
}
type Rpminfo_test struct {
	Check	string	`xml:"check,attr"`
	Comment	string	`xml:"comment,attr"`
	Id	string	`xml:"id,attr"`
	Version	string	`xml:"version,attr"`
}
type Rpminfo_object struct {
	Id	string	`xml:"id,attr"`
	Version	string	`xml:"version,attr"`
}
type Object struct {
	Object_ref	string	`xml:"object_ref,attr"`
}
type Definition struct {
	Version	string	`xml:"version,attr"`
	Class	string	`xml:"class,attr"`
	Id	string	`xml:"id,attr"`
}
type Cve struct {
	Text	string	`xml:",chardata"`
	Impact	string	`xml:"impact,attr"`
	Public	string	`xml:"public,attr"`
	Cvss2	string	`xml:"cvss2,attr"`
	Cwe	string	`xml:"cwe,attr"`
	Cvss3	string	`xml:"cvss3,attr"`
	Href	string	`xml:"href,attr"`
}
type Affected struct {
	Family	string	`xml:"family,attr"`
}
type Criterion struct {
	Comment	string	`xml:"comment,attr"`
	Test_ref	string	`xml:"test_ref,attr"`
}
type CriteriaCriteriaDefinitionDefinitions struct {
	Operator	string	`xml:"operator,attr"`
}
type CriteriaCriteriaCriteriaDefinitionDefinitions struct {
	Operator	string	`xml:"operator,attr"`
}
type CriterionCriteriaCriteriaCriteriaCriteriaDefinitionDefinitions struct {
	Comment	string	`xml:"comment,attr"`
	Test_ref	string	`xml:"test_ref,attr"`
}
type State struct {
	State_ref	string	`xml:"state_ref,attr"`
}
type Version struct {
	Text	string	`xml:",chardata"`
	Operation	string	`xml:"operation,attr"`
}

The result is non-functional.
It contains dashes "-", which are invalid for structs (f.e. Oval_definitions.Red-Def).
When I changed those to underscores, the execution is prevented by

panic: main.Oval_definitions field "Affected" with tag "definitions>definition>metadata>affected" conflicts with field "Platform" with tag "definitions>definition>metadata>affected>platform"

after changing that, I get

panic: main.Oval_definitions field "CriterionCriteriaCriteriaCriteriaDefinitionDefinitions" with tag "definitions>definition>criteria>criteria>criteria>criterion" conflicts with field "CriteriaCriteriaDefinitionDefinitions" with tag "definitions>definition>criteria>criteria"

Which is a very tedious task to do for every field.

The struct have conflict path when a tag have both subtag and attribute.

xml like this

<?xml version="1.0" encoding="utf-8"?>
<root>
    <channel id="1">
         <item></item>
         <item></item>
        <item></item>
   </channel>
</root>

will get structs like that

type Root struct {
    Channel Channel `xml:"channel"`
    Item    []string    `xml:"channel>item"`
}

type Channel struct {
    Id  string  `xml:"id,attr"`
}

This one have a conflict path,the correct from should be

type Root struct {
    Channel Channel `xml:"channel"`
}

type Channel struct {
    Id  string  `xml:"id,attr"`
    Item    []string    `xml:"item"`
}

Remove the conflict is a tough job with current data struct.

So I'm working a new version that can handle both nesting style and non-nesting style(without conflict).

xml2jsonstruct not working on Windows

I get the following error when trying to run this:

C:\Users\Me
λ xmljson2struct.exe
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x30 pc=0x401c91]

goroutine 1 [running]:
main.main()
        C:/Users/Me/dev/golang/src/github.com/wicast/xj2s/cmd/xmljson2struct/main.go:48 +0x9d1

go version go1.5.1 windows/amd64

It might have something to do with this line using ModeCharDevice, but it's hard to tell. I haven't used os.Stat before in my projects, either, so it could just as likely be that.

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.