本文整理汇总了Golang中encoding/xml.MarshalIndent函数的典型用法代码### 示例。如果您正苦于以下问题:Golang MarshalIndent函数的具体用法?Golang MarshalIndent怎么用?Golang MarshalIndent使用的例子?那么恭喜您, 这里精选的函数代码### 示例或许可以为您提供帮助。

在下文中一共展示了MarshalIndent函数的20个代码### 示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码### 示例。

示例1: TestOutAndBackIn

func TestOutAndBackIn(t *testing.T) {

    const ID = "/test1"

    ref := newRoute(ID)

    x, err := xml.MarshalIndent(ref, "", "  ")
    if err != nil {
        t.Error(err)
    }

    /*t.Log("ref as xml is :", string(x))*/

    var fresh Route //only empty fields

    err = xml.Unmarshal(x, &fresh)
    if err != nil {
        t.Error(err)
    }

    var x2 []byte
    x2, err = xml.MarshalIndent(fresh, "", "  ")
    if err != nil {
        t.Error(err)
    }

    t.Log("fresh as xml is:", string(x2))

    if string(x) != string(x2) {
        t.Error("not equal, sorry MAC")
    }

}

开发者ID:docstream,项目名称:model-go,代码行数:33,代码来源:dbobjects_test.go

示例2: MakeRequest

func (c *Client) MakeRequest(otype, result string, updateCheck, isPing bool) (*omaha.Response, error) {
    client := &http.Client{}
    req := c.OmahaRequest(otype, result, updateCheck, isPing)
    raw, err := xml.MarshalIndent(req, "", " ")
    if err != nil {
        return nil, err
    }

    resp, err := client.Post(c.config.server+"/v1/update/", "text/xml", bytes.NewReader(raw))
    if err != nil {
        return nil, err
    }
    defer resp.Body.Close()

    oresp := new(omaha.Response)
    err = xml.NewDecoder(resp.Body).Decode(oresp)
    if err != nil {
        return nil, err
    }

    if instanceFlags.verbose {
        raw, _ := xml.MarshalIndent(req, "", " ")
        c.Log("request: %s\n", string(raw))
        raw, _ = xml.MarshalIndent(oresp, "", " ")
        c.Log("response: %s\n", string(raw))
    }

    return oresp, nil
}

开发者ID:stuart-warren,项目名称:updateservicectl,代码行数:29,代码来源:instance.go

示例3: processAtom

func (cs *contentStore) processAtom(rawContent []byte, topic Topic) {
    atomFeed := &feeds.AtomFeed{}
    err := xml.Unmarshal(rawContent, atomFeed)
    if err != nil {
        log.Println("Couldn't parse atom content", err.Error())
        return
    }

    items := cs.content[topic]
    if items == nil {
        cs.content[topic] = make(map[time.Time]string)
        items = cs.content[topic]
    }

    sortedDates := cs.contentSortedItems[topic]
    if sortedDates == nil {
        cs.contentSortedItems[topic] = make([]time.Time, 0, len(atomFeed.Entries))
        sortedDates = cs.contentSortedItems[topic]
    }

    for _, newItem := range atomFeed.Entries {
        date, err := time.Parse(time.RFC3339, newItem.Updated)
        if err != nil {
            log.Printf("Couldn't parse %s as a RFC3339 date. Not accepting this.", newItem.Updated)
            continue
        }

        content, err := xml.MarshalIndent(newItem, "", "  ")
        if err != nil {
            log.Println("Couldn't re-marshal element:", err.Error())
            continue
        }

        sortedDates = insertDate(sortedDates, date)
        items[date] = string(content)
    }

    cs.contentSortedItems[topic] = sortedDates

    // since no one is supposed to use it afterwards ...
    atomFeed.Entries = []*feeds.AtomEntry{}
    header, err := xml.MarshalIndent(atomFeed, "", "  ")
    if err != nil {
        log.Println("Error when re-marshaling header:", err.Error())
    }

    cs.contentHeader[topic] = string(header)
}

开发者ID:rakoo,项目名称:psgb,代码行数:48,代码来源:contentstore.go

示例4: openStreamAndGetFeatures

func (c *Client) openStreamAndGetFeatures() (*streamFeatures, error) {
    // Declare intent to be a xmpp client.
    openStream := fmt.Sprintf("<?xml version='1.0'?><stream:stream to='%s' xmlns='%s' xmlns:stream='%s' version='1.0'>",
        xmlEscape(c.domain), nsClient, nsStream)
    if Debug {
        fmt.Printf("===xmpp===send:\n%s\n", openStream)
    }
    fmt.Fprint(c.conn, openStream)

    // Server should respond with a stream opening.
    se, err := nextStart(c.p)
    if err != nil {
        return nil, err
    }

    if se.Name.Space != nsStream || se.Name.Local != "stream" {
        return nil, errors.New("xmpp: expected <stream> but got <" + se.Name.Local + "> in " + se.Name.Space)
    }

    features := &streamFeatures{}
    if err = c.p.DecodeElement(features, nil); err != nil {
        return nil, errors.New("unmarshal <features>: " + err.Error())
    }
    if Debug {
        bytes, err := xml.MarshalIndent(features, "", "    ")
        if err == nil {
            fmt.Printf("===xmpp===receive:%s\n", string(bytes))
        }
    }
    return features, nil
}

开发者ID:NoahShen,项目名称:go-xmpp,代码行数:31,代码来源:xmpp.go

示例5: WriteAsXml

// WriteAsXml is a convenience method for writing a value in xml (requires Xml tags on the value)
func (r *Response) WriteAsXml(value interface{}) error {
    var output []byte
    var err error

    if value == nil { // do not write a nil representation
        return nil
    }
    if r.prettyPrint {
        output, err = xml.MarshalIndent(value, " ", " ")
    } else {
        output, err = xml.Marshal(value)
    }

    if err != nil {
        return r.WriteError(http.StatusInternalServerError, err)
    }
    r.Header().Set(HEADER_ContentType, MIME_XML)
    if r.statusCode > 0 { // a WriteHeader was intercepted
        r.ResponseWriter.WriteHeader(r.statusCode)
    }
    _, err = r.Write([]byte(xml.Header))
    if err != nil {
        return err
    }
    if _, err = r.Write(output); err != nil {
        return err
    }
    return nil
}

开发者ID:shenjinxi,项目名称:go-restful,代码行数:30,代码来源:response.go

最后编辑: kuteng  文档更新时间: 2021-08-23 19:14   作者:kuteng