Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complex XML Response #72

Open
DanielZambranoC opened this issue Dec 18, 2020 · 3 comments
Open

Complex XML Response #72

DanielZambranoC opened this issue Dec 18, 2020 · 3 comments

Comments

@DanielZambranoC
Copy link

DanielZambranoC commented Dec 18, 2020

Hi, I've been trying to parse an answer and I still can't get it right.

I have tried in different ways, which I believe closer has been the structure provided by this tool (https://www.onlinetool.io/xmltogo/)

I need to be able to extract indexDateString and value

Can you advise me on where I am failing?

Error obtained: xml.Unmarshal error: EOF

Try 1

type GetSeriesResponse struct {
	XMLName         xml.Name `xml:"GetSeriesResponse"`
	Text            string   `xml:",chardata"`
	Xmlns           string   `xml:"xmlns,attr"`
	GetSeriesResult struct {
		Text   string `xml:",chardata"`
		Series struct {
			Text       string `xml:",chardata"`
			FameSeries struct {
				Text string `xml:",chardata"`
				Obs  []struct {
					Text            string `xml:",chardata"`
					Xmlns           string `xml:"xmlns,attr"`
					IndexDateString string `xml:"indexDateString"`
					StatusCode      string `xml:"statusCode"`
					Value           string `xml:"value"`
				} `xml:"obs"`
			} `xml:"fameSeries"`
		} `xml:"Series"`
	} `xml:"GetSeriesResult"`
} 

Try 2

type GetSeriesResponse struct {
	GetSeriesResult string `xml:"GetSeriesResult"`
}

type GetSeriesResult struct {
	XMLName xml.Name `xml:"GetSeriesResult"`
	Text    string   `xml:",chardata"`
	Series  struct {
		Text       string `xml:",chardata"`
		FameSeries struct {
			Text string `xml:",chardata"`
			Obs  []struct {
				Text            string `xml:",chardata"`
				Xmlns           string `xml:"xmlns,attr"`
				IndexDateString string `xml:"indexDateString"`
				StatusCode      string `xml:"statusCode"`
				Value           string `xml:"value"`
			} `xml:"obs"`
		} `xml:"fameSeries"`
	} `xml:"Series"`
}

Response

<GetSeriesResponse xmlns="http://bancocentral.org/">
            <GetSeriesResult>
                <Series>
                    <fameSeries>
                        <obs xmlns="">
                            <indexDateString>07-12-2020</indexDateString>
                            <statusCode>OK</statusCode>
                            <value>3.5877</value>
                        </obs>
                        <obs xmlns="">
                            <indexDateString>08-12-2020</indexDateString>
                            <statusCode>OK</statusCode>
                            <value>NaN</value>
                        </obs>
                        <obs xmlns="">
                            <indexDateString>09-12-2020</indexDateString>
                            <statusCode>OK</statusCode>
                            <value>3.6025</value>
                        </obs>
                        <obs xmlns="">
                            <indexDateString>10-12-2020</indexDateString>
                            <statusCode>OK</statusCode>
                            <value>3.6045</value>
                        </obs>
                        <obs xmlns="">
                            <indexDateString>11-12-2020</indexDateString>
                            <statusCode>OK</statusCode>
                            <value>3.5969</value>
                        </obs>
                        <obs xmlns="">
                            <indexDateString>12-12-2020</indexDateString>
                            <statusCode>OK</statusCode>
                            <value>NaN</value>
                        </obs>
                        <obs xmlns="">
                            <indexDateString>13-12-2020</indexDateString>
                            <statusCode>OK</statusCode>
                            <value>NaN</value>
                        </obs>
                        <obs xmlns="">
                            <indexDateString>14-12-2020</indexDateString>
                            <statusCode>OK</statusCode>
                            <value>3.5968</value>
                        </obs>
                        <obs xmlns="">
                            <indexDateString>15-12-2020</indexDateString>
                            <statusCode>OK</statusCode>
                            <value>3.5868</value>
                        </obs>
                    </fameSeries>
                </Series>
            </GetSeriesResult>
        </GetSeriesResponse>
@oxyii
Copy link

oxyii commented Sep 20, 2023

@DanielZambranoC

type Obs struct {
	XMLName         xml.Name `xml:"obs"`
	IndexDateString string   `xml:"indexDateString"`
	StatusCode      string   `xml:"statusCode"`
	Value           string   `xml:"value"`
}

type Response struct {
	XMLName         xml.Name `xml:"GetSeriesResponse"`
	GetSeriesResult struct {
		XMLName xml.Name `xml:"GetSeriesResult"`
		Series  struct {
			XMLName    xml.Name `xml:"Series"`
			FameSeries struct {
				XMLName xml.Name `xml:"fameSeries"`
				Obs     []Obs
			}
		}
	}
}

of course you must set correct data types for Obs fields )

@oxyii
Copy link

oxyii commented Sep 20, 2023

and yes, you can shortcut XMLName tags like this:

type Response struct {
	XMLName         xml.Name `xml:"GetSeriesResponse"`
	GetSeriesResult struct {
		Series struct {
			FameSeries struct {
				Obs []Obs `xml:"obs"`
			} `xml:"fameSeries"`
		} `xml:"Series"`
	} `xml:"GetSeriesResult"`
}

@oxyii
Copy link

oxyii commented Sep 20, 2023

@tiaguinho this issue is stale and resolved. You can close it, I think...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants