本文整理汇总了Golang中encoding/xml.EscapeText函数的典型用法代码示例。如果您正苦于以下问题:Golang EscapeText函数的具体用法?Golang EscapeText怎么用?Golang EscapeText使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了EscapeText函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: encodeDomainCheck
func encodeDomainCheck(buf *bytes.Buffer, domains []string, extFee bool) error {
buf.Reset()
buf.WriteString(xmlCommandPrefix)
buf.WriteString(`<check>`)
buf.WriteString(`<domain:check xmlns:domain="urn:ietf:params:xml:ns:domain-1.0">`)
for _, domain := range domains {
buf.WriteString(`<domain:name>`)
xml.EscapeText(buf, []byte(domain))
buf.WriteString(`</domain:name>`)
}
buf.WriteString(`</domain:check>`)
buf.WriteString(`</check>`)
if extFee {
// Extensions
buf.WriteString(`<extension>`)
// CentralNic fee extension
buf.WriteString(`<fee:check xmlns:fee="urn:ietf:params:xml:ns:fee-0.5">`)
for _, domain := range domains {
buf.WriteString(`<fee:domain>`)
buf.WriteString(`<fee:name>`)
xml.EscapeText(buf, []byte(domain))
buf.WriteString(`</fee:name>`)
buf.WriteString(`<fee:command>create</fee:command>`)
buf.WriteString(`</fee:domain>`)
}
buf.WriteString(`</fee:check>`)
buf.WriteString(`</extension>`)
}
buf.WriteString(xmlCommandSuffix)
return nil
}
示例2: value_to_xml
func value_to_xml(v interface{}) (string, error) {
switch t := v.(type) {
case int, int8, int64, int32, *int, *int8, *int64, *int32:
return fmt.Sprintf("<int>%d</int>", t), nil
case string:
buf := bytes.NewBuffer(make([]byte, 0))
err := xml.EscapeText(buf, []byte(t))
if err != nil {
return "", err
}
return fmt.Sprintf("<string>%s</string>", buf.String()), nil
case []byte:
return fmt.Sprintf("<base64>%s</base64>", base64.StdEncoding.EncodeToString(t)), nil
case float32, float64, *float32, *float64:
return fmt.Sprintf("<double>%f</double>", t), nil
case bool:
return fmt.Sprintf("<boolean>%d</boolean>", btoi(t)), nil
case time.Time:
buf := bytes.NewBuffer(make([]byte, 0))
err := xml.EscapeText(buf, []byte(t.Format(time_ISO8601_FORMAT)))
if err != nil {
return "", err
}
return fmt.Sprintf("<dateTime.iso8601>%s</dateTime.iso8601>", buf.String()), nil
case *time.Time:
buf := bytes.NewBuffer(make([]byte, 0))
err := xml.EscapeText(buf, []byte(t.Format(time_ISO8601_FORMAT)))
if err != nil {
return "", err
}
return fmt.Sprintf("<dateTime.iso8601>%s</dateTime.iso8601>", buf.String()), nil
case []interface{}:
tmp_str_arr := []string{"<array><data>"}
for _, v := range t {
subval, err := value_to_xml(v)
if err != nil {
return "", err
}
tmp_str_arr = append(tmp_str_arr, fmt.Sprintf("<value>%s</value>", subval))
}
tmp_str_arr = append(tmp_str_arr, "</data></array>")
return strings.Join(tmp_str_arr, ""), nil
case map[string]interface{}:
tmp_str_arr := []string{"<struct>"}
for name, item := range t {
tmp_str_arr = append(tmp_str_arr, "<member>")
tmp_str_arr = append(tmp_str_arr, fmt.Sprintf("<name>%s</name>", name))
subval, err := value_to_xml(item)
if err != nil {
return "", err
}
tmp_str_arr = append(tmp_str_arr, fmt.Sprintf("<value>%s</value>", subval))
tmp_str_arr = append(tmp_str_arr, "</member>")
}
tmp_str_arr = append(tmp_str_arr, "</struct>")
return strings.Join(tmp_str_arr, ""), nil
default:
return "", errors.New("Unrecognized type")
}
}
示例3: main
func main() {
inputReader := strings.NewReader(input)
decoder := xml.NewDecoder(inputReader)
for {
// Read tokens from the XML document in a stream.
t, _ := decoder.Token()
if t == nil {
break
}
// Inspect the type of the token just read.
//fmt.Printf("\n<!--%#v-->\n", t)
switch x := t.(type) {
case xml.StartElement:
fmt.Printf("<%s", x.Name.Local)
for _, attr := range x.Attr {
fmt.Printf(" %s=\"", attr.Name.Local)
xml.EscapeText(os.Stdout, []byte(attr.Value))
os.Stdout.Write([]byte{'"'})
}
fmt.Printf(">")
case xml.CharData:
xml.EscapeText(os.Stdout, bytes.TrimSpace(x))
case xml.EndElement:
fmt.Printf("</%s>\n", x.Name.Local)
}
}
}
示例4: genQuery
// meh3
func genQuery(query string, args dict) []byte {
var out bytes.Buffer
out.WriteString("<query")
for key, val := range args {
fmt.Fprintf(&out, " %s=\"", key)
xml.EscapeText(&out, []byte(val))
out.WriteString("\"")
}
out.WriteString(">")
xml.EscapeText(&out, []byte(query))
out.WriteString("</query>\r\n.\r\n")
return out.Bytes()
}
示例5: ToXML
// ToXML marshal map[string]string to xmlWriter with xml format, the root node name is xml.
// NOTE: This function assumes the key of m map[string]string are legitimate xml name string
// that does not contain the required escape character!
func (x *Xml) ToXML(xmlWriter io.Writer, m map[string]string) (err error) {
if xmlWriter == nil {
return errors.New("nil xmlWriter")
}
if _, err = io.WriteString(xmlWriter, "<xml>"); err != nil {
return
}
for k, v := range m {
if _, err = io.WriteString(xmlWriter, "<"+k+">"); err != nil {
return
}
if err = xml.EscapeText(xmlWriter, []byte(v)); err != nil {
return
}
if _, err = io.WriteString(xmlWriter, "</"+k+">"); err != nil {
return
}
}
if _, err = io.WriteString(xmlWriter, "</xml>"); err != nil {
return
}
return
}
示例6: WriteQuad
func (w *Writer) WriteQuad(q quad.Quad) error {
if w.err != nil {
return w.err
}
if !w.written {
if _, err := w.w.Write([]byte(header)); err != nil {
return err
}
w.written = true
w.nodes = make(map[string]int)
}
s := w.writeNode(quad.StringOf(q.Subject))
o := w.writeNode(quad.StringOf(q.Object))
if w.err != nil {
return w.err
}
_, w.err = fmt.Fprintf(w.w, "\t\t<edge source=\"n%d\" target=\"n%d\"><data key=\"d1\">", s, o)
if w.err != nil {
return w.err
}
if w.err = xml.EscapeText(w.w, []byte(quad.StringOf(q.Predicate))); w.err != nil {
return w.err
}
_, w.err = w.w.Write([]byte("</data></edge>\n"))
return w.err
}
示例7: writeFault
// Return an XML-RPC fault
func writeFault(out io.Writer, code int, msg string) {
fmt.Fprintf(out, `<?xml version="1.0"?>
<methodResponse>
<fault>
<value>
<struct>
<member>
<name>faultCode</name>
<value><int>%d</int></value>
</member>
<member>
<name>faultString</name>
<value>`, code)
err := xml.EscapeText(out, []byte(msg))
fmt.Fprintf(out, `</value>
</member>
</struct>
</value>
</fault>
</methodResponse>`)
// XXX dump the error to Stderr for now
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot write fault#%d(%s): %v\n", code, msg,
err)
}
}
示例8: httpAPIVoiceMessage
// The HTTP voice processing endpoint reads DTMF (input from Twilio request) and translates it into a statement and execute.
func (sh *WebShell) httpAPIVoiceMessage(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/xml")
w.Header().Set("Cache-Control", "must-revalidate")
w.WriteHeader(http.StatusOK)
digits := r.FormValue("Digits")
decodedLetters := voiceDecodeDTMF(digits)
log.Printf("Voice message got digits: %s (decoded - %s)", digits, decodedLetters)
if cmd := sh.cmdFind(decodedLetters); cmd == "" {
// PIN mismatch
w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say>Sorry</Say>
<Hangup/>
</Response>
`))
} else {
// Speak the command result and repeat
output := sh.cmdRun(cmd, sh.WebTimeoutSec, sh.WebTruncateLen, true, true)
var escapeOutput bytes.Buffer
if err := xml.EscapeText(&escapeOutput, []byte(output)); err != nil {
log.Printf("XML escape failed - %v", err)
}
w.Write([]byte(fmt.Sprintf(`<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Gather action="%s%s" method="POST" timeout="15" finishOnKey="#" numDigits="1000">
<Say voice="man" loop="1" language="en">%s over</Say>
</Gather>
</Response>
`, sh.VoiceEndpointPrefix, sh.VoiceProcEndpoint, escapeOutput.String())))
}
}
示例9: serializeItem
func serializeItem(k string, v interface{}, w io.Writer) {
w.Write([]byte(`<item key="`))
xml.EscapeText(w, []byte(k))
w.Write([]byte(`">`))
switch i := v.(type) {
case string:
xml.EscapeText(w, []byte(i))
case NestedStringMap:
serializeMap(i, w)
case []NestedStringMap:
serializeNSMArray(i, w)
case []string:
serializeStringArray(i, w)
}
w.Write([]byte(`</item>`))
}
示例10: NewCommand
func (s *Shell) NewCommand(cmd string, arguments []string) (string, error) {
var buf = bytes.NewBuffer(make([]byte, 0, len(cmd)))
if e := xml.EscapeText(buf, []byte(cmd)); nil != e {
return "", e
}
env := &envelope.CreateCommand{Uuid(), s.Id, strings.Replace(buf.String(), """, """, -1), arguments}
reader, err := s.Deliver(bytes.NewBufferString(env.Xml()))
if err != nil {
return "", err
}
defer closeReader(reader)
decoder := xml.NewDecoder(reader)
if err := ReadEnvelopeBody(decoder); nil != err {
return "", err
}
ok, err := locateElements(decoder, []string{"CommandResponse", "CommandId"})
if nil != err {
return "", errors.New("locate 'Envelope/Body/CommandResponse/CommandId' failed, " + err.Error())
}
if !ok {
return "", ElementNotExists("Envelope/Body/CommandResponse/CommandId")
}
id, e := readXmlText(decoder)
if nil != e {
return "", errors.New("read CommandId from the response failed, " + e.Error())
}
return id, nil
}
示例11: FormatMapToXML
// 格式化 map[string]string 为 xml 格式
// 用于微信支付
func FormatMapToXML(xmlWriter io.Writer, Map map[string]string) (err error) {
if xmlWriter == nil {
return errors.New("xmlWriter == nil")
}
_, err = io.WriteString(xmlWriter, "<xml>\n")
if err != nil {
return
}
for key, value := range Map {
_, err = io.WriteString(xmlWriter, "<"+key+">")
if err != nil {
return
}
if err = xml.EscapeText(xmlWriter, []byte(value)); err != nil {
return
}
_, err = io.WriteString(xmlWriter, "</"+key+">\n")
if err != nil {
return
}
}
_, err = io.WriteString(xmlWriter, "</xml>")
if err != nil {
return
}
return
}
示例12: escapeXml
func escapeXml(in string) string {
var out bytes.Buffer
err := xml.EscapeText(&out, []byte(in))
if err != nil {
panic("Could not escape XML: " + err.Error())
}
return out.String()
}
示例13: printText
func (this *Node) printText() []byte {
val := []byte(this.Value)
if len(this.Parent.Children) > 1 {
return val
}
var b bytes.Buffer
xml.EscapeText(&b, val)
return b.Bytes()
}
示例14: escapeXml
func escapeXml(s string) string {
buf := new(bytes.Buffer)
buf.Grow(len(s))
err := xml.EscapeText(buf, []byte(s))
if err != nil {
panic(err)
}
return buf.String()
}
示例15: escapeXmlText
func escapeXmlText(s string) (string, error) {
buffer := new(bytes.Buffer)
err := xml.EscapeText(buffer, []byte(s))
if err != nil {
return "", err
}
return buffer.String(), nil
}