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 {
 Name        string   `xml:"name,attr"`
 Description string   `xml:"description"`
 Rights      []Right  `xml:"rights"`
 Request     Request  `xml:"request"`
 Response    Response `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(&param, &t); err != nil {
 return err
 }
 *pl = append(*pl, param)
 }
 tk, err = d.Token()
 }
 return err
}


// Param holds data
type Param struct {
 Name        string `xml:"name"`
 Type        string `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.
>
>
>
> <?xml version="1.0" encoding="utf-8"?>
> <topLevel name="insight">
>   <description>description number1</description>
>   <rights>
>     <privLevel>user</privLevel>
>   </rights>
>   <method name="graphCreate">
>     <description>graphCreate API</description>
>     <rights></rights>
>     <request>
>       <params>
>         <name type="xs:string" minOccurs="1" >
>           <description>A friendly name to identify the graph</description>
>         </name>
>         <description type="xs:string" minOccurs="1" >
>           <description>Detailed description of the graph</description>
>         </description>        
>         <definition type="graphDefinition" minOccurs="1" maxOccurs="1">
>           <description>Specify the graph definition. i.e how this graph 
> should be built.</description>
>         </definition>
>       </params>
>     </request>
>     <response>
>       <params>
>    <id type="xs:unsignedInt" minOccurs="1" >
>           <description>graph identifier.</description>
>         </id>
>       </params>
>     </response>
>   </method>
>  </topLevel>
>
> 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 {
>         Name        string  `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.

Reply via email to