Nokogiri实施援助

时间:2017-03-06 22:39:47

标签: ruby xml nokogiri

<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<hierarchy rotation="0">
<node class="android.widget.FrameLayout" bounds="[0,0][768,1184]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.android.systemui" resource-id="" text="" index="0">
<node class="android.widget.FrameLayout" bounds="[0,0][768,1184]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.android.systemui" resource-id="com.android.systemui:id/panel_holder" text="" index="0">
<node class="android.widget.FrameLayout" bounds="[0,0][768,1184]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.android.systemui" resource-id="com.android.systemui:id/notification_panel" text="" index="0">
<node class="android.widget.FrameLayout" bounds="[0,0][768,1184]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.android.systemui" resource-id="com.android.systemui:id/notification_container_parent" text="" index="0">
<node class="android.widget.RelativeLayout" bounds="[0,0][768,120]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="true" enabled="true" clickable="true" checked="false" checkable="false" content-desc="" package="com.android.systemui" resource-id="com.android.systemui:id/header" text="" index="1">
<node class="android.widget.LinearLayout" bounds="[48,8][153,62]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.android.systemui" resource-id="com.android.systemui:id/clock" text="" index="0">
<node class="android.widget.TextView" bounds="[48,21][103,59]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.android.systemui" resource-id="com.android.systemui:id/time_view" text="5:28" index="0"/>
<node class="android.widget.TextView" bounds="[103,21][153,59]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.android.systemui" resource-id="com.android.systemui:id/am_pm_view" text=" PM" index="1"/>
</node>
<node class="android.widget.FrameLayout" bounds="[16,62][260,100]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.android.systemui" resource-id="com.android.systemui:id/date_group" text="" index="1">
<node class="android.widget.TextView" bounds="[48,62][260,100]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.android.systemui" resource-id="com.android.systemui:id/date_expanded" text="Monday, March 6" index="0"/>
</node>
<node class="android.widget.LinearLayout" bounds="[542,0][658,120]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.android.systemui" resource-id="com.android.systemui:id/system_icons_super_container" text="" index="3">
<node class="android.widget.FrameLayout" bounds="[542,35][650,85]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.android.systemui" resource-id="com.android.systemui:id/system_icons_container" text="" index="0">
<node class="android.widget.LinearLayout" bounds="[542,35][650,85]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.android.systemui" resource-id="com.android.systemui:id/system_icons" text="" index="0">
<node class="android.widget.LinearLayout" bounds="[547,43][631,77]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.android.systemui" resource-id="com.android.systemui:id/signal_cluster" text="" index="1">
<node class="android.widget.FrameLayout" bounds="[547,43][583,77]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="Wifi signal full." package="com.android.systemui" resource-id="com.android.systemui:id/wifi_combo" text="" index="0">
<node class="android.widget.ImageView" bounds="[547,43][583,77]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.android.systemui" resource-id="com.android.systemui:id/wifi_signal" text="" index="0"/>
</node>
<node class="android.widget.LinearLayout" bounds="[583,43][617,77]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.android.systemui" resource-id="com.android.systemui:id/mobile_signal_group" text="" index="1">
<node class="android.widget.FrameLayout" bounds="[583,43][617,77]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="3G Phone two bars." package="com.android.systemui" resource-id="com.android.systemui:id/mobile_combo" text="" index="0">
<node class="android.widget.ImageView" bounds="[583,43][617,77]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.android.systemui" resource-id="com.android.systemui:id/mobile_signal" text="" index="0"/>
</node>
</node>
</node>
<node class="android.view.View" bounds="[631,45][650,74]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="Battery 100 percent." package="com.android.systemui" resource-id="com.android.systemui:id/battery" text="" index="2"/>
</node>
</node>
</node>
<node class="android.widget.FrameLayout" bounds="[658,0][726,120]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.android.systemui" resource-id="com.android.systemui:id/multi_user_switch" text="" index="4">
<node class="android.widget.ImageView" bounds="[670,38][714,82]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.android.systemui" resource-id="com.android.systemui:id/multi_user_avatar" text="" index="0"/>
</node>
</node>
</node>
</node>
<node class="android.view.View" bounds="[0,0][768,1184]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.android.systemui" resource-id="com.android.systemui:id/scrim_behind" text="" index="1"/>
<node class="android.view.View" bounds="[0,0][768,1184]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.android.systemui" resource-id="com.android.systemui:id/scrim_in_front" text="" index="2"/>
<node class="android.widget.FrameLayout" bounds="[0,0][768,50]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.android.systemui" resource-id="com.android.systemui:id/status_bar" text="" index="3">
<node class="android.widget.LinearLayout" bounds="[0,0][768,50]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.android.systemui" resource-id="com.android.systemui:id/status_bar_contents" text="" index="0">
<node class="android.widget.FrameLayout" bounds="[12,0][575,50]" selected="false" password="false" long-clickable="false" scrollable="false" focused="false" focusable="false" enabled="true" clickable="false" checked="false" checkable="false" content-desc="" package="com.android.systemui" resource-id="com.android.systemui:id/notification_icon_area" text="" index="0"/>
</node>
</node>
</node>
</hierarchy>

我有一个XML文件,我正在尝试提取&#34;文本&#34;属性,&#34; ana.zx&#34;例如,要么将其放在不同的文件中,要么将其转换为字符串。

我设法将文件保存到存储空间并打开它,但我还没弄明白如何使用Nokogiri来提取该属性。

这是我到目前为止所做的:

doc = File.open("window_dump.xml") { |f| Nokogiri::XML(f)}

1 个答案:

答案 0 :(得分:0)

您的XML无效。 XML规范非常严格,因此文档必须是有效的,不像HTML是狗狗友好的。

以下是Nokogiri发现的错误:

 # => [#<Nokogiri::XML::SyntaxError: XML declaration allowed only at the start of the document>,
 #     #<Nokogiri::XML::SyntaxError: Opening and ending tag mismatch: node line 4 and hierarchy>,
 #     #<Nokogiri::XML::SyntaxError: Premature end of data in tag hierarchy line 3>]

解析文档后,对返回的值使用errors方法,以查看Nokogiri要解析文档的操作。如果有错误,Nokogiri可能只是为了返回一些东西而改变文档,而那些改变的节点可能会隐藏信息。在您的文档中,它在文本的早期版本中,因此您可能无法访问所有后续子节点。

如果errors显示存在问题,则在需要更多信息时使用XML验证程序。至少我会将文件拉入Vim并让它重新格式化文档,以便我可以检查其缩进/嵌套,因为通常问题是由于缺少结束标记。

此外,我们通常使用Nokogiri来打开和解析文件:

doc = Nokogiri::XML(File.open('path/to/file.xml'))

Nokogiri会为您读取并关闭该文件,因此不需要使用块。