2013-04-24 4 views
5

मैं निम्नलिखित XML आउटपुट हासिल करने के लिए कोशिश कर रहा हूँमार्शलिंग एक्सएमएल जाओ XMLName + xmlns

<?xml version="1.0" encoding="UTF-8"?> 

<CreateHostedZoneRequest xmlns="https://route53.amazonaws.com/doc/2012-12-12/"> 
    <Name>DNS domain name</Name> 
    <CallerReference>unique description</CallerReference> 
    <HostedZoneConfig> 
     <Comment>optional comment</Comment> 
    </HostedZoneConfig> 
</CreateHostedZoneRequest> 

मैं जिसके बाद एक्सएमएल कि बहुत करीब हालांकि मैं CreateHostedZoneRequest

में
सांकेतिक शब्दों में बदलना करने में असमर्थ हैं है आउटपुट है

xmlns = "https://route53.amazonaws.com/doc/2012-12-12/

package main 

import "fmt" 
import "encoding/xml" 

type ZoneRequest struct { 
    Name   string 
    CallerReference string 
    Comment   string `xml:"HostedZoneConfig>Comment"` 
} 

var zoneRequest = ZoneRequest{ 
    Name:   "DNS domain name", 
    CallerReference: "unique description", 
    Comment:   "optional comment", 
} 

func main() { 
    tmp, _ := createHostedZoneXML(zoneRequest) 
    fmt.Println(tmp) 
} 

func createHostedZoneXML(zoneRequest ZoneRequest) (response string, err error) { 
    tmp := struct { 
    ZoneRequest 
    XMLName struct{} `xml:"CreateHostedZoneRequest"` 
    }{ZoneRequest: zoneRequest} 

    byteXML, err := xml.MarshalIndent(tmp, "", ` `) 
    if err != nil { 
    return "", err 
    } 
    response = xml.Header + string(byteXML) 
    return 
} 

http://play.golang.org/p/pyK76VPD5-

मैं CreateHostedZoneRequest में xmlns को कैसे एन्कोड कर सकता हूं?

उत्तर

3

आप जो संभवतः सबसे सुरुचिपूर्ण समाधान नहीं है, लेकिन

Playground link

type ZoneRequest struct { 
    Name   string 
    CallerReference string 
    Comment   string `xml:"HostedZoneConfig>Comment"` 
    Xmlns   string `xml:"xmlns,attr"` 
} 

var zoneRequest = ZoneRequest{ 
    Name:   "DNS domain name", 
    CallerReference: "unique description", 
    Comment:   "optional comment", 
    Xmlns:   "https://route53.amazonaws.com/doc/2012-12-12/", 
} 

उत्पादन

<?xml version="1.0" encoding="UTF-8"?> 

<CreateHostedZoneRequest xmlns="https://route53.amazonaws.com/doc/2012-12-12/"> 
    <Name>DNS domain name</Name> 
    <CallerReference>unique description</CallerReference> 
    <HostedZoneConfig> 
     <Comment>optional comment</Comment> 
    </HostedZoneConfig> 
</CreateHostedZoneRequest> 
5

मैं एक ऐसी ही सवाल था काम करने के लिए लगता है यह कर सकते हैं,। Unmarshal विधि के लिये दस्तावेज (http://golang.org/pkg/encoding/xml/#Unmarshal) है: XMLName क्षेत्र फार्म "नाम" या "नाम स्थान-URL नाम" का एक संबद्ध टैग है

हैं, तो XML तत्व दिए गए नाम होना आवश्यक है (और, वैकल्पिक रूप से, नाम स्थान) या अन्य Unmarshal एक त्रुटि देता है।

"नाम स्थान-URL नाम" का उपयोग struct टैग में:

type ZoneRequest struct { 
    XMLName xml.Name `xml:"https://route53.amazonaws.com/doc/2012-12-12/ CreateHostedZoneRequest"` 
} 

उत्पादन चाहिए:

<CreateHostedZoneRequest xmlns="https://route53.amazonaws.com/doc/2012-12-12/"> 
संबंधित मुद्दे