Home > Backend Development > Golang > How to parse XML files and filter nodes using XML processing functions in Go language?

How to parse XML files and filter nodes using XML processing functions in Go language?

WBOY
Release: 2023-07-31 22:30:32
Original
1046 people have browsed it

How to use XML processing functions in Go language to parse XML files and filter nodes?

In the Go language, we can use the standard library encoding/xml to process XML files. encoding/xml Provides a series of functions and structures to help us quickly parse XML files and filter out the required nodes as needed. This article will introduce how to use the XML processing function in the Go language to parse XML files and filter nodes, and provide corresponding code examples.

First, we need to import the encoding/xml package:

import (
    "encoding/xml"
    "fmt"
    "io/ioutil"
    "os"
)
Copy after login

Assume we have an XML file with the path ./data.xml, The content of the XML file is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <person>
        <name>张三</name>
        <age>25</age>
    </person>
    <person>
        <name>李四</name>
        <age>30</age>
    </person>
    <person>
        <name>王五</name>
        <age>35</age>
    </person>
</root>
Copy after login

We first need to define a structure to store the parsed XML data. Suppose we parse each <person> node into a Person structure:

type Person struct {
    Name string `xml:"name"`
    Age  int    `xml:"age"`
}
Copy after login

Next, we can use xml.Unmarshal() The function parses the XML file into a structure slice, the code is as follows:

func main() {
    file, err := os.Open("./data.xml")
    if err != nil {
        fmt.Println("打开文件失败:", err)
        return
    }
    defer file.Close()

    data, err := ioutil.ReadAll(file)
    if err != nil {
        fmt.Println("读取文件失败:", err)
        return
    }

    var persons []Person
    err = xml.Unmarshal(data, &persons)
    if err != nil {
        fmt.Println("解析XML文件失败:", err)
        return
    }

    fmt.Println(persons)
}
Copy after login

Run the above code, we can see the output as follows:

[{张三 25} {李四 30} {王五 35}]
Copy after login

Next, we can filter as needed out of a specific node. For example, if we only want to obtain information about people aged 30 or above, we can use the following code:

func main() {
    file, err := os.Open("./data.xml")
    if err != nil {
        fmt.Println("打开文件失败:", err)
        return
    }
    defer file.Close()

    data, err := ioutil.ReadAll(file)
    if err != nil {
        fmt.Println("读取文件失败:", err)
        return
    }

    var persons []Person
    err = xml.Unmarshal(data, &persons)
    if err != nil {
        fmt.Println("解析XML文件失败:", err)
        return
    }

    var filteredPersons []Person
    for _, person := range persons {
        if person.Age >= 30 {
            filteredPersons = append(filteredPersons, person)
        }
    }

    fmt.Println(filteredPersons)
}
Copy after login

Run the above code, we can see the output as follows:

[{李四 30} {王五 35}]
Copy after login

Through the above code example , we can see how to use the XML processing function in the Go language to parse the XML file and filter out the required nodes. You can modify the code according to actual needs to perform more complex XML parsing and filtering operations.

The above is the detailed content of How to parse XML files and filter nodes using XML processing functions in Go language?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template