douxi3233
douxi3233
2017-11-15 22:43
浏览 141

逐行读取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
    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())
    }
    
    点赞 评论
  • douzhang2092
    douzhang2092 2017-11-15 23:03

    From my limited knowledge your assignment needs to be a pointer so try Accounts *AccountUser instead of Accounts AccountUser

    You might want to supply the XML so it is easier for others to understand and show examples with.

    点赞 评论

相关推荐