[go-nuts] Re: dynamic xml

2017-10-19 Thread Jim Cote
You'll need to use an UnmarshalXML func for the params:

// Param is a tag w/ any name
type Param struct {
Namestring `xml:"-"`
Typestring `xml:"type,attr"`
MinOccurs   int`xml:"minOccurs,attr"`
MaxOccurs   int`xml:"maxOccurs,attr"`
Description string `xml:"description"`
}


// ParamList type for unmarhsal
type ParamList []Param


// UnmarshalXML for the win
func (pl *ParamList) UnmarshalXML(d *xml.Decoder, s xml.StartElement) error 
{
tk, err := d.Token()
for err == nil {
switch t := tk.(type) {
case xml.StartElement:
param := Param{Name: t.Name.Local}
if err = d.DecodeElement(, ); err != nil {
return err
}
*pl = append(*pl, param)
 }
 if tk, err = d.Token(); err == io.EOF {
 return nil
 }
}
return err
}


// TopLevel is of course top level
type TopLevel struct {
Description string   `xml:"description"`
Rights  Rights   `xml:"rights"`
Method  []Method `xml:"method"`
}


// Method has request
type Method struct {
Namestring   `xml:"name,attr"`
Description string   `xml:"description"`
Rights  []Right  `xml:"rights"`
Request Request  `xml:"request"`
ResponseResponse `xml:"response"`
}


// Right whatever
type Right struct {
Right string `xml:"right"`
}


// Request like Response
type Request struct {
Params ParamList `xml:"params"`
}


// Response like Request
type Response struct {
Params ParamList `xml:"params"`
}


// Rights 
type Rights struct {
PrivLevel string `xml:"privLevel"`
}


On Wednesday, October 18, 2017 at 11:50:45 AM UTC-4, Jeffrey Smith wrote:
>
> I have a lot of XML documents that have sections in that contain tags that 
> can be of any name but will contain type,minOccurs,maxOccurs and have a 
> description inside it. For instance name,description,definition and id 
> below.
>
>
>
> 
> 
>   description number1
>   
> user
>   
>   
> graphCreate API
> 
> 
>   
> 
>   A friendly name to identify the graph
> 
> 
>   Detailed description of the graph
> 
> 
>   Specify the graph definition. i.e how this graph 
> should be built.
> 
>   
> 
> 
>   
>
>   graph identifier.
> 
>   
> 
>   
>  
>
> Whats the best way to parse this. So far I have but can't work out how to 
> dynamically generate the struct to stuff this into. Any suggestions?
>
> type topLevel struct {
> Description string   `xml:"description"`
> Rights  rights   `xml:"rights"`
> Method  []method `xml:"method"`
> }
>
>
> type method struct {
> Namestring  `xml:"name,attr"`
> Description string  `xml:"description"`
> Rights  []right `xml:"rights"`
> }
>
>
> type right struct {
> Right string `xml:"right"`
> }
>
>
> type request struct {
> }
>
>
> type rights struct {
> PrivLevel string `xml:"privLevel"`
> }
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: dynamic xml

2017-10-19 Thread Jim Cote
You'll need to use a custom UnmarshalXML func to handle this.



// TopLevel is of course top level
type TopLevel struct {
 Description string   `xml:"description"`
 Rights  Rights   `xml:"rights"`
 Method  []Method `xml:"method"`
}


// Method has request
type Method struct {
 Namestring   `xml:"name,attr"`
 Description string   `xml:"description"`
 Rights  []Right  `xml:"rights"`
 Request Request  `xml:"request"`
 ResponseResponse `xml:"response"`
}


// Right whatever
type Right struct {
 Right string `xml:"right"`
}


// Request like Response
type Request struct {
 Params ParamList `xml:"params"`
}


// Response like Request
type Response struct {
 Params ParamList `xml:"params"`
}


// ParamList type for unmarhsal
type ParamList []Param


// UnmarshalXML for the win
func (pl *ParamList) UnmarshalXML(d *xml.Decoder, s xml.StartElement) error 
{
 tk, err := d.Token()
 for err == nil {
 switch t := tk.(type) {
 case xml.StartElement:
 var param = Param{Name: t.Name.Local}
 if err = d.DecodeElement(, ); err != nil {
 return err
 }
 *pl = append(*pl, param)
 }
 tk, err = d.Token()
 }
 return err
}


// Param holds data
type Param struct {
 Namestring `xml:"name"`
 Typestring `xml:"type,attr"`
 MinOccurs   int`xml:"minOccurs,attr"`
 MaxOccurs   int`xml:"maxOccurs,attr"`
 Description string `xml:"description"`
}


// Rights hmm
type Rights struct {
 PrivLevel string `xml:"privLevel"`
}


On Wednesday, October 18, 2017 at 11:50:45 AM UTC-4, Jeffrey Smith wrote:
>
> I have a lot of XML documents that have sections in that contain tags that 
> can be of any name but will contain type,minOccurs,maxOccurs and have a 
> description inside it. For instance name,description,definition and id 
> below.
>
>
>
> 
> 
>   description number1
>   
> user
>   
>   
> graphCreate API
> 
> 
>   
> 
>   A friendly name to identify the graph
> 
> 
>   Detailed description of the graph
> 
> 
>   Specify the graph definition. i.e how this graph 
> should be built.
> 
>   
> 
> 
>   
>
>   graph identifier.
> 
>   
> 
>   
>  
>
> Whats the best way to parse this. So far I have but can't work out how to 
> dynamically generate the struct to stuff this into. Any suggestions?
>
> type topLevel struct {
> Description string   `xml:"description"`
> Rights  rights   `xml:"rights"`
> Method  []method `xml:"method"`
> }
>
>
> type method struct {
> Namestring  `xml:"name,attr"`
> Description string  `xml:"description"`
> Rights  []right `xml:"rights"`
> }
>
>
> type right struct {
> Right string `xml:"right"`
> }
>
>
> type request struct {
> }
>
> Enter code here...
>
>
> type rights struct {
> PrivLevel string `xml:"privLevel"`
> }
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.