NSXMLParser 访问字符串值错误 [英] NSXMLParser accessing string values error

查看:23
本文介绍了NSXMLParser 访问字符串值错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的xml数据

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.6.0_27" class="java.beans.XMLDecoder">
 <object class="java.util.ArrayList">
  <void method="add">
   <object class="impl.DictSpotIndexImpl">
    <void property="englishMeaning">
     <string></string>
    </void>
    <void property="englishWordName">
     <string> dendritical</string>
    </void>
    <void property="tamilMeaning">
     <string>Empty</string>
    </void>
    <void property="tamilWordName">
     <string>மரம் போன்ற வரைததடங்களையுடைய படி அடுக்கியற் பாறை</string>
    </void>
    <void property="wordClass">
     <string>regular</string>
    </void>
    <void property="wordConstraints">
     <string></string>
    </void>
    <void property="wordDataSource">
     <string>Source Not found</string>
    </void>
    <void property="wordImages">
     <string></string>
    </void>
    <void property="wordPopularity">
     <string></string>
    </void>
    <void property="wordTypes">
     <string>பெயரடை</string>
    </void>
   </object>
  </void>
 </object>
</java>

现在我想获取属性 tamilWordName 内的值.我怎样才能做到这一点.?任何建议将不胜感激.

Now i want to get the values which is inside of the property tamilWordName. How can i do this.? Any suggestion would be really appreciated.

到目前为止我是这样尝试的,真的不知道如何处理属性.

So far i tried like this, really no idea about how to deal with the attributes.

//
//  xmlParser.swift
//  Dictionary
//
//  Created by Alvin Varghese on 27/Oct/14.
//  Copyright (c) 2014 Karky Research Foundation. All rights reserved.
//

import Cocoa

class xmlParser: NSObject, NSXMLParserDelegate
{

    var element : String?

    func doParse(filePath : String)
    {
        var data : NSData = NSData(contentsOfFile: filePath)!

        var xmlParser : NSXMLParser = NSXMLParser(data: data)!
        xmlParser.delegate = self
        var success : Bool = xmlParser.parse()

        if success
        {
        }

    }


    func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [NSObject : AnyObject])
    {
        element = elementName

        if elementName == "void.tamilWordName"
        {
            println(elementName)
        }


    }

    func parser(parser: NSXMLParser, foundCharacters string: String)
    {

    }


    func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String, qualifiedName qName: String)
    {


    }



    func parser(parser: NSXMLParser, parseErrorOccurred parseError: NSError)
    {

    }





}

推荐答案

这里是处理属性的代码片段.

Here is a code snippet for dealing with attributes.

如果元素名称是 void,则必须检查属性.如果存在名称为 property 的属性,请检查该值是否为 tamilWordName.一旦找到层次结构中的所有元素,设置一个布尔标志并等待 didStartElement 中的下一个元素.只要您在 didEndElement 中找到元素名称 void,就处理它们.

If the element name is void you have to check for attributes. If an attribute with the name property exists, check if the value is tamilWordName. Once you found all the in the hierachy, set a boolean flag and wait for the next element(s) to come in didStartElement. Process them as long as you found the element name void in didEndElement.

func parser(parser: NSXMLParser!, didStartElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!, attributes attributeDict: [NSObject : AnyObject]!) {
    self.currentElement = elementName  // helper variable to hold the current element

    if elementName == "void" {
        let attrs = attributeDict as [String: NSString]

        if let prop = attrs["property"] {
            if prop == "tamilWordName" {
                // found it, now set a boolean property variable and process the elements coming
                self.tamilWordName = true
            }
        }
    }
}

func parser(parser: NSXMLParser!, foundCharacters string: String!) {
    if currentElement == "string" && self.tamilWordName {
        // append to variable which holds the string
        self.tamilWordString += string
    }        
}

func parser(parser: NSXMLParser!, didEndElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!) {
    if elementName == "string" && self.tamilWordName {
        // string element inside tamilWordName closed, process it here
    }
    else if elementName == "void" && self.tamilWordName {
        // void element with property tamilWordName closed
        self.tamilWordName = false
    }            
}

我希望你能在这里得到这个想法.

I hope you get the idea here.

这篇关于NSXMLParser 访问字符串值错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆