我想知道如何从以下XML中解析每个"类别":
到目前为止我的代码
doc = Nokogiri::XML(open(url))
xml.xpath("//category").each do |c|
standings_array.push(c)
end
XML
<?xml version="1.0" encoding="UTF-8"?>
<standings generated="2014-04-25T19:59:29Z">
<categories>
<category id="d1a50060-2ceb-45b6-ae08-b3a1ca38da86" name="Ukraine" country_code="UKR" country="Ukraine">
<tournament_group id="1a5bfc9a-57ed-49a6-9a9f-ac1e9f3fcc09" name="Premier League" season_start="2013-07-11T22:00:00Z" season_end="2014-05-05T21:59:00Z" season="2013">
<tournament id="417d7be0-3b5b-4573-9b72-e8c037093f6a" name="Premier League" season_start="2013-07-11T22:00:00Z" season_end="2014-05-05T21:59:00Z" season="2013">
<team id="4de003dc-8627-4ab3-adff-0b05f700e179" name="FC Shakhtar Donetsk" alias="SHA" country_code="UKR" country="Ukraine" type="team" rank="1" win="18" draw="2" loss="5" goals_for="54" goals_against="21" points="56" change="1" />
<team id="fc242f57-6a6f-4023-aef5-99310e6514dd" name="Dnipro Dnipropetrovsk" alias="DNI" country_code="UKR" country="Ukraine" type="team" rank="2" win="16" draw="6" loss="3" goals_for="49" goals_against="22" points="54" change="-1" />
<team id="fb78bcfc-5df9-4d3f-9097-944b1fa305aa" name="FC Dynamo Kiev" alias="DYK" country_code="UKR" country="Ukraine" type="team" rank="3" win="14" draw="5" loss="6" goals_for="49" goals_against="30" points="47" change="0" />
<team id="45c15051-6de0-4785-978e-3e1c91a11a86" name="Metalist Kharkiv" alias="KHA" country_code="UKR" country="Ukraine" type="team" rank="4" win="12" draw="9" loss="3" goals_for="46" goals_against="27" points="45" change="0" />
<team id="ecd4c7bf-2c7e-40a1-9dde-d4fa195b6485" name="Chernomorets Odessa" alias="CHE" country_code="UKR" country="Ukraine" type="team" rank="5" win="11" draw="7" loss="6" goals_for="26" goals_against="19" points="40" change="0" />
<team id="1435377f-0dbd-4b95-aeec-7bd48d759cbf" name="FC Zorya Lugansk" alias="ZOR" country_code="UKR" country="Ukraine" type="team" rank="6" win="9" draw="9" loss="6" goals_for="30" goals_against="24" points="36" change="2" />
<team id="766a69ed-1c8e-415b-9a3b-e561354fb2e7" name="Vorskla Poltava" alias="POL" country_code="UKR" country="Ukraine" type="team" rank="7" win="9" draw="9" loss="6" goals_for="33" goals_against="32" points="36" change="-1" />
<team id="e0635d1d-16cb-4dd2-849e-86656a71e063" name="FC Metalurg Donetsk" alias="DON" country_code="UKR" country="Ukraine" type="team" rank="8" win="9" draw="7" loss="8" goals_for="39" goals_against="38" points="34" change="-1" />
<team id="bb3e8242-9542-44f3-a09e-f7b5deb0710a" name="FC Illichivec Mariupol" alias="MAR" country_code="UKR" country="Ukraine" type="team" rank="9" win="9" draw="3" loss="12" goals_for="23" goals_against="28" points="30" change="1" />
<team id="6dd06d48-73eb-450b-880f-11f95be2e439" name="PFC Sevastopol" alias="SEV" country_code="UKR" country="Ukraine" type="team" rank="10" win="8" draw="4" loss="11" goals_for="27" goals_against="38" points="28" change="-1" />
<team id="33e18929-fb0a-432e-9797-b15e987b002c" name="Karpaty Lviv" alias="LVI" country_code="UKR" country="Ukraine" type="team" rank="11" win="6" draw="9" loss="9" goals_for="25" goals_against="32" points="27" change="1" />
<team id="1f8550b1-5ada-460c-ba84-5e7b551935e9" name="Volyn Lutsk" alias="VOL" country_code="UKR" country="Ukraine" type="team" rank="12" win="7" draw="6" loss="11" goals_for="23" goals_against="39" points="27" change="-1" />
<team id="20565616-f0c6-4560-a28f-bedeea31e476" name="Goverla-Uzhgorod" alias="UZG" country_code="UKR" country="Ukraine" type="team" rank="13" win="6" draw="4" loss="14" goals_for="19" goals_against="34" points="22" change="0" />
<team id="f6ea5689-e3d7-4f64-895b-cdc8303dc290" name="FC Metalurg Zaporizhya" alias="ZAP" country_code="UKR" country="Ukraine" type="team" rank="14" win="2" draw="5" loss="18" goals_for="17" goals_against="49" points="11" change="0" />
<team id="cdde3c63-807e-4935-b97f-a6cbd9ad3d92" name="Tavriya Simferopol" alias="SIM" country_code="UKR" country="Ukraine" type="team" rank="15" win="2" draw="3" loss="20" goals_for="14" goals_against="41" points="9" change="0" />
<team id="89293230-4c6e-45a7-84b3-1a98a8c03a32" name="Arsenal Kiev" alias="ARS" country_code="UKR" country="Ukraine" type="team" rank="16" win="0" draw="0" loss="0" goals_for="0" goals_against="0" points="0" change="0" />
</tournament>
</tournament_group>
</category>
</categories>
</standings>
xpath中的//
表示根。categories
不存在于文档的根目录中。(它们是//standings/categories/
集合的子级)。
您希望使用//standings/categories/category
来获取每个。
doc = Nokogiri::XML(open(url))
doc.xpath("//standings/categories/category").each do |c|
# what do you want to do with the categories? I'll just print the id:
p c["id"]
end
输出:
d1a50060-2ceb-45b6-ae08-b3a1ca38da86
在那个样本上。
谢谢大家。由于某种原因,它无法通过Nokogiri工作,当我从URL读取XML时,我不得不切换到XMLSimple。我以前使用过野宫,没有任何问题。
xml_data = Net::HTTP.get_response(URI.parse(url)).body
data = XmlSimple.xml_in(xml_data)
data["categories"][0].each do |c|
standings_array.push(c)
end