K
K
Kairat Ubukulov2018-07-31 12:23:29
go
Kairat Ubukulov, 2018-07-31 12:23:29

How to make an import.xml file parser?

There is a file called import.xml. The file is located on the server where the site is located. There is a code that parses an external resource file. Question: How to parse a file that is on the server?
Here is the code to parse the file by reference.

func DownloadAzertiFile(url string) *AzertiCatalog {
  list := AzertiCatalog{}
  response, err := http.Get(url)
  if err != nil {
    return &list
  } else {
    defer response.Body.Close()
    body, _ := ioutil.ReadAll(response.Body)
    if err != nil {
      return &list
    }
    newbody := strings.Replace(string(body), "<offers>", "", -1)
    newbody = strings.Replace(newbody, "</offers>", "", -1)
    err := xml.Unmarshal([]byte(newbody), &list)
    if err != nil {
      return &list
    }
  }
  return &list
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Bynov, 2018-08-14
@ubukulov

Good afternoon!

b, err := ioutil.ReadFile("path/to/file.xml")
if err != nil {
    panic(err
}

In b you will have the contents of the file as an array of bytes, which can be parsed by xml.Unmarshal as in your example

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question