0

I am working on extracting data from a xml output. I have written the below code. I just need to extract dept number from the below xml. On running the below code getting a null output. Can someone let me how to extract the dept number from the xml and why I am getting null as output?

package main

import (
    "encoding/xml"
    "fmt"
)

type Users struct {
    XMLName xml.Name `xml:"users"`
    User   User   `xml:"user"`
}
type User struct {
    XMLName xml.Name `xml:"user"` 
    Type string  `xml:"name"` 
    DD  DD   `xml:"dd"`
}
type DD struct {
    XMLName xml.Name `xml:"dd"` 
    Number string  `xml:"number"`
    Description string  `xml:"description"` 
   Type  Type   `xml:"type"`
    Dept  Dept   `xml:"dept"`
}
type Type struct{
   XMLName xml.Name `xml:"type"` 
}
type Dept struct {
    XMLName xml.Name `xml:"dept"`
    Number string  `xml:"number"`
   Type  Type   `xml:"type"`
}

func main() {
var users Users
var byteValue = []byte(`<users>
<user>
<type>1</type>
<bu>
    <number>123</number>
    <id>100</id>
    <type>
        <code>123</code>
    </type>
</bu>
<dd>
    <number>1</number>
    <description>abc</description>
    <type>
        <code>12345</code>
        <id>qw123<id>
    <type>
    <dept>
        <number>10</number>      <<<<<<<
        <type>qw12345</type>
        
    </dept>
</dd>
<bd>
    <code>34we5</code>
    <id>qw123<id>
</bd>
<OD>
    <code>9876</code>
    <id>qwerty123<id>
</OD>   
</user>
</users>`)
xml.Unmarshal(byteValue, &users)
fmt.Println("Dept Number: " + users.User.DD.Dept.Number)
}
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Ruban
  • 125
  • 7

1 Answers1

2

Looks like the XML provided is wrong. Please do try using below XML

<users>
    <user>
        <type>1</type>
        <bu>
            <number>123</number>
            <id>100</id>
            <type>
                <code>123</code>
            </type>
        </bu>
        <dd>
            <number>1</number>
            <description>abc</description>
            <type>
                <code>12345</code>
                <id>qw123</id>
            </type>
            <dept>
                <number>10</number>
                <type>qw12345</type>
            </dept>
        </dd>
        <bd>
            <code>34we5</code>
            <id>qw123</id>
        </bd>
        <OD>
            <code>9876</code>
            <id>qwerty123</id>
        </OD>
    </user>
</users>

You can have a look at the working example with your provided example here on playground. https://play.golang.org/p/4zQsaz5Z_5P

  • Shailesh, Thanks for rectified code can you spot what is the issue with the XML. Both looks same. – Ruban Jul 19 '21 at 08:57
  • 1
    @Ruban 3 of your `id` elements and one `type` element don't have a valid closing tag. But you would know that if you checked the error returned from `xml.Unmarshal`. – mkopriva Jul 19 '21 at 09:06
  • 1
    @Ruban since you're new to Go you may not know but, handling of errors is imperative and should never-ever be omitted, it's one of the most important principles in Go programming. – mkopriva Jul 19 '21 at 09:09
  • 1
    @Ruban btw. if you only need that single nested value you can significantly simplify the Go structs by using `>` in the `xml:"..."` struct tag: https://play.golang.org/p/zxSoPH2npkN – mkopriva Jul 19 '21 at 09:15
  • 1
    @Ruban I used online tools for rectifying the XML, you can check this https://jsonformatter.org/xml-formatter. I agree with mkopriva of using errors as it is imperative. I found the issue with the marshaling with that approach only, you can see that I have made the changes in the shared playground snippet as well. – Shailesh Suryawanshi Jul 19 '21 at 09:41