go 语言学习 ---解析xml

实例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>

  

转载于:https://www.cnblogs.com/saryli/p/11044636.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值