douxi3233 2017-11-15 22:43
浏览 153
已采纳

逐行读取XML文件

I am new to go and trying to write a script to parse multiple soap responses.

Here is a snippet of the xml snippet of the xml I am trying to parse. Its a full SOAP response but I have just included that tags I wish to parse

            <Results xsi:type="AccountUser">
                <Client>
                    <ID>72rere341</ID>
                </Client>
                <PartnerKey xsi:nil="true" />
                <PartnerProperties>
                    <Name>email</Name>
                    <Value>example1@test1.com</Value>
                </PartnerProperties>
                <ID>755454475</ID>
                <ObjectID xsi:nil="true" />
                <UserID>5fd0acfc-6crerfrgrfe6e9a675f65</UserID>
                <ActiveFlag>true</ActiveFlag>
                <Delete>0</Delete>
                <LastSuccessfulLogin>2014-11-07T16:00:46.747</LastSuccessfulLogin>
            </Results>
            <Results xsi:type="AccountUser">
                <Client>
                    <ID>72rere5341</ID>
                </Client>
                <PartnerKey xsi:nil="true" />
                <PartnerProperties>
                    <Name>email</Name>
                    <Value>example2@test1.com</Value>
                </PartnerProperties>
                <ID>7225483</ID>
                <ObjectID xsi:nil="true" />
                <UserID>example2@test1.com</UserID>
                <ActiveFlag>false</ActiveFlag>
                <Delete>0</Delete>
                <LastSuccessfulLogin>2015-04-29T05:01:27.85</LastSuccessfulLogin>
            </Results>

I would like to print each result on a new line.

Here is a snippet of my code:

package main 


import (

    "os"
    "fmt"
    "encoding/xml"
    "io/ioutil"

)

type AccountUser struct {


    ParentMID string `xml:"Client>ID"`
    EmailAddress string `xml:"PartnerProperties>Value"`
    BuinessUnit string `xml:"ID"`
    UserID string `xml:"UserID"`
    Active string`xml:"ActiveFlag"`
    LastSucessfulLogin string`xml:"LastSucessfulLogin"`

}

type Email struct {

    Email string `xml:"PartnerProperties>Value"`
}


type Query struct {
    Accounts AccountUser
    AccountList []Email `xml:"PartnerProperties>Value"`
}

func (a AccountUser) String() string {
    return fmt.Sprintf("%s - %s - %s", a.ParentMID, a.EmailAddress, a.UserID)
}


func main() {
    xmlFile, err := os.Open("Results.xml")
    if err != nil {
        fmt.Println("Error opening file:", err)
        return
    }
    defer xmlFile.Close()

    b, _ := ioutil.ReadAll(xmlFile)

    var q Query
    xml.Unmarshal(b, &q)

    fmt.Println(q.Accounts)
    for _, account := range q.AccountList {
        fmt.Printf("\t%s
", account)
    }
}

When I run this in terminal, It returns nothing ie.

-     - //just the string in the function

I would love pointers on how I can resolve this.

  • 写回答

2条回答 默认 最新

  • duanchendu69495 2017-11-16 03:03
    关注

    There are some problems with your code :

    First is your xml file is not correct the correct xml file must contains parent xml like <Data> //Yourdata here </Data> it must be look like this :

    <?xml version="1.0" encoding="UTF-8" ?>
    <Data>
        <Results>
            <Client>
                <ID>72rere341</ID>
            </Client>
            <PartnerKey xsi:nil="true" />
            <PartnerProperties>
                <Name>email</Name>
                <Value>example1@test1.com</Value>
            </PartnerProperties>
            <ID>755454475</ID>
            <ObjectID xsi:nil="true" />
            <UserID>5fd0acfc-6crerfrgrfe6e9a675f65</UserID>
            <ActiveFlag>true</ActiveFlag>
            <Delete>0</Delete>
            <LastSuccessfulLogin>2014-11-07T16:00:46.747</LastSuccessfulLogin>
        </Results>
        <Results>
            <Client>
                <ID>72rere5341</ID>
            </Client>
            <PartnerKey xsi:nil="true" />
            <PartnerProperties>
                <Name>email</Name>
                <Value>example2@test1.com</Value>
            </PartnerProperties>
            <ID>7225483</ID>
            <ObjectID xsi:nil="true" />
            <UserID>example2@test1.com</UserID>
            <ActiveFlag>false</ActiveFlag>
            <Delete>0</Delete>
            <LastSuccessfulLogin>2015-04-29T05:01:27.85</LastSuccessfulLogin>
        </Results>
    </Data>
    

    Second your struct AccountUser has a method:

    func (a AccountUser) String() string {
        return fmt.Sprintf("%s - %s - %s", a.ParentMID, a.EmailAddress, a.UserID)
    }
    

    To fix your problem fix the your xml file, and make your String() method like this :

    func (a *AccountUser) String() string {
        return ""
    }
    

    And here is the working code that I have tried with the above xml file :

    package main
    
    import (
        "encoding/xml"
        "fmt"
        "io/ioutil"
        "log"
        "os"
    )
    
    type AccountUser struct {
        UserID              string
        ActiveFlag          string
        LastSuccessfulLogin string
        PartnerProperties   Partner `xml:"PartnerProperties"`
    }
    
    type Partner struct {
        Name  string
        Value string
    }
    
    type Query struct {
        ResultList []AccountUser `xml:"Results"`
    }
    
    func (a *AccountUser) String() string {
        return fmt.Sprintf("%s - %s - %s", a.PartnerProperties.Name, a.PartnerProperties.Value, a.UserID)
    }
    
    func main() {
        xmlFile, err := os.Open("read.xml")
        if err != nil {
            fmt.Println("Error opening file:", err)
            return
        }
        defer xmlFile.Close()
    
        b, err := ioutil.ReadAll(xmlFile)
        if err != nil {
            log.Fatal(err)
        }
    
        fmt.Println("read result = ", string(b))
    
        var q Query
        err = xml.Unmarshal(b, &q)
        if err != nil {
            log.Fatal(err)
        }
    
        fmt.Println(q.ResultList[0].String())
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致