Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse xml custom properties using Golang

Tags:

xml

maven

go

My xml is present inside jar file. I am using encoding/xml library for parsing it. If you notice there is not attribute in properties like name, id. Its a customer property whose name can be anything.

<project>
        <modelVersion>4.0.0</modelVersion>
        <groupId>org.example</groupId>
        <artifactId>example-artifact</artifactId>
        <version>1.0.0</version>
        <parent>
            <groupId>org.parent</groupId>
            <artifactId>parent-artifact</artifactId>
            <version>1.1.0</version>
        </parent>
        <properties>
            <jmh.version>1.36</jmh.version>
            <some.other.property>value</some.other.property>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.dependency</groupId>
                <artifactId>dependency-artifact</artifactId>
                <version>${jmh.version}</version>
            </dependency>
        </dependencies>
    </project>
type Project struct {
    Pkg_Name   string       `xml:"artifactId"`
    Version    string       `xml:"version"`
    Parent_ver string       `xml:"parent>version"`
    Package    []Dependency `xml:"dependencies>dependency"`
}

type Dependency struct {
    ArtifactId string `xml:"artifactId"`
    Version    string `xml:"version"`
}

type Parent struct {
    GroupId    string `xml:"groupId"`
    ArtifactId string `xml:"artifactId"`
    Version    string `xml:"version"`
}

// Define a structure for the POM XML content (with dependencies)
type Pom struct {
    XMLName      xml.Name     `xml:"project"`
    ModelVersion string       `xml:"modelVersion"`
    GroupId      string       `xml:"groupId"`
    ArtifactId   string       `xml:"artifactId"`
    Version      string       `xml:"version"`
    Parent       *Parent      `xml:"parent"`
    Properties   *Properties  `xml:"properties"`
    Dependencies []Dependency `xml:"dependencies>dependency"`
}

// Define a struct to hold dynamic properties
type Properties struct {
    XMLName xml.Name       `xml:"properties"`
    Items   []PropertyItem `xml:",any"`
}

type PropertyItem struct {
    Name  string `xml:",any"`
    Value string `xml:",chardata"`
}

I have defined above struct for decoding the pom.xml. But unable to get the property key i.e JmhVersion. I am getting property values though.

decoder = xml.NewDecoder(pomReader) err := decoder.Decode(&pom)

pomreader is the zipped file object.

like image 666
Tushar Likhar Avatar asked Dec 01 '25 18:12

Tushar Likhar


2 Answers

    package main
    
    import (
        "encoding/xml"
        "fmt"
        "os"
    )
    
    type Project struct {
        XMLName      xml.Name     `xml:"project"`
        ModelVersion string       `xml:"modelVersion"`
        GroupID      string       `xml:"groupId"`
        ArtifactID   string       `xml:"artifactId"`
        Version      string       `xml:"version"`
        Parent       Parent       `xml:"parent"`
        Properties   Properties   `xml:"properties"`
        Dependencies Dependencies `xml:"dependencies"`
    }
    
    type Parent struct {
        GroupID    string `xml:"groupId"`
        ArtifactID string `xml:"artifactId"`
        Version    string `xml:"version"`
    }
    
    type Properties struct {
        JMHVersion        string `xml:"jmh.version"`
        SomeOtherProperty string `xml:"some.other.property"`
    }
    
    type Dependencies struct {
        Dependency []Dependency `xml:"dependency"`
    }
    
    type Dependency struct {
        GroupID    string `xml:"groupId"`
        ArtifactID string `xml:"artifactId"`
        Version    string `xml:"version"`
    }
    
    func main() {
        // Read the XML file
        xmlFile, err := os.ReadFile("pom.xml")
        if err != nil {
            fmt.Println("Error reading file:", err)
            return
        }
    
        // Unmarshal the XML into the Project struct
        var project Project
        err = xml.Unmarshal(xmlFile, &project)
        if err != nil {
            fmt.Println("Error unmarshalling XML:", err)
            return
        }
    
        // Print the parsed data
        fmt.Printf("Project: %+v\n", project)
        fmt.Printf("Parent: %+v\n", project.Parent)
        fmt.Printf("Properties: %+v\n", project.Properties)
        for _, dep := range project.Dependencies.Dependency {
            fmt.Printf("Dependency: %+v\n", dep)
        }
    }

this works for me properly .

like image 162
Mahdi zarepoor Avatar answered Dec 04 '25 17:12

Mahdi zarepoor


type Project struct {
    Pkg_Name   string       `xml:"artifactId"`
    Version    string       `xml:"version"`
    Parent_ver string       `xml:"parent>version"`
    Package    []Dependency `xml:"dependencies>dependency"`
}

type Dependency struct {
    ArtifactId string `xml:"artifactId"`
    Version    string `xml:"version"`
}

type Parent struct {
    GroupId    string `xml:"groupId"`
    ArtifactId string `xml:"artifactId"`
    Version    string `xml:"version"`
}

// Define a structure for the POM XML content (with dependencies)
type Pom struct {
    XMLName      xml.Name     `xml:"project"`
    ModelVersion string       `xml:"modelVersion"`
    GroupId      string       `xml:"groupId"`
    ArtifactId   string       `xml:"artifactId"`
    Version      string       `xml:"version"`
    Parent       *Parent      `xml:"parent"`
    Properties   *Properties  `xml:"properties"`
    Dependencies []Dependency `xml:"dependencies>dependency"`
}

// Define a struct to hold dynamic properties
type Properties struct {
    XMLName xml.Name       `xml:"properties"`
    Items   []PropertyItem `xml:",any"`
}

type PropertyItem struct {
    XMLName xml.Name
    Value string `xml:",chardata"`
}

Above is the correct struct to decode pom.xml file

Change is from below PropertyItem

type PropertyItem struct {
    Name  string `xml:",any"`
    Value string `xml:",chardata"`
}

to

type PropertyItem struct {
    XMLName xml.Name
    Value   string `xml:",chardata"`
}
like image 23
Tushar Likhar Avatar answered Dec 04 '25 18:12

Tushar Likhar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!