我想解析以下响应,并仅将“ pdf-info”标记的值保存为字符串,
let serverResponse = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<xfdf xmlns=\"http://ns.adobe.com/xfdf/\" xml:space=\"preserve\">\n\t<pdf-info version=\"1\" xmlns=\"http://www.pdftron.com/pdfinfo\">\n\t\t<field max-len=\"40\" name=\"Given Name Text Box\" type=\"Tx\">\n\t\t\t<font type=\"TrueType\" size=\"11\" name=\"ArialMT\" />\n\t\t</field>\n\t\t<field max-len=\"40\" name=\"Family Name Text Box\" type=\"Tx\">\n\t\t\t<font type=\"TrueType\" size=\"11\" name=\"ArialMT\" />\n\t\t</field>\n\t\t<field max-len=\"20\" name=\"House nr Text Box\" type=\"Tx\">\n\t\t\t<font type=\"TrueType\" size=\"11\" name=\"ArialMT\" />\n\t\t</field>\n\t\t<field max-len=\"40\" name=\"Address 2 Text Box\" type=\"Tx\">\n\t\t\t<font type=\"TrueType\" size=\"11\" name=\"ArialMT\" />\n\t\t</field>\n\t\t<field max-len=\"20\" name=\"Postcode Text Box\" type=\"Tx\">\n\t\t\t<font type=\"TrueType\" size=\"11\" name=\"ArialMT\" />\n\t\t</field>\n\t\t<field flags=\"Combo Edit\" name=\"Country Combo Box\" type=\"Ch\">\n\t\t\t<font type=\"TrueType\" size=\"11\" name=\"ArialMT\" />\n\t\t\t<options>\n\t\t\t\t<option value=\"Austria\" />\n\t\t\t\t<option value=\"Belgium\" />\n\t\t\t\t<option value=\"Britain\" />\n\t\t\t\t<option value=\"Bulgaria\" />\n\t\t\t\t<option value=\"Croatia\" />\n\t\t\t\t<option value=\"Cyprus\" />\n\t\t\t\t<option value=\"Czech-Republic\" />\n\t\t\t\t<option value=\"Denmark\" />\n\t\t\t\t<option value=\"Estonia\" />\n\t\t\t\t<option value=\"Finland\" />\n\t\t\t\t<option value=\"France\" />\n\t\t\t\t<option value=\"Germany\" />\n\t\t\t\t<option value=\"Greece\" />\n\t\t\t\t<option value=\"Hungary\" />\n\t\t\t\t<option value=\"Ireland\" />\n\t\t\t\t<option value=\"Italy\" />\n\t\t\t\t<option value=\"Latvia\" />\n\t\t\t\t<option value=\"Lithuania\" />\n\t\t\t\t<option value=\"Luxembourg\" />\n\t\t\t\t<option value=\"Malta\" />\n\t\t\t\t<option value=\"Netherlands\" />\n\t\t\t\t<option value=\"Poland\" />\n\t\t\t\t<option value=\"Portugal\" />\n\t\t\t\t<option value=\"Romania\" />\n\t\t\t\t<option value=\"Slovakia\" />\n\t\t\t\t<option value=\"Slovenia\" />\n\t\t\t\t<option value=\"Spain\" />\n\t\t\t\t<option value=\"Sweden\" />\n\t\t\t</options>\n\t\t</field>\n\t\t<field max-len=\"20\" name=\"Height Formatted Field\" type=\"Tx\">\n\t\t\t<default-value>150</default-value>\n\t\t\t<font type=\"TrueType\" size=\"11\" name=\"ArialMT\" />\n\t\t</field>\n\t\t<field max-len=\"40\" name=\"City Text Box\" type=\"Tx\">\n\t\t\t<font type=\"TrueType\" size=\"11\" name=\"ArialMT\" />\n\t\t</field>\n\t\t<field flags=\"NoToggleToOff\" name=\"Driving License Check Box\" type=\"Btn\">\n\t\t\t<default-value>Off</default-value>\n\t\t\t<font name=\"ZapfDingbats\" />\n\t\t</field>\n\t\t<field flags=\"Combo\" name=\"Favourite Colour List Box\" type=\"Ch\">\n\t\t\t<default-value>Red</default-value>\n\t\t\t<font type=\"TrueType\" size=\"11\" name=\"ArialMT\" />\n\t\t\t<options>\n\t\t\t\t<option value=\"Black\" />\n\t\t\t\t<option value=\"Brown\" />\n\t\t\t\t<option value=\"Red\" />\n\t\t\t\t<option value=\"Orange\" />\n\t\t\t\t<option value=\"Yellow\" />\n\t\t\t\t<option value=\"Green\" />\n\t\t\t\t<option value=\"Blue\" />\n\t\t\t\t<option value=\"Violet\" />\n\t\t\t\t<option value=\"Grey\" />\n\t\t\t\t<option value=\"White\" />\n\t\t\t</options>\n\t\t</field>\n\t\t<field flags=\"NoToggleToOff\" name=\"Language 1 Check Box\" type=\"Btn\">\n\t\t\t<default-value>Off</default-value>\n\t\t\t<font name=\"ZapfDingbats\" />\n\t\t</field>\n\t\t<field flags=\"NoToggleToOff\" name=\"Language 2 Check Box\" type=\"Btn\">\n\t\t\t<default-value>Yes</default-value>\n\t\t\t<font name=\"ZapfDingbats\" />\n\t\t</field>\n\t\t<field flags=\"NoToggleToOff\" name=\"Language 3 Check Box\" type=\"Btn\">\n\t\t\t<default-value>Off</default-value>\n\t\t\t<font name=\"ZapfDingbats\" />\n\t\t</field>\n\t\t<field flags=\"NoToggleToOff\" name=\"Language 4 Check Box\" type=\"Btn\">\n\t\t\t<default-value>Off</default-value>\n\t\t\t<font name=\"ZapfDingbats\" />\n\t\t</field>\n\t\t<field flags=\"NoToggleToOff\" name=\"Language 5 Check Box\" type=\"Btn\">\n\t\t\t<default-value>Off</default-value>\n\t\t\t<font name=\"ZapfDingbats\" />\n\t\t</field>\n\t\t<field flags=\"Combo\" name=\"Gender List Box\" type=\"Ch\">\n\t\t\t<default-value>Man</default-value>\n\t\t\t<font type=\"TrueType\" size=\"11\" name=\"ArialMT\" />\n\t\t\t<options>\n\t\t\t\t<option value=\"Man\" />\n\t\t\t\t<option value=\"Woman\" />\n\t\t\t</options>\n\t\t</field>\n\t\t<field max-len=\"40\" name=\"Address 1 Text Box\" type=\"Tx\">\n\t\t\t<font type=\"TrueType\" size=\"11\" name=\"ArialMT\" />\n\t\t</field>\n\t\t<widget page=\"1\" field=\"Given Name Text Box\">\n\t\t\t<rect y2=\"467.9\" x2=\"315.7\" y1=\"453.7\" x1=\"165.7\" />\n\t\t</widget>\n\t\t<widget page=\"1\" field=\"Family Name Text Box\">\n\t\t\t<rect y2=\"435.4\" x2=\"315.7\" y1=\"421.2\" x1=\"165.7\" />\n\t\t</widget>\n\t\t<widget page=\"1\" field=\"Address 1 Text Box\">\n\t\t\t<rect y2=\"402.5\" x2=\"315.7\" y1=\"388.3\" x1=\"165.7\" />\n\t\t</widget>\n\t\t<widget page=\"1\" field=\"House nr Text Box\">\n\t\t\t<rect y2=\"402.6\" x2=\"446.9\" y1=\"388.4\" x1=\"378.4\" />\n\t\t</widget>\n\t\t<widget page=\"1\" field=\"Address 2 Text Box\">\n\t\t\t<rect y2=\"382.6\" x2=\"315.7\" y1=\"368.4\" x1=\"165.7\" />\n\t\t</widget>\n\t\t<widget page=\"1\" field=\"Postcode Text Box\">\n\t\t\t<rect y2=\"362.7\" x2=\"238.5\" y1=\"348.5\" x1=\"165.7\" />\n\t\t</widget>\n\t\t<widget page=\"1\" field=\"City Text Box\">\n\t\t\t<rect y2=\"362.7\" x2=\"447.2\" y1=\"348.5\" x1=\"297.1\" />\n\t\t</widget>\n\t\t<widget page=\"1\" field=\"Country Combo Box\">\n\t\t\t<rect y2=\"330.1\" x2=\"315.7\" y1=\"315.9\" x1=\"165.7\" />\n\t\t</widget>\n\t\t<widget page=\"1\" field=\"Gender List Box\">\n\t\t\t<rect y2=\"297.6\" x2=\"241.2\" y1=\"283.4\" x1=\"165.7\" />\n\t\t</widget>\n\t\t<widget page=\"1\" field=\"Height Formatted Field\">\n\t\t\t<rect y2=\"265\" x2=\"238\" y1=\"250.8\" x1=\"165.7\" />\n\t\t</widget>\n\t\t<widget appearance=\"Off\" page=\"1\" field=\"Driving License Check Box\">\n\t\t\t<rect y2=\"232.3\" x2=\"175.4\" y1=\"221.4\" x1=\"164.1\" />\n\t\t\t<appearances>\n\t\t\t\t<appearance name=\"Off\" />\n\t\t\t\t<appearance name=\"Yes\" />\n\t\t\t</appearances>\n\t\t\t<captions Normal=\"8\" />\n\t\t</widget>\n\t\t<widget appearance=\"Off\" page=\"1\" field=\"Language 1 Check Box\">\n\t\t\t<rect y2=\"188.5\" x2=\"69\" y1=\"177.6\" x1=\"57.7\" />\n\t\t\t<appearances>\n\t\t\t\t<appearance name=\"Off\" />\n\t\t\t\t<appearance name=\"Yes\" />\n\t\t\t</appearances>\n\t\t\t<captions Normal=\"8\" />\n\t\t</widget>\n\t\t<widget appearance=\"Yes\" page=\"1\" field=\"Language 2 Check Box\">\n\t\t\t<rect y2=\"188.5\" x2=\"166.1\" y1=\"177.6\" x1=\"154.8\" />\n\t\t\t<appearances>\n\t\t\t\t<appearance name=\"Off\" />\n\t\t\t\t<appearance name=\"Yes\" />\n\t\t\t</appearances>\n\t\t\t<captions Normal=\"8\" />\n\t\t</widget>\n\t\t<widget appearance=\"Off\" page=\"1\" field=\"Language 3 Check Box\">\n\t\t\t<rect y2=\"188.5\" x2=\"263.1\" y1=\"177.6\" x1=\"251.8\" />\n\t\t\t<appearances>\n\t\t\t\t<appearance name=\"Off\" />\n\t\t\t\t<appearance name=\"Yes\" />\n\t\t\t</appearances>\n\t\t\t<captions Normal=\"8\" />\n\t\t</widget>\n\t\t<widget appearance=\"Off\" page=\"1\" field=\"Language 4 Check Box\">\n\t\t\t<rect y2=\"188.5\" x2=\"354.1\" y1=\"177.6\" x1=\"342.8\" />\n\t\t\t<appearances>\n\t\t\t\t<appearance name=\"Off\" />\n\t\t\t\t<appearance name=\"Yes\" />\n\t\t\t</appearances>\n\t\t\t<captions Normal=\"8\" />\n\t\t</widget>\n\t\t<widget appearance=\"Off\" page=\"1\" field=\"Language 5 Check Box\">\n\t\t\t<rect y2=\"188.5\" x2=\"451.1\" y1=\"177.6\" x1=\"439.8\" />\n\t\t\t<appearances>\n\t\t\t\t<appearance name=\"Off\" />\n\t\t\t\t<appearance name=\"Yes\" />\n\t\t\t</appearances>\n\t\t\t<captions Normal=\"8\" />\n\t\t</widget>\n\t\t<widget page=\"1\" field=\"Favourite Colour List Box\">\n\t\t\t<rect y2=\"157.6\" x2=\"322.8\" y1=\"143.4\" x1=\"165.7\" />\n\t\t</widget>\n\t</pdf-info>\n\t<fields>\n\t\t<field name=\"Given Name Text Box\">\n\t\t\t<value></value>\n\t\t</field>\n\t\t<field name=\"Family Name Text Box\">\n\t\t\t<value></value>\n\t\t</field>\n\t\t<field name=\"House nr Text Box\">\n\t\t\t<value></value>\n\t\t</field>\n\t\t<field name=\"Address 2 Text Box\">\n\t\t\t<value></value>\n\t\t</field>\n\t\t<field name=\"Postcode Text Box\">\n\t\t\t<value></value>\n\t\t</field>\n\t\t<field name=\"Country Combo Box\">\n\t\t\t<value></value>\n\t\t</field>\n\t\t<field name=\"Height Formatted Field\">\n\t\t\t<value>150</value>\n\t\t</field>\n\t\t<field name=\"City Text Box\">\n\t\t\t<value></value>\n\t\t</field>\n\t\t<field name=\"Driving License Check Box\">\n\t\t\t<value>Off</value>\n\t\t</field>\n\t\t<field name=\"Favourite Colour List Box\">\n\t\t\t<value>Red</value>\n\t\t</field>\n\t\t<field name=\"Language 1 Check Box\">\n\t\t\t<value>Off</value>\n\t\t</field>\n\t\t<field name=\"Language 2 Check Box\">\n\t\t\t<value>Yes</value>\n\t\t</field>\n\t\t<field name=\"Language 3 Check Box\">\n\t\t\t<value>Off</value>\n\t\t</field>\n\t\t<field name=\"Language 4 Check Box\">\n\t\t\t<value>Off</value>\n\t\t</field>\n\t\t<field name=\"Language 5 Check Box\">\n\t\t\t<value>Off</value>\n\t\t</field>\n\t\t<field name=\"Gender List Box\">\n\t\t\t<value>Man</value>\n\t\t</field>\n\t\t<field name=\"Address 1 Text Box\">\n\t\t\t<value></value>\n\t\t</field>\n\t</fields>\n\t<annots />\n\t<pages>\n\t\t<defmtx matrix=\"1.333333,0.000000,0.000000,-1.333333,0.000000,1122.666667\" />\n\t</pages>\n</xfdf>"
我尝试了以下代码,
let recordKey = "xfdf"
var currentValue: String?
private var services: [PDFInfo] = []
private var foundedService: PDFInfo?
private var activeElement: String?
if let formData = serverResponse {
let parser = XMLParser(data: Data(formData.utf8))
parser.delegate = self
if parser.parse() {
print(self.services)
}
}
extension ViewController: XMLParserDelegate {
// initialize results structure
func parserDidStartDocument(_ parser: XMLParser) {
currentValue = ""
}
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
if elementName == recordKey {
foundedService = PDFInfo(info: "")
}
activeElement = elementName
}
// found characters
//
// - If this is an element we care about, append those characters.
// - If `currentValue` still `nil`, then do nothing.
func parser(_ parser: XMLParser, foundCharacters string: String) {
currentValue? += string
switch activeElement {
case "pdf-info": foundedService?.info = string
default: break
}
}
// end element
//
// - If we're at the end of the whole dictionary, then save that dictionary in our array
// - If we're at the end of an element that belongs in the dictionary, then save that value in the dictionary
func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
if elementName == recordKey {
services.append(foundedService!)
foundedService = nil
}
}
// Just in case, if there's an error, report it. (We don't want to fly blind here.)
func parser(_ parser: XMLParser, parseErrorOccurred parseError: Error) {
print(parseError)
currentValue = nil
}
}
//Model.swift
struct PDFInfo {
var info: String
}
但是输出:
"\n\t\t\t"
预期输出:
"<pdf-info version=\"1\" xmlns=\"http://www.pdftron.com/pdfinfo\">\n\t\t<field max-len=\"40\" name=\"Given Name Text Box\" type=\"Tx\">\n\t\t\t<font type=\"TrueType\" size=\"11\" name=\"ArialMT\" />\n\t\t</field>\n\t\t<field max-len=\"40\" name=\"Family Name Text Box\" type=\"Tx\">\n\t\t\t<font type=\"TrueType\" size=\"11\" name=\"ArialMT\" />\n\t\t</field>\n\t\t<field max-len=\"20\" name=\"House nr Text Box\" type=\"Tx\">\n\t\t\t<font type=\"TrueType\" size=\"11\" name=\"ArialMT\" />\n\t\t</field>\n\t\t<field max-len=\"40\" name=\"Address 2 Text Box\" type=\"Tx\">\n\t\t\t<font type=\"TrueType\" size=\"11\" name=\"ArialMT\" />\n\t\t</field>\n\t\t<field max-len=\"20\" name=\"Postcode Text Box\" type=\"Tx\">\n\t\t\t<font type=\"TrueType\" size=\"11\" name=\"ArialMT\" />\n\t\t</field>\n\t\t<field flags=\"Combo Edit\" name=\"Country Combo Box\" type=\"Ch\">\n\t\t\t<font type=\"TrueType\" size=\"11\" name=\"ArialMT\" />\n\t\t\t<options>\n\t\t\t\t<option value=\"Austria\" />\n\t\t\t\t<option value=\"Belgium\" />\n\t\t\t\t<option value=\"Britain\" />\n\t\t\t\t<option value=\"Bulgaria\" />\n\t\t\t\t<option value=\"Croatia\" />\n\t\t\t\t<option value=\"Cyprus\" />\n\t\t\t\t<option value=\"Czech-Republic\" />\n\t\t\t\t<option value=\"Denmark\" />\n\t\t\t\t<option value=\"Estonia\" />\n\t\t\t\t<option value=\"Finland\" />\n\t\t\t\t<option value=\"France\" />\n\t\t\t\t<option value=\"Germany\" />\n\t\t\t\t<option value=\"Greece\" />\n\t\t\t\t<option value=\"Hungary\" />\n\t\t\t\t<option value=\"Ireland\" />\n\t\t\t\t<option value=\"Italy\" />\n\t\t\t\t<option value=\"Latvia\" />\n\t\t\t\t<option value=\"Lithuania\" />\n\t\t\t\t<option value=\"Luxembourg\" />\n\t\t\t\t<option value=\"Malta\" />\n\t\t\t\t<option value=\"Netherlands\" />\n\t\t\t\t<option value=\"Poland\" />\n\t\t\t\t<option value=\"Portugal\" />\n\t\t\t\t<option value=\"Romania\" />\n\t\t\t\t<option value=\"Slovakia\" />\n\t\t\t\t<option value=\"Slovenia\" />\n\t\t\t\t<option value=\"Spain\" />\n\t\t\t\t<option value=\"Sweden\" />\n\t\t\t</options>\n\t\t</field>\n\t\t<field max-len=\"20\" name=\"Height Formatted Field\" type=\"Tx\">\n\t\t\t<default-value>150</default-value>\n\t\t\t<font type=\"TrueType\" size=\"11\" name=\"ArialMT\" />\n\t\t</field>\n\t\t<field max-len=\"40\" name=\"City Text Box\" type=\"Tx\">\n\t\t\t<font type=\"TrueType\" size=\"11\" name=\"ArialMT\" />\n\t\t</field>\n\t\t<field flags=\"NoToggleToOff\" name=\"Driving License Check Box\" type=\"Btn\">\n\t\t\t<default-value>Off</default-value>\n\t\t\t<font name=\"ZapfDingbats\" />\n\t\t</field>\n\t\t<field flags=\"Combo\" name=\"Favourite Colour List Box\" type=\"Ch\">\n\t\t\t<default-value>Red</default-value>\n\t\t\t<font type=\"TrueType\" size=\"11\" name=\"ArialMT\" />\n\t\t\t<options>\n\t\t\t\t<option value=\"Black\" />\n\t\t\t\t<option value=\"Brown\" />\n\t\t\t\t<option value=\"Red\" />\n\t\t\t\t<option value=\"Orange\" />\n\t\t\t\t<option value=\"Yellow\" />\n\t\t\t\t<option value=\"Green\" />\n\t\t\t\t<option value=\"Blue\" />\n\t\t\t\t<option value=\"Violet\" />\n\t\t\t\t<option value=\"Grey\" />\n\t\t\t\t<option value=\"White\" />\n\t\t\t</options>\n\t\t</field>\n\t\t<field flags=\"NoToggleToOff\" name=\"Language 1 Check Box\" type=\"Btn\">\n\t\t\t<default-value>Off</default-value>\n\t\t\t<font name=\"ZapfDingbats\" />\n\t\t</field>\n\t\t<field flags=\"NoToggleToOff\" name=\"Language 2 Check Box\" type=\"Btn\">\n\t\t\t<default-value>Yes</default-value>\n\t\t\t<font name=\"ZapfDingbats\" />\n\t\t</field>\n\t\t<field flags=\"NoToggleToOff\" name=\"Language 3 Check Box\" type=\"Btn\">\n\t\t\t<default-value>Off</default-value>\n\t\t\t<font name=\"ZapfDingbats\" />\n\t\t</field>\n\t\t<field flags=\"NoToggleToOff\" name=\"Language 4 Check Box\" type=\"Btn\">\n\t\t\t<default-value>Off</default-value>\n\t\t\t<font name=\"ZapfDingbats\" />\n\t\t</field>\n\t\t<field flags=\"NoToggleToOff\" name=\"Language 5 Check Box\" type=\"Btn\">\n\t\t\t<default-value>Off</default-value>\n\t\t\t<font name=\"ZapfDingbats\" />\n\t\t</field>\n\t\t<field flags=\"Combo\" name=\"Gender List Box\" type=\"Ch\">\n\t\t\t<default-value>Man</default-value>\n\t\t\t<font type=\"TrueType\" size=\"11\" name=\"ArialMT\" />\n\t\t\t<options>\n\t\t\t\t<option value=\"Man\" />\n\t\t\t\t<option value=\"Woman\" />\n\t\t\t</options>\n\t\t</field>\n\t\t<field max-len=\"40\" name=\"Address 1 Text Box\" type=\"Tx\">\n\t\t\t<font type=\"TrueType\" size=\"11\" name=\"ArialMT\" />\n\t\t</field>\n\t\t<widget page=\"1\" field=\"Given Name Text Box\">\n\t\t\t<rect y2=\"467.9\" x2=\"315.7\" y1=\"453.7\" x1=\"165.7\" />\n\t\t</widget>\n\t\t<widget page=\"1\" field=\"Family Name Text Box\">\n\t\t\t<rect y2=\"435.4\" x2=\"315.7\" y1=\"421.2\" x1=\"165.7\" />\n\t\t</widget>\n\t\t<widget page=\"1\" field=\"Address 1 Text Box\">\n\t\t\t<rect y2=\"402.5\" x2=\"315.7\" y1=\"388.3\" x1=\"165.7\" />\n\t\t</widget>\n\t\t<widget page=\"1\" field=\"House nr Text Box\">\n\t\t\t<rect y2=\"402.6\" x2=\"446.9\" y1=\"388.4\" x1=\"378.4\" />\n\t\t</widget>\n\t\t<widget page=\"1\" field=\"Address 2 Text Box\">\n\t\t\t<rect y2=\"382.6\" x2=\"315.7\" y1=\"368.4\" x1=\"165.7\" />\n\t\t</widget>\n\t\t<widget page=\"1\" field=\"Postcode Text Box\">\n\t\t\t<rect y2=\"362.7\" x2=\"238.5\" y1=\"348.5\" x1=\"165.7\" />\n\t\t</widget>\n\t\t<widget page=\"1\" field=\"City Text Box\">\n\t\t\t<rect y2=\"362.7\" x2=\"447.2\" y1=\"348.5\" x1=\"297.1\" />\n\t\t</widget>\n\t\t<widget page=\"1\" field=\"Country Combo Box\">\n\t\t\t<rect y2=\"330.1\" x2=\"315.7\" y1=\"315.9\" x1=\"165.7\" />\n\t\t</widget>\n\t\t<widget page=\"1\" field=\"Gender List Box\">\n\t\t\t<rect y2=\"297.6\" x2=\"241.2\" y1=\"283.4\" x1=\"165.7\" />\n\t\t</widget>\n\t\t<widget page=\"1\" field=\"Height Formatted Field\">\n\t\t\t<rect y2=\"265\" x2=\"238\" y1=\"250.8\" x1=\"165.7\" />\n\t\t</widget>\n\t\t<widget appearance=\"Off\" page=\"1\" field=\"Driving License Check Box\">\n\t\t\t<rect y2=\"232.3\" x2=\"175.4\" y1=\"221.4\" x1=\"164.1\" />\n\t\t\t<appearances>\n\t\t\t\t<appearance name=\"Off\" />\n\t\t\t\t<appearance name=\"Yes\" />\n\t\t\t</appearances>\n\t\t\t<captions Normal=\"8\" />\n\t\t</widget>\n\t\t<widget appearance=\"Off\" page=\"1\" field=\"Language 1 Check Box\">\n\t\t\t<rect y2=\"188.5\" x2=\"69\" y1=\"177.6\" x1=\"57.7\" />\n\t\t\t<appearances>\n\t\t\t\t<appearance name=\"Off\" />\n\t\t\t\t<appearance name=\"Yes\" />\n\t\t\t</appearances>\n\t\t\t<captions Normal=\"8\" />\n\t\t</widget>\n\t\t<widget appearance=\"Yes\" page=\"1\" field=\"Language 2 Check Box\">\n\t\t\t<rect y2=\"188.5\" x2=\"166.1\" y1=\"177.6\" x1=\"154.8\" />\n\t\t\t<appearances>\n\t\t\t\t<appearance name=\"Off\" />\n\t\t\t\t<appearance name=\"Yes\" />\n\t\t\t</appearances>\n\t\t\t<captions Normal=\"8\" />\n\t\t</widget>\n\t\t<widget appearance=\"Off\" page=\"1\" field=\"Language 3 Check Box\">\n\t\t\t<rect y2=\"188.5\" x2=\"263.1\" y1=\"177.6\" x1=\"251.8\" />\n\t\t\t<appearances>\n\t\t\t\t<appearance name=\"Off\" />\n\t\t\t\t<appearance name=\"Yes\" />\n\t\t\t</appearances>\n\t\t\t<captions Normal=\"8\" />\n\t\t</widget>\n\t\t<widget appearance=\"Off\" page=\"1\" field=\"Language 4 Check Box\">\n\t\t\t<rect y2=\"188.5\" x2=\"354.1\" y1=\"177.6\" x1=\"342.8\" />\n\t\t\t<appearances>\n\t\t\t\t<appearance name=\"Off\" />\n\t\t\t\t<appearance name=\"Yes\" />\n\t\t\t</appearances>\n\t\t\t<captions Normal=\"8\" />\n\t\t</widget>\n\t\t<widget appearance=\"Off\" page=\"1\" field=\"Language 5 Check Box\">\n\t\t\t<rect y2=\"188.5\" x2=\"451.1\" y1=\"177.6\" x1=\"439.8\" />\n\t\t\t<appearances>\n\t\t\t\t<appearance name=\"Off\" />\n\t\t\t\t<appearance name=\"Yes\" />\n\t\t\t</appearances>\n\t\t\t<captions Normal=\"8\" />\n\t\t</widget>\n\t\t<widget page=\"1\" field=\"Favourite Colour List Box\">\n\t\t\t<rect y2=\"157.6\" x2=\"322.8\" y1=\"143.4\" x1=\"165.7\" />\n\t\t</widget>\n\t</pdf-info>"
答案 0 :(得分:0)
您可以使用正则表达式来代替解析XML,
// The RegExp pattern to find everything within the tags
let pattern = "<pdf-info.*>((.|\n)*?)<\\/pdf-info>"
// Create the expression
let expression = try NSRegularExpression(pattern: pattern, options: .caseInsensitive)
// Get a list of matches
let range = NSRange(location: 0, length: serverResponse.utf8.count)
let matches = expression.matches(in: serverResponse, options: [], range: range)
// We know there's only 1 match, so extract the contents
let results = matches
.first
.map {
(serverResponse as NSString).substring(with: $0.range)
}
print(results) // Prints Optional("<pdf-info ...etc... </pdf-info>")