XMLParser在“ß”字符上拆分元素

时间:2017-03-24 22:07:11

标签: cocoa nsxmlparser foundation

我在iOS Playground中使用此代码(Swift 3,Xcode 8.2.1):

import UIKit
import PlaygroundSupport

PlaygroundPage.current.needsIndefiniteExecution = true

class ParserDelegate: NSObject, XMLParserDelegate {

    @objc func parser(_ parser: XMLParser, foundCharacters string: String) {
        print("found string:", string)
    }

    func parser(_ parser: XMLParser, parseErrorOccurred parseError: Error) {
        print("error:", parseError)
    }

    func parserDidEndDocument(_ parser: XMLParser) {
        PlaygroundPage.current.finishExecution()
    }

}

let string = "<xml>straße</xml>"
let parser = XMLParser(data: string.data(using: .utf8)!)
let delegate = ParserDelegate()
parser.delegate = delegate
parser.parse()

// prints this:
// found string: stra
// found string: ße

为什么XMLParserstraße拆分为straße,而不是将其全部解析为一个字符串?有没有一种简单的方法,除了连接parser(_:foundCharacters:)找到的所有字符串,直到我打电话给parser(_:didEndElement:namespaceURI:qualifiedName:)

1 个答案:

答案 0 :(得分:0)

关心解析器如何分解一段文本并不是你的事。无论在parser(_:foundCharacters:)到达之前调用多少次,都可以以这样的方式实现didEndElement来实现func parser(_ parser: XMLParser, foundCharacters string: String) { self.text = self.text + string } 。典型的实现将如下所示:

self.text

...其中didStartElement是一个属性,在didEndElementfoundCharacters中进行管理。

  

这是否有一个简单的方法

这是一种看待它的非常愚蠢的方式。这不是你需要的一种方式&#34;方式&#34;。这是实施09 <?php include 'db.php'; 10 $vin = $_GET['VIN']; 11 $query = "SELECT * FROM INVENTORY"; 12 /* Try to query the database */ 13 if($result = $mysqli->query($query)){ 14 //Don't do anything if successful. 15 } 16 else 17 { 18 echo "Sorry, a vehicle with VIN of $vin cannot be found ".mysql_error()."<br>"; 19 } 20 21 //Loop through all the rows returned by the query, creating a table row for each 22 while ($result_ar = mysqli_fetch_assoc($result)){ 23 $year = $result_ar['YEAR']; 24 $make = $result_ar['Make']; 25 $model = $result_ar['Model']; 26 $trim = $result_ar['TRIM']; 27 $color = $result_ar['EXT_COLOR']; 28 $interior = $result_ar['INT_COLOR']; 29 $mileage = $result_ar['MILEAGE']; 30 $transmission = $result_ar['TRANSMISSION']; 31 $price = $result_ar['ASKING_PRICE']; 32 } 33 echo "$year $make $model </p>"; 34 echo "<p>Asking Price: $price </p>"; 35 echo "<p>Exterior Color: $color </p>"; 36 echo "<p>Interior Color: $interior </p>"; 37 38 $mysqli->close(); 39 ?> 的正确方法。做到这一点并继续生活。

相关问题