小编Sch*_*uki的帖子

编组XML Go XMLName + xmlns

我试图获得以下XML输出

<?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>
Run Code Online (Sandbox Code Playgroud)

我有以下输出非常接近的XML,但我无法编码到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, …
Run Code Online (Sandbox Code Playgroud)

xml marshalling go

8
推荐指数
2
解决办法
5421
查看次数

如何使用ElementTree在XML中查找特定元素

我正在尝试解析从youtube视频源获取的XML字符串,使用Python 3.3.1.这是代码:

import re
import sys
import urllib.request
import urllib.parse
import xml.etree.ElementTree as element_tree

def get_video_id(video_url):
    return re.search(r'watch\?v=.*', video_url).group(0)[8:]

def get_video_feed(video_url):
    video_feed = "http://gdata.youtube.com/feeds/api/videos/" + get_video_id(video_url)
    return urllib.request.urlopen(video_feed).read()

def get_media_info(video_url):
    content = get_video_feed(video_url)
    content = str(content, 'ascii')
    media = {}
    e = element_tree.XML(content);

    print ( "CONTENT: \n" + content )

    print ( "\n\nELEMENTS : \n")
    for i in list(e):
        print (i) 

    media['title'] = e.findall('title')   //NOTE THIS!
    return media


def main():
    video_url = 'http://youtube.com/watch?v=q5sOLzEerwA' 

    print ( get_media_info(video_url) )

if …
Run Code Online (Sandbox Code Playgroud)

python xml youtube-api elementtree python-3.x

3
推荐指数
2
解决办法
2万
查看次数

标签 统计

xml ×2

elementtree ×1

go ×1

marshalling ×1

python ×1

python-3.x ×1

youtube-api ×1