Get attibuteDict of a subclass Xml parsing - Swift

2019-09-12 08:55发布

I want to get attribute of a subclass of my item :

  <pdv id="1000002" latitude="4621842" longitude="522767" cp="01000" pop="R">
<adresse>16 Avenue de Marboz</adresse>
<ville>BOURG-EN-BRESSE</ville>
<ouverture debut="01:00" fin="01:00" saufjour=""/>
<services>
  <service>Automate CB</service>
  <service>Vente de gaz domestique</service>
</services>
<prix nom="Gazole" id="1" maj="2015-05-30 11:30:17" valeur="1206"/>
<prix nom="SP95" id="2" maj="2015-05-30 11:30:17" valeur="1398"/>
<prix nom="SP98" id="6" maj="2015-05-30 11:30:17" valeur="1434"/>
<rupture id="3" nom="E85" debut="2009-11-03 12:19:00" fin=""/>
<fermeture/></pdv>

I know how to get <pdv id="000"> with attributeDict but I try to get <prix nom="example">

I try with that code :

func beginParsing()
{
    parser = NSXMLParser(contentsOfURL: testUrl)!
    parser.delegate = self
    parser.parse()
}

func parser(parser: NSXMLParser!, didStartElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!, attributes attributeDict: [NSObject : AnyObject]!)
{
    element = elementName
    if (elementName as NSString).isEqualToString("pdv")
    {

        cp = attributeDict["cp"]! as NSMutableString
        latitude = attributeDict["latitude"]! as NSMutableString
        longitude = attributeDict["longitude"]! as NSMutableString

        elements = NSMutableDictionary.alloc()
        elements = [:]
        PrixCarbu = NSMutableString.alloc()
        PrixCarbu = ""

    }

}

func parser(parser: NSXMLParser!, foundCharacters string: NSString!)
{

    if element.isEqualToString("prix") {
        PrixCarbu.appendString(string)

        func parser(parser: NSXMLParser!, didStartElement elementName: String!, namespaceURI: String!, qualifiedName: String!, attributes: [NSObject : AnyObject]!) {

            prixString = elementName
            if (elementName as NSString).isEqual("prix") {

                println(attributes)

            }

        }

    }

}


func parser(parser: NSXMLParser!, didEndElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!)
{
    if (elementName as NSString).isEqualToString("pdv") {
        if !cp.isEqual(nil) {
            elements.setObject(cp, forKey: "cp")
        }
        if !latitude.isEqual(nil) {
            elements.setObject(latitude, forKey: "latitude")
        }
        if !longitude.isEqual(nil) {
            elements.setObject(longitude, forKey: "longitude")
        }`

Thank's!

1条回答
太酷不给撩
2楼-- · 2019-09-12 09:31

You should put your "prix" check into the start element method. It is equivalent to "pdv", just use the same mechanism.

查看更多
登录 后发表回答