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条)

报告相同问题?

悬赏问题

  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复