gpt4 book ai didi

ios - XML 到字典的复杂转换 (Swift/iOS)

转载 作者:可可西里 更新时间:2023-10-31 23:35:51 24 4
gpt4 key购买 nike

我正在绞尽脑汁如何将这个解析后的 xml 转换成数组或字典。 xml 标签没有帮助,因为标签是通用的,并且有 ~10 个标题。我也许可以根据标签的顺序做一些事情。有什么想法吗?

NSXMLParser 方法代码:

class MyXMLParserDelegate: NSObject, NSXMLParserDelegate {

@objc func parserDidStartDocument(parser: NSXMLParser) {
print("parserDidStartDocument")
}

@objc func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
print("didStartElement --> \(elementName)")
}

@objc func parser(parser: NSXMLParser, foundCharacters string: String) {
print("foundCharacters --> \(string)")
}

@objc func parser(parser: NSXMLParser, didEndElement elementName: String,
namespaceURI: String?, qualifiedName qName: String?) {
print("didEndElement --> \(elementName)")
}

@objc func parser(parser: NSXMLParser, didStartMappingPrefix prefix: String,
toURI namespaceURI: String) {
print("didStartMappingPrefix --> Prefix: \(prefix) toURI: \(namespaceURI)")
}

@objc func parser(parser: NSXMLParser, didEndMappingPrefix prefix: String) {
print("didEndMappingPrefix --> Prefix: \(prefix)")
}

@objc func parserDidEndDocument(parser: NSXMLParser) {
//reload table with array
print("parserDidEndDocument")
}
}

使用 NSXMLParser 方法解析 XML 的示例结果:

<result>
<header>
<col>
<label>Tree Name</label>
</col>
<col>
<label>Num Levels</label>
</col>
<col>
<label>Defaults Weight</label>
</col>
<col>
<label>Name</label>
</col>
<col>
<label>Abbrev</label>
</col>
<col>
<label>Level</label>
</col>
<col>
<label>Full Name</label>
</col>
</header>
<body>
<row>
<col>Cost Center 1</col>
<col>2</col>
<col>5</col>
<col>Miami Dolphins Front Office</col>
<col/>
<col>0</col>
<col/>
</row>
<row>
<col>Cost Center 1</col>
<col>2</col>
<col>5</col>
<col>Accounts Receivable</col>
<col>A/R</col>
<col>1</col>
<col>Accounts Receivable</col>
</row>
<row>
<col>Cost Center 1</col>
<col>2</col>
<col>5</col>
<col>06</col>
<col>06</col>
<col>1</col>
<col>06</col>
</row>
<row>
<col>Cost Center 2</col>
<col>3</col>
<col>5</col>
<col>Cost Center 2</col>
<col/>
<col>0</col>
<col/>
</row>
<row>
<col>Cost Center 2</col>
<col>3</col>
<col>5</col>
<col>test2</col>
<col/>
<col>1</col>
<col>test2</col>
</row>
<row>
<col>Cost Center 2</col>
<col>3</col>
<col>5</col>
<col>test</col>
<col/>
<col>1</col>
<col>test</col>
</row>
<row>
<col>Cost Center 3</col>
<col>3</col>
<col>5</col>
<col>Cost Center 3</col>
<col/>
<col>0</col>
<col/>
</row>
<row>
<col>Cost Center 3</col>
<col>3</col>
<col>5</col>
<col>test</col>
<col/>
<col>1</col>
<col>test</col>
</row>
</body>
<footer/>
</result>

parserDidStartDocument

didStartElement --> 结果

找到的字符 -->

didStartElement --> 标题

找到的字符 -->

didStartElement --> col

找到的字符 -->

didStartElement --> 标签

foundCharacters --> 树名

didEndElement --> 标签

找到的字符 -->

didEndElement --> col

找到的字符 -->

didStartElement --> col

找到的字符 -->

didStartElement --> 标签

foundCharacters --> 层数

didEndElement --> 标签

找到的字符 -->

didEndElement --> col

找到的字符 -->

didStartElement --> col

找到的字符 -->

didStartElement --> 标签

foundCharacters --> 默认权重

didEndElement --> 标签

找到的字符 -->

didEndElement --> col

找到的字符 -->

didStartElement --> col

找到的字符 -->

didStartElement --> 标签

foundCharacters --> 姓名

didEndElement --> 标签

找到的字符 -->

didEndElement --> col

找到的字符 -->

didStartElement --> col

找到的字符 -->

didStartElement --> 标签

foundCharacters --> Abbrev

didEndElement --> 标签

找到的字符 -->

didEndElement --> col

找到的字符 -->

didStartElement --> col

找到的字符 -->

didStartElement --> 标签

foundCharacters --> 等级

didEndElement --> 标签

找到的字符 -->

didEndElement --> col

找到的字符 -->

didStartElement --> col

找到的字符 -->

didStartElement --> 标签

foundCharacters --> 全名

didEndElement --> 标签

找到的字符 -->

didEndElement --> col

找到的字符 -->

didEndElement --> 标题

找到的字符 -->

didStartElement --> 正文

找到的字符 -->

didStartElement --> 行

找到的字符 -->

didStartElement --> col

foundCharacters --> 成本中心 1

didEndElement --> col

找到的字符 -->

didStartElement --> col

foundCharacters --> 2

didEndElement --> col

找到的字符 -->

...

最佳答案

我需要这样的东西来测试生成的 XML,但不得不自己动手。我创建了一个嵌套的元素树,每个元素都包含有关 xml 标记的信息。

fileprivate class XmlToDictionaryParserDelegate: NSObject, XMLParserDelegate {

private var currentElement: XmlElement?

fileprivate init(_ element: XmlElement) {
self.currentElement = element
}

public func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
self.currentElement = self.currentElement?.pop(elementName)
}

public func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
self.currentElement = self.currentElement?.push(elementName)
self.currentElement?.attributeDict = attributeDict
}

func parser(_ parser: XMLParser, foundCharacters string: String) {
self.currentElement?.text += string
}
}

public class XmlElement {
public private(set) var name = "unnamed"
public private(set) var children = [String: XmlElement]()
public private(set) var parent: XmlElement? = nil
public fileprivate(set) var text = ""
public fileprivate(set) var attributeDict: [String : String] = [:]

private init(_ parent: XmlElement? = nil, name: String = "") {
self.parent = parent
self.name = name
}

public convenience init?(fromString: String) {
guard let data = fromString.data(using: .utf8) else {
return nil
}
self.init(fromData: data)
}

public init(fromData: Data) {
let parser = XMLParser(data: fromData)
let delegate = XmlToDictionaryParserDelegate(self)
parser.delegate = delegate
parser.parse()
}

fileprivate func push(_ elementName: String) -> XmlElement {
let childElement = XmlElement(self, name: elementName)
children[elementName] = childElement
return childElement
}

fileprivate func pop(_ elementName: String) -> XmlElement? {
assert(elementName == self.name)
return self.parent
}

public subscript(name: String) -> XmlElement? {
return self.children[name]
}
}

使用从字符串(或数据)创建元素

let xml = XmlElement(fromString: "<first>text<second bar="foo"/></first>")

然后像这样使用:

XCTAssert(xml["first"]?.text == "text")

XCTAssert(xml["first"]?["second"].attributeDict["bar"] == "foo")

关于ios - XML 到字典的复杂转换 (Swift/iOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38426107/

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