NSXMLparser解析()不起作用

时间:2014-12-11 00:56:01

标签: ios xml swift ios8 nsxmlparser

我正在开发一个游戏,其中有一些XML文件中描述的障碍模式,所以我在swift文件中实现了这个类:

class PatternsParser: NSObject, NSXMLParserDelegate
{
var myParser: NSXMLParser!
var arrayOfPatterns = [Element]()
var currentId = 0

init(url: NSURL)
{
    myParser = NSXMLParser(contentsOfURL: url)
    println(myParser.parserError?)
    super.init()
    myParser.delegate = self
    if myParser.parse()
    {
        println("Was able to parse")
    }
}

func parser(parser: NSXMLParser!, parseErrorOccurred parseError: NSError!) {
    println("error = \(parseError)")
}

在我写的另一个swift文件中:

// this is just a function to return the document path I use to create the NSURL
func getXMLPath(pathName path:String) -> String? 
{
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
    let documentsDirectory = paths[0] as String
    let filePath: String? = documentsDirectory?.stringByAppendingPathComponent(path)
    return filePath
}

    let buffer: NSURL! = NSURL(fileURLWithPath: getXMLPath(pathName:"patterns.xml"))
    var patternParser = PatternsParser(url:buffer)

然而,我得到的是myParser.parse()总是返回no,即使如此,parserError属性也没有告诉我任何事情(是零)。我也试过实现方法parseErrorOcurredvalidationErrorOcurred,但没有...

如果你想查看patterns.xml文件,请点击这里:

<?xml version="1.0"?>
<game>  
    <pattern id="0" difficulty="0" width="800">
        <coinArea>
            <rect x="100" y="470,5" width="12" height="5"/>
        </coinArea>
    </pattern>

    <pattern id="1" difficulty="1" width="600">
        <coinArea>
            <rect x="100" edge="up" width="8"/>
        </coinArea>
        <wall orientation="vertical" texture="wallText">
            <rect x="300" edge="down"/>
        </wall>
    </pattern>

    <pattern id="2" difficulty="1" width="750">
        <wall orientation="vertical" texture="wallText">
            <rect edge="down" x="25"/>
        </wall>
        <wall orientation="vertical" texture="wallText">
            <rect edge="up" x="60"/>
        </wall>
        <electricLine orientation="horizontal">
            <rect edge="up" x="0"/>
        </electricLine>
    </pattern>
</game>

2 个答案:

答案 0 :(得分:1)

  1. 原始问题中的XML无效。您可以从Mac OS X xmllint运行Terminal命令,这将指出(a)您缺少根元素来包装您的个人pattern标记; (b)您的electricLine开放标记输入错误。它应该是:

    <?xml version="1.0"?>
    <patterns>
        <pattern id="0" difficulty="0" width="800">
            <coinArea>
                <rect x="100" y="470,5" width="12" height="5"/>
            </coinArea>
        </pattern>
    
        <pattern id="1" difficulty="1" width="600">
            <coinArea>
                <rect x="100" edge="up" width="8"/>
            </coinArea>
            <wall orientation="vertical" texture="wallText">
                <rect x="300" edge="down"/>
            </wall>
        </pattern>
    
        <pattern id="2" difficulty="1" width="750">
            <wall orientation="vertical" texture="wallText">
                <rect edge="down" x="25"/>
            </wall>
            <wall orientation="vertical" texture="wallText">
                <rect edge="up" x="60"/>
            </wall>
            <electricLine orientation="horizontal">
                <rect edge="up" x="0"/>
            </electricLine>
        </pattern>
    </patterns>
    

    您在修订后的问题中解决了此问题。新的XML看起来很好。

  2. 如果您实施了parseErrorOccurred协议的NSXMLParserDelegate方法,它就会指出这些问题。

    我使用以下parseErrorOccurred方法运行原始的,不正确的XML:

    func parser(parser: NSXMLParser!, parseErrorOccurred parseError: NSError!) {
        println("error = \(parseError)")
    }
    

    它正确报道:

      

    error = Error Domain = NSXMLParserErrorDomain Code = 5&#34;无法完成操作。 (NSXMLParserErrorDomain错误5。)&#34; UserInfo = 0x60000006eb40 {NSXMLParserErrorColumn = 1,NSXMLParserErrorLineNumber = 9,NSXMLParserErrorMessage =文档末尾的额外内容   }

  3. 如果您的委托方法没有被调用,请确保该文件存在于Document文件夹中:

    let path = getXMLPath(pathName:"patterns.xml")!
    if !NSFileManager.defaultManager().fileExistsAtPath(path) {
        println("file NOT found at \(path)")
    } else {
        println("file found at \(path)")
    }
    let fileURL: NSURL! = NSURL(fileURLWithPath: path)
    var patternParser = PatternsParser(url: fileURL)
    

答案 1 :(得分:0)

我的解决方案

我的问题确实是网址,因此不使用

func getXMLPath(pathName path:String) -> String? 
{
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
    let documentsDirectory = paths[0] as String
    let filePath: String? = documentsDirectory?.stringByAppendingPathComponent(path)
    return filePath
}

let buffer: NSURL! = NSURL(fileURLWithPath: getXMLPath(pathName:"patterns.xml"))
var patternParser = PatternsParser(url:buffer)

我将其替换为:

let bundle = NSBundle.mainBundle()
let url: NSURL! = bundle.URLForResource("patterns", withExtension: "xml")
var patternParser = PatternsParser(url: url)