xmlgoxml-parsingworkday-api

Parsing complex nested xml with colon in tags


I'm trying to parse following xml :

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
   <env:Body>
      <wd:Get_Workers_Response wd:version="v35.0" xmlns:wd="urn:com.workday/bsvc">
         <wd:Request_References>
            <wd:Worker_Reference>
               <wd:ID wd:type="WID">11112222333444</wd:ID>
               <wd:ID wd:type="Employee_ID">123456789</wd:ID>
            </wd:Worker_Reference>
         </wd:Request_References>
        <wd:Response_Data>
            <wd:Worker>

               <wd:Worker_Descriptor>user xyz</wd:Worker_Descriptor>
               <wd:Worker_Data>
                  <wd:Worker_ID>123456789</wd:Worker_ID>
                  <wd:User_ID>user_one_id</wd:User_ID>
                  <wd:Personal_Data>

                     <wd:Contact_Data>

                       <wd:Email_Address_Data>
                           <wd:Email_Address>abc@gmail.com</wd:Email_Address>
                           <wd:Usage_Data wd:Public="1">
                              <wd:Type_Data wd:Primary="1">
                                 <wd:Type_Reference>
                                    <wd:ID wd:type="WID">12346780</wd:ID>
                                    <wd:ID wd:type="Communication_Usage_Type_ID">WORK</wd:ID>
                                 </wd:Type_Reference>
                              </wd:Type_Data>
                           </wd:Usage_Data>
                           <wd:Email_Reference>
                              <wd:ID wd:type="WID">0987654321</wd:ID>
                              <wd:ID wd:type="Email_ID">EMAIL_REFERENCE-3-1</wd:ID>
                           </wd:Email_Reference>
                           <wd:ID>EMAIL_REFERENCE-3-1</wd:ID>
                        </wd:Email_Address_Data>
                     </wd:Contact_Data>
                     
                  </wd:Personal_Data>


                 </wd:Worker_Data>
            </wd:Worker>
         </wd:Response_Data>
      </wd:Get_Workers_Response>
   </env:Body>
</env:Envelope>

I tried this struct :

type ElementOne struct {
    XMLName xml.Name `xml:"Envelope"`
    Bar     interface{}    `xml:"env:Body>wd:Get_Workers_Response "`
}

I'm unable to get inside <env:Body> tag. parsing doesn't seems to work due to colon. I tried escaping colon as well (env\:Body). I need following values from this xml : WID, Employee_ID from Worker_Reference and Email_ID which is inside nested tags in wd:Email_Address_Data

Any help will be really appreciated, I'm new to xml parsing so it's may be I'm missing something here.

Thanks.


Solution

  • You're using the wrong syntax in the struct tags.

    Example for just WID and Employee_ID:

    type wdID struct {
        ID      string `xml:"type,attr"`
        Content string `xml:",chardata"`
    }
    
    type exampleStruct struct {
        XMLName xml.Name `xml:"Envelope"`
        WIDs     []wdID   `xml:"Body>Get_Workers_Response>Request_References>Worker_Reference>ID"`
    }
    

    Also, since the name of your tags repeat themselves (multiple "ID"), you have to parse them out into an array where you get the unique attribute that distinguishes them (the wdID struct).

    Go playground with working example of a smaller version of the XML you gave: https://play.golang.org/p/BS8crPQ7G1n

    EDIT: https://golang.org/pkg/encoding/xml/#Unmarshal If you read in the comments of the XML Unmarshal'er you can find some tidbits about the specifics for struct tagging