C
C
chincharovpc2021-06-04 00:08:01
go
chincharovpc, 2021-06-04 00:08:01

How to parse xml in golang?

Need to parse xml.

<?xml version="1.0" encoding="WINDOWS-1251"?>
<commandresult>
    <name>abonents.list</name>
    <answer>200: OK</answer>
    <data>
        <items>
            <count>2</count>
            <item>
                <name>id</name>
                <value>67</value>
                <name>phone</name>
                <value>67875667867</value>
                <name>name</name>
                <value>Auth</value>
            </item>
            <item>
                <name>id</name>
                <value>68</value>
                <name>phone</name>
                <value>67875674565</value>
                <name>name</name>
                <value>Auth2</value>
            </item>
        </items>
    </data>
</commandresult>


How can I parse elements?
<name>id</name>
                <value>67</value>
                <name>phone</name>
                <value>67875667867</value>
                <name>name</name>
                <value>Auth</value>


My code looks like this:
type GetAbonentsResponseXml struct {
  XMLName xml.Name `xml:"commandresult"`
  Name    string   `xml:"name"`
  Answer  string   `xml:"answer"`
  Count   int      `xml:"data>items>count"`
  Items   []struct {
    Name  []string `xml:"name"`
    Value []string `xml:"value"`
  } `xml:"data>items>item"`
}


Only it returns arrays from name and value attributes
Items: [
  {
     Name:[id phone name] 
     Value:[67 67875667867 Auth]
  }
]


How can I solve this problem? with a loop?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
1
12rbah, 2021-06-04
@chincharovpc

1) You have an incorrect structure.
Service for generating structures for html https://www.onlinetool.io/xmltogo/
2) access to the element will look something like this commandresult.Data[i].Items[i].count, with the code I think you can handle it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question