如何快速从XML获取属性值?

时间:2018-08-01 03:13:02

标签: swift xml attributes

示例XML显示如下:

<?xml version="1.0"?>
 <PackData name="Player Data" desc="The Player Data download - generated at        15-Jun-2018 05:00:03">
   <PlayerData baseImageUrl="//xxx.org/images/player/">
     <P id="2771" f="Hern&#xE1;ndez" s="XAVI" d="1980-01-25" h="170" w="68"   i="2771.jpg"/>
     <P id="2772" f="Lionel" s="MESSI" d="1987-06-24" h="170" w="72" i="2772.png"/>  
   </PlayerData>
</PackData>

我用来解析XML的代码:

  if let xmlPath = Bundle.main.path(forResource: "playerdata", ofType: "xml"){ 

    do{
        let contents = try String(contentsOfFile: xmlPath)
        //print(contents)
    }
    catch{
        print(error)
    }
}
else{
     print("wrong")
}

我可以将XML转换为字符串,但是我不知道如何访问那些属性值。当用户在命令行中输入“ MESSI”时,如何获得“ 2772.png”?谢谢。

0 个答案:

没有答案