Skip to content

Commit

Permalink
Add children method for enumerating elements.
Browse files Browse the repository at this point in the history
This method can be distinguished from the all method because the all
method enumerates all elements *at the currently indexed level*. The
children method enumerates all *child* elements at the currently indexed
level.

Resolves issue #7.
  • Loading branch information
drmohundro committed Jan 30, 2015
1 parent 4ef62b2 commit b35cd0d
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v0.6.0 (January 30, 2015)

* Added `children` property to allow for enumerating all child elements.

## v0.5.5 (January 25, 2015)

* Added OSX target, should allow SWXMLHash to work in OSX as well as iOS.
Expand Down
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Alternatively, you can look up an element with specific attributes. The below wi
xml["root"]["catalog"]["book"].withAttr("id", "123")["author"].element?.text
```

### Returning All Elements
### Returning All Elements At Current Level

Given:

Expand Down Expand Up @@ -125,6 +125,35 @@ for elem in xml["root"]["catalog"]["book"] {
}
```

### Returning All Child Elements At Current Level

Given:

```xml
<root>
<catalog>
<book>
<genre>Fiction</genre>
<title>Book</title>
<date>1/1/2015</date>
</book>
</catalog>
</root>
```

The below will `NSLog` "root", "catalog", "book", "genre", "title", and "date" (note the `children` method).

```swift
func enumerate(indexer: XMLIndexer) {
for child in indexer.children {
NSLog(child.element!.name)
enumerate(child)
}
}

enumerate(xml)
```

### Error Handling

```swift
Expand Down
4 changes: 2 additions & 2 deletions SWXMLHash.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'SWXMLHash'
s.version = '0.5.5'
s.version = '0.6.0'
s.summary = 'Simple XML parsing in Swift'
s.homepage = 'https://github.com/drmohundro/SWXMLHash'
s.license = { :type => 'MIT' }
Expand All @@ -9,6 +9,6 @@ Pod::Spec.new do |s|
s.requires_arc = true
s.osx.deployment_target = '10.9'
s.ios.deployment_target = '8.0'
s.source = { :git => 'https://github.com/drmohundro/SWXMLHash.git', :tag => '0.5.5' }
s.source = { :git => 'https://github.com/drmohundro/SWXMLHash.git', :tag => '0.6.0' }
s.source_files = 'Source/*.swift'
end
21 changes: 21 additions & 0 deletions SWXMLHashPlayground.playground/section-1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,24 @@ let count = xml["root"].all.count

// "Apples"
xml["root"]["h:table"]["h:tr"]["h:td"][0].element!.text!


// enumerate all child elements (procedurally)
func enumerate(indexer: XMLIndexer, level: Int) {
for child in indexer.children {
var name = child.element!.name
NSLog("\(level) \(name)")

enumerate(child, level + 1)
}
}

enumerate(xml, 0)


// enumerate all child elements (functionally)
func reduceName(names: String, elem: XMLIndexer) -> String {
return names + elem.element!.name + elem.children.reduce(", ", combine: reduceName)
}

xml.children.reduce("elements: ", combine: reduceName)
16 changes: 15 additions & 1 deletion Source/SWXMLHash.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public enum XMLIndexer : SequenceType {
}
}

/// The underlying array of XMLElements at the currently indexed level of XML.
/// All elements at the currently indexed level
public var all: [XMLIndexer] {
get {
switch self {
Expand All @@ -140,6 +140,20 @@ public enum XMLIndexer : SequenceType {
}
}
}

/// All child elements from the currently indexed level
public var children: [XMLIndexer] {
get {
var list = [XMLIndexer]()
for elem in all.map({ $0.element! }) {
for keyVal in elem.children.values {
for elem in keyVal {
list.append(XMLIndexer(elem))
}
}
}
return list
}
}

/**
Expand Down
8 changes: 6 additions & 2 deletions Tests/SWXMLHashSpecs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class SWXMLHashTests: QuickSpec {
}

it("should be able to iterate element groups") {
expect(", ".join(xml["root"]["catalog"]["book"].all.map { elem in elem["genre"].element!.text! })).to(equal("Computer, Fantasy, Fantasy"))
expect(", ".join(xml["root"]["catalog"]["book"].all.map { $0["genre"].element!.text! })).to(equal("Computer, Fantasy, Fantasy"))
}

it("should be able to iterate element groups even if only one element is found") {
Expand Down Expand Up @@ -82,6 +82,10 @@ class SWXMLHashTests: QuickSpec {

expect(parsed["niotemplate"]["other"].element?.text).to(equal("this\n has\n white\n space"))
}

it("should be able to enumerate children") {
expect(", ".join(xml["root"]["catalog"]["book"][0].children.map{ $0.element!.name })).to(equal("author, title, genre, price, publish_date, description"))
}
}

describe("white space parsing") {
Expand Down Expand Up @@ -131,4 +135,4 @@ class SWXMLHashTests: QuickSpec {
}
}
}
}
}

0 comments on commit b35cd0d

Please sign in to comment.