[Solved]Help me for xml parser


(jeremy) #1

Hello all,

I’am sorry for my english but i’am a french guy and my english is very bad.

I tried to parse an xml file with GO.
The xml file is a JIRA export

I use code find on the internet.

The simple file xml for exemple

JIRA

and the go code

package main

import (
“encoding/xml”
“fmt”
“io/ioutil”
“os”
)

// Structure globale
type RSS struct {
XMLName xml.Name xml:"balise"
version string xml:"version,attr"
titre string xml:"title"
}

func main() {

// Open our xmlFile                                                          
xmlFile, err := os.Open("test.xml")                                          
// if we os.Open returns an error then handle it                             
if err != nil {                                                              
    fmt.Println(err)                                                         
}                                                                            
                                                                             
fmt.Println("Successfully Opened users.xml")                                 
// defer the closing of our xmlFile so that we can parse it later on         
defer xmlFile.Close()                                                        
                                                                             
// read our opened xmlFile as a byte array.                                  
byteValue, err := ioutil.ReadAll(xmlFile)                                    
                                                                             
    if err != nil {                                                          
                                                                             
    fmt.Println(err)                                                         
    }                                                                        
                                                                             
// we initialize our Users array                                             
var rss RSS                                                                  
                                                                             
err = xml.Unmarshal(byteValue, &rss)                                         
fmt.Println(rss)                                                 

}

when run the program, the result is just “{{ balise} }”
Why i don’t see the version and title in the reponse?

Thank you for your help, i search since 2 days but no answer for the moment :frowning:


(Yamil Bracho) #2

I guess there is some backticks missing in the xml struct definition.
Also, publis some lines from your xml file to check it.


(jeremy) #3

thanks for your rapide answers.
The real file is bigger but for the moment i have just write this in file
( i have add space for presentation)
< balise version=“0.92”>
< title>
JIRA
< /title>
< /balise>


(Norbert Melzer) #4

Can you please provide something on play.golang.org which shows your problem? The way you created your code and XML makes it hard to copy it.

Alternatively you can use markdown fenced codeblocks here in the forum to have proper formatted and displayed code which we then could simply copy and paste


(Norbert Melzer) #5

I’ve tried to do it in the playground, and after fixing the errors that came form the forums magic-quotes, I had to hardcode the XML, also I used fmt.Printf("%#v", rss), as I prefer the debug output for such things. Its more verbose, but also more explicit.

Anyway, the first try showed some warnings:

prog.go:13:2: struct field version has xml tag but is not exported
prog.go:14:2: struct field titre has xml tag but is not exported

So after exporting those fields, everything looks like expected:

Successfully Opened users.xml
main.RSS{XMLName:xml.Name{Space:"", Local:"balise"}, Version:"0.92", Titre:"\n    JIRA\n  "}

(jeremy) #6

Thanks you very much NobbZ, i have just change version to Version and titre to Titre in definition of structure and all is OK!