• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

go语言学习---解析xml

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

实例1

//main
package main

import (
	"bytes"
	"encoding/xml"
	"fmt"
	"io/ioutil"
)
//获取属性值
func getAttributeValue(attr []xml.Attr, name string) string {
	for _, a := range attr {
		if a.Name.Local == name {
			return a.Value
		}
	}
	return ""
}

func main() {
	content, err := ioutil.ReadFile("KYTT.plym")
	decoder := xml.NewDecoder(bytes.NewBuffer(content))

	var t xml.Token
	var inItemGroup bool
	for t, err = decoder.Token(); err == nil; t, err = decoder.Token() {
		switch token := t.(type) {
		case xml.StartElement:
			name := token.Name.Local
			//fmt.Println(name)
			if inItemGroup {
				if name == "Window" {
					//fmt.Println(name)
					fmt.Println(getAttributeValue(token.Attr, "Width"))
				}

			} else {
				if name == "Playlist" {
					inItemGroup = true
				}
			}
		case xml.EndElement:
			if inItemGroup {
				if token.Name.Local == "Playlist" {
					inItemGroup = false
				}
			}
		}
	}
}

 

KYTT.plym内容
<?xml version="1.0"?>
<PlayProgram X="0" Y="0" Width="1152" Height="512">
  <Playlist Type="TimeSegment" Name="常规段1" Date="05/10/2019#05/11/2019" Day="True#True#True#True#True#True#True" Time="22:00:00#07:00:00" IsSpeficTimeZone="False" DiffToUTC="00:00:00" ID="0">
    <Context>
      <BasicPage>
        <Page Name="节目1" PlayType="Order" Duration="00:06:00" PlayTimes="1" BackColor="255#0#0#0" BackgroundImage="" ImageLayout="Stretch" BackMusic="" CustomString="">
          <Window Name="通用窗口1" X="0" Y="0" Width="1152" Height="512" Tag="Common">
            <Item Type="0">
              <Media>
                <VideoMedia>
              <Name>22.mp4</Name>
              <PlayDuration>
                <string>0#0#45#13#281</string>
              </PlayDuration>
              <BeginTime>0001-01-01T00:00:00</BeginTime>
              <EndTime>0001-01-01T00:00:00</EndTime>
              <Times>-1</Times>
              <BackColor>
                <string>255#0#0#0</string>
              </BackColor>
              <BackImagePath />
              <BackImageLayout>Stretch</BackImageLayout>
              <Opacity>1</Opacity>
              <ID>103</ID>
              <EnableBorderElement>false</EnableBorderElement>
              <BorderElement>
                <BorderType>6</BorderType>
                <IsClockWise>true</IsClockWise>
                <Speed>5</Speed>
                <BorderWidth>1</BorderWidth>
                <BackColor>
                  <string>255#255#0#0</string>
                </BackColor>
                <ForeColor>
                  <string>255#0#128#0</string>
                </ForeColor>
                <ColorType>0</ColorType>
                <BorderDirectionStyle>0</BorderDirectionStyle>
                <BorderSurroundedType>0</BorderSurroundedType>
                <BorderUnitData>
                  <string>424d960000000000000036000000280000002000000001000000010018000000000000000000202e0000202e00000000000000000000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff00000000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff00ff0000ff0000ff0000ff0000ff0000ff0000ff0000ff0000ffff00ffff00ffff00ffff00ffff00ffff00ffff00ffff</string>
                </BorderUnitData>
                <BorderLeftUnitData>
                  <string />
                </BorderLeftUnitData>
                <BorderRightUnitData>
                  <string />
                </BorderRightUnitData>
                <BorderBottomUnitData>
                  <string />
                </BorderBottomUnitData>
              </BorderElement>
              <Tag />
              <FinishedMode>PlayInSpecTime</FinishedMode>
              <RotateAngle>None</RotateAngle>
              <Path>G:\BaiduNetdiskDownload\22.mp4</Path>
              <DispRatioType>Full</DispRatioType>
              <TxtElement>
                <TextFont>
                  <string>SimSun#12#Regular#Point#12</string>
                </TextFont>
                <TextColor>
                  <string>255#255#0#0</string>
                </TextColor>
                <IsTextEffect>false</IsTextEffect>
                <TextEffectType>0</TextEffectType>
                <TextEffectColor>
                  <string>255#255#255#0</string>
                </TextEffectColor>
                <TextEffectWidth>2</TextEffectWidth>
                <TextAlignment>TopLeft</TextAlignment>
              </TxtElement>
              <IsShowTextElement>false</IsShowTextElement>
              <VolumnPercent>100</VolumnPercent>
              <RotateType>None</RotateType>
              <IsStartFromSpecificPos>false</IsStartFromSpecificPos>
              <StartPosition>
                <string>0#0#0#0#0</string>
              </StartPosition>
              <EndPosition>
                <string>0#0#45#13#281</string>
              </EndPosition>
            </VideoMedia>
              </Media>
              <AdditionalInfo />
            </Item>
          </Window>
        </Page>
      </BasicPage>
      <GlobalPage>
        <Page />
      </GlobalPage>
    </Context>
  </Playlist>
</PlayProgram>

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Go语言圣经习题练习_1.6并发获取多个URL发布时间:2022-07-10
下一篇:
Go语言标准库之fmt包发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap