4 This class is inherited from `AEXMLElement` and has a few addons to represent **XML Document**.
6 XML Parsing is also done with this object.
8 open class AEXMLDocument: AEXMLElement {
12 /// Root (the first child element) element of XML Document **(Empty element with error if not exists)**.
13 open var root: AEXMLElement {
14 guard let rootElement = children.first else {
15 let errorElement = AEXMLElement(name: "Error")
16 errorElement.error = AEXMLError.rootElementMissing
22 open let options: AEXMLOptions
27 Designated initializer - Creates and returns new XML Document object.
29 - parameter root: Root XML element for XML Document (defaults to `nil`).
30 - parameter options: Options for XML Document header and parser settings (defaults to `AEXMLOptions()`).
32 - returns: Initialized XML Document object.
34 public init(root: AEXMLElement? = nil, options: AEXMLOptions = AEXMLOptions()) {
35 self.options = options
37 let documentName = String(describing: AEXMLDocument.self)
38 super.init(name: documentName)
40 // document has no parent element
43 // add root element to document (if any)
44 if let rootElement = root {
45 _ = addChild(rootElement)
50 Convenience initializer - used for parsing XML data (by calling `loadXMLData:` internally).
52 - parameter xmlData: XML data to parse.
53 - parameter options: Options for XML Document header and parser settings (defaults to `AEXMLOptions()`).
55 - returns: Initialized XML Document object containing parsed data. Throws error if data could not be parsed.
57 public convenience init(xml: Data, options: AEXMLOptions = AEXMLOptions()) throws {
58 self.init(options: options)
63 Convenience initializer - used for parsing XML string (by calling `init(xmlData:options:)` internally).
65 - parameter xmlString: XML string to parse.
66 - parameter encoding: String encoding for creating `Data` from `xmlString` (defaults to `String.Encoding.utf8`)
67 - parameter options: Options for XML Document header and parser settings (defaults to `AEXMLOptions()`).
69 - returns: Initialized XML Document object containing parsed data. Throws error if data could not be parsed.
71 public convenience init(xml: String,
72 encoding: String.Encoding = String.Encoding.utf8,
73 options: AEXMLOptions = AEXMLOptions()) throws
75 guard let data = xml.data(using: encoding) else { throw AEXMLError.parsingFailed }
76 try self.init(xml: data, options: options)
82 Creates instance of `AEXMLParser` (private class which is simple wrapper around `XMLParser`)
83 and starts parsing the given XML data. Throws error if data could not be parsed.
85 - parameter data: XML which should be parsed.
87 open func loadXML(_ data: Data) throws {
88 children.removeAll(keepingCapacity: false)
89 let xmlParser = AEXMLParser(document: self, data: data)
95 /// Override of `xml` property of `AEXMLElement` - it just inserts XML Document header at the beginning.
96 open override var xml: String {
97 var xml = "\(options.documentHeader.xmlString)\n"