Created
October 25, 2018 11:25
-
-
Save Niakr1s/f868f67d150bc5b1fede72e0077b73b8 to your computer and use it in GitHub Desktop.
xml parsing example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/xml" | |
"fmt" | |
"io/ioutil" | |
"os" | |
) | |
type CadastralBlock struct { | |
XMLName xml.Name `xml:""` | |
Parcels Parcels `xml:"Parcels"` | |
} | |
type Parcels struct { | |
Parcel Parcel `xml:"Parcel"` | |
} | |
type Parcel struct { | |
XMLName xml.Name `xml:"Parcel"` | |
CadastralBlock string `xml:"CadastralBlock"` | |
EntitySpatial EntitySpatial `xml:"EntitySpatial"` | |
} | |
type EntitySpatial struct { | |
XMLName xml.Name `xml:"EntitySpatial"` | |
SpatialElement SpatialElement `xml:"SpatialElement"` | |
} | |
type SpatialElement struct { | |
XMLName xml.Name `xml:"SpatialElement"` | |
Ordinates []Ordinate `xml:"SpelementUnit>Ordinate"` | |
} | |
type Ordinate struct { | |
XMLName xml.Name `xml:"Ordinate"` | |
X float64 `xml:"X,attr"` | |
Y float64 `xml:"Y,attr"` | |
} | |
const filename = "kvzu.xml" | |
func main() { | |
f, err := os.Open(filename) | |
if err != nil { | |
fmt.Println("Error while opening file", filename, err) | |
} | |
fmt.Println("File succesfully opened", filename) | |
defer f.Close() | |
byteValue, _ := ioutil.ReadAll(f) | |
var block CadastralBlock | |
xml.Unmarshal(byteValue, &block) | |
fmt.Printf("%+v\n", block) | |
coords := block.Parcels.Parcel.EntitySpatial.SpatialElement.Ordinates | |
for _, coord := range coords { | |
fmt.Println(coord.X, coord.Y) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment