gpt4 book ai didi

ios - NSXMLParser 访问字符串值错误

转载 作者:行者123 更新时间:2023-11-29 02:33:51 26 4
gpt4 key购买 nike

我有这样的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 中的值。我怎样才能做到这一点。?任何建议将不胜感激。

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

//
// 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)
{

}





}

最佳答案

这是处理属性的代码片段。

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

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

我希望你能明白这里的意思。

关于ios - NSXMLParser 访问字符串值错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26588119/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com