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" >
    <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>