platform :ios, '13.0'
use_frameworks!
target 'DemoApp' do
source 'https://github.com/CocoaPods/Specs.git'
pod 'Fuzi'
end
<?xml version="1.0"?>
<nutrition>
<daily-values>
<total-fat units="g">65</total-fat>
<saturated-fat units="g">20</saturated-fat>
<cholesterol units="mg">300</cholesterol>
<sodium units="mg">2400</sodium>
<carb units="g">300</carb>
<fiber units="g">25</fiber>
<protein units="g">50</protein>
</daily-values>
<food>
<name>Avocado Dip</name>
<mfr>Sunnydale</mfr>
<serving units="g">29</serving>
<calories total="110" fat="100"/>
<total-fat>11</total-fat>
<saturated-fat>3</saturated-fat>
<cholesterol>5</cholesterol>
<sodium>210</sodium>
<carb>2</carb>
<fiber>0</fiber>
<protein>1</protein>
<vitamins>
<a>0</a>
<c>0</c>
</vitamins>
<minerals>
<ca>0</ca>
<fe>0</fe>
</minerals>
</food>
<food>
<name>Bagels, New York Style </name>
<mfr>Thompson</mfr>
<serving units="g">104</serving>
<calories total="300" fat="35"/>
<total-fat>4</total-fat>
<saturated-fat>1</saturated-fat>
<cholesterol>0</cholesterol>
<sodium>510</sodium>
<carb>54</carb>
<fiber>3</fiber>
<protein>11</protein>
<vitamins>
<a>0</a>
<c>0</c>
</vitamins>
<minerals>
<ca>8</ca>
<fe>20</fe>
</minerals>
</food>
<food>
<name>Beef Frankfurter, Quarter Pound </name>
<mfr>Armitage</mfr>
<serving units="g">115</serving>
<calories total="370" fat="290"/>
<total-fat>32</total-fat>
<saturated-fat>15</saturated-fat>
<cholesterol>65</cholesterol>
<sodium>1100</sodium>
<carb>8</carb>
<fiber>0</fiber>
<protein>13</protein>
<vitamins>
<a>0</a>
<c>2</c>
</vitamins>
<minerals>
<ca>1</ca>
<fe>6</fe>
</minerals>
</food>
<food>
<name>Chicken Pot Pie</name>
<mfr>Lakeson</mfr>
<serving units="g">198</serving>
<calories total="410" fat="200"/>
<total-fat>22</total-fat>
<saturated-fat>9</saturated-fat>
<cholesterol>25</cholesterol>
<sodium>810</sodium>
<carb>42</carb>
<fiber>2</fiber>
<protein>10</protein>
<vitamins>
<a>20</a>
<c>2</c>
</vitamins>
<minerals>
<ca>2</ca>
<fe>10</fe>
</minerals>
</food>
<food>
<name>Cole Slaw</name>
<mfr>Fresh Quick</mfr>
<serving units=" cup">1.5</serving>
<calories total="20" fat="0"/>
<total-fat>0</total-fat>
<saturated-fat>0</saturated-fat>
<cholesterol>0</cholesterol>
<sodium>15</sodium>
<carb>5</carb>
<fiber>2</fiber>
<protein>1</protein>
<vitamins>
<a>30</a>
<c>45</c>
</vitamins>
<minerals>
<ca>4</ca>
<fe>2</fe>
</minerals>
</food>
<food>
<name>Eggs</name>
<mfr>Goodpath</mfr>
<serving units="g">50</serving>
<calories total="70" fat="40"/>
<total-fat>4.5</total-fat>
<saturated-fat>1.5</saturated-fat>
<cholesterol>215</cholesterol>
<sodium>65</sodium>
<carb>1</carb>
<fiber>0</fiber>
<protein>6</protein>
<vitamins>
<a>6</a>
<c>0</c>
</vitamins>
<minerals>
<ca>2</ca>
<fe>4</fe>
</minerals>
</food>
<food>
<name>Hazelnut Spread</name>
<mfr>Ferreira</mfr>
<serving units="tbsp">2</serving>
<calories total="200" fat="90"/>
<total-fat>10</total-fat>
<saturated-fat>2</saturated-fat>
<cholesterol>0</cholesterol>
<sodium>20</sodium>
<carb>23</carb>
<fiber>2</fiber>
<protein>3</protein>
<vitamins>
<a>0</a>
<c>0</c>
</vitamins>
<minerals>
<ca>6</ca>
<fe>4</fe>
</minerals>
</food>
<food>
<name>Potato Chips</name>
<mfr>Lees</mfr>
<serving units="g">28</serving>
<calories total="150" fat="90"/>
<total-fat>10</total-fat>
<saturated-fat>3</saturated-fat>
<cholesterol>0</cholesterol>
<sodium>180</sodium>
<carb>15</carb>
<fiber>1</fiber>
<protein>2</protein>
<vitamins>
<a>0</a>
<c>10</c>
</vitamins>
<minerals>
<ca>0</ca>
<fe>0</fe>
</minerals>
</food>
<food>
<name>Soy Patties, Grilled</name>
<mfr>Gardenproducts</mfr>
<serving units="g">96</serving>
<calories total="160" fat="45"/>
<total-fat>5</total-fat>
<saturated-fat>0</saturated-fat>
<cholesterol>0</cholesterol>
<sodium>420</sodium>
<carb>10</carb>
<fiber>4</fiber>
<protein>9</protein>
<vitamins>
<a>0</a>
<c>0</c>
</vitamins>
<minerals>
<ca>0</ca>
<fe>0</fe>
</minerals>
</food>
<food>
<name>Truffles, Dark Chocolate</name>
<mfr>Lyndon's</mfr>
<serving units="g">39</serving>
<calories total="220" fat="170"/>
<total-fat>19</total-fat>
<saturated-fat>14</saturated-fat>
<cholesterol>25</cholesterol>
<sodium>10</sodium>
<carb>16</carb>
<fiber>1</fiber>
<protein>1</protein>
<vitamins>
<a>0</a>
<c>0</c>
</vitamins>
<minerals>
<ca>0</ca>
<fe>0</fe>
</minerals>
</food>
</nutrition>
import UIKit
import Fuzi
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
yeTye()
}
func yeTye(){
let fileUrl = URL(fileURLWithPath: ((#file as NSString).deletingLastPathComponent as NSString).appendingPathComponent("nutrition.xml"))
do
{
// 加载文件:
let data = try Data(contentsOf: fileUrl)
let document = try XMLDocument(data: data)
if let root = document.root
{
print("---Root Element: \(String(describing: root.tag))--") //Optional("nutrition")
print("---Daily values:--\n")
for element in root.firstChild(tag: "daily-values")?.children ?? []
{ // 遍历daily-values节点:
let nutrient = element.tag
let amount = element.numberValue
let unit = element["units"]
print("- \(amount!)\(unit!) \(nutrient!)")
// - 65g total-fat
// - 20g saturated-fat
// - 300mg cholesterol
// - 2400mg sodium
// - 300g carb
// - 25g fiber
// - 50g protein
}
var xpath = "//food/name"
print("---按路径搜索(根节点下的所有/food/name节点),Path Search: \(xpath)--") //XPath Search: //food/name
for element in document.xpath(xpath)
{
print("---\(element)--")
// <name>Avocado Dip</name>
// <name>Bagels, New York Style </name>
// <name>Beef Frankfurter, Quarter Pound </name>
// <name>Chicken Pot Pie</name>
// <name>Cole Slaw</name>
// <name>Eggs</name>
// <name>Hazelnut Spread</name>
// <name>Potato Chips</name>
// <name>Soy Patties, Grilled</name>
// <name>Truffles, Dark Chocolate</name>
}
let css = "food > serving[units]"
var blockElement:XMLElement? = nil
print("---CSS Search: \(css)--") // CSS Search: food > serving[units]
for (index, element) in document.css(css).enumerated()
{ // 索引为1的节点:
if index == 1
{
blockElement = element
break
}
}
print("Second element: \(blockElement!)\n") //Second element: <serving units="g">104</serving>
xpath = "//food/name"
print("XPath Search: \(xpath)") //XPath Search: //food/name
let firstElement = document.firstChild(xpath: xpath)!
print("First element: \(firstElement)") // First element: <name>Avocado Dip</name>;
}
}
catch
{
print("Something went wrong :(")
}
}
}