Skip to content

Commit 2211b35

Browse files
authored
Merge pull request #188 from drmohundro/add-innerxml
Add innerXML support
2 parents b42962d + ae3346e commit 2211b35

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

Source/SWXMLHash.swift

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -473,22 +473,22 @@ public class IndexOps {
473473
parser.startParsing(ops)
474474
let indexer = XMLIndexer(parser.root)
475475
var childIndex = indexer
476-
for op in ops {
477-
childIndex = childIndex[op.key]
478-
if op.index >= 0 {
479-
childIndex = childIndex[op.index]
476+
for oper in ops {
477+
childIndex = childIndex[oper.key]
478+
if oper.index >= 0 {
479+
childIndex = childIndex[oper.index]
480480
}
481481
}
482482
ops.removeAll(keepingCapacity: false)
483483
return childIndex
484484
}
485485

486486
func stringify() -> String {
487-
var s = ""
488-
for op in ops {
489-
s += "[" + op.toString() + "]"
487+
var ret = ""
488+
for oper in ops {
489+
ret += "[" + oper.toString() + "]"
490490
}
491-
return s
491+
return ret
492492
}
493493
}
494494

@@ -723,8 +723,8 @@ public enum XMLIndexer {
723723
public func byKey(_ key: String) throws -> XMLIndexer {
724724
switch self {
725725
case .stream(let opStream):
726-
let op = IndexOp(key)
727-
opStream.ops.append(op)
726+
let oper = IndexOp(key)
727+
opStream.ops.append(oper)
728728
return .stream(opStream)
729729
case .element(let elem):
730730
let match = elem.xmlChildren.filter({
@@ -915,6 +915,12 @@ public class XMLElement: XMLContent {
915915
})
916916
}
917917

918+
public var innerXML: String {
919+
return children.reduce("", {
920+
return $0.description + $1.description
921+
})
922+
}
923+
918924
/// All child elements (text or XML)
919925
public var children = [XMLContent]()
920926

Tests/SWXMLHashTests/XMLParsingTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,13 @@ class XMLParsingTests: XCTestCase {
205205
XCTAssertEqual(parsed.description, "<root><foo><what id=\"myId\">puppies</what></foo></root>")
206206
}
207207

208+
func testShouldBeAbleToGetInnerXML() {
209+
let testXML = "<root><foo><what id=\"myId\">puppies</what><elems><elem>1</elem><elem>2</elem></elems></foo></root>"
210+
let parsed = SWXMLHash.parse(testXML)
211+
212+
XCTAssertEqual(parsed["root"]["foo"].element!.innerXML, "<what id=\"myId\">puppies</what><elems><elem>1</elem><elem>2</elem></elems>")
213+
}
214+
208215
// error handling
209216

210217
func testShouldReturnNilWhenKeysDontMatch() {

0 commit comments

Comments
 (0)