- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 sax 解析器解析大型 xml 文件。当解析器到达一个空节点时,字符方法不会触发。这是一个例子...
require 'nokogiri'
class Parser < Nokogiri::XML::SAX::Document
def initialize
@count=1
end
def start_element(name, attrs = [])
puts name
end
def characters(string)
string.strip!
puts "#{@count} #{string}"
@count += 1
end
def end_element(name)
puts name
end
end
Nokogiri::XML::SAX::Parser.new(Parser.new).parse(File.open('sax_example3.xml'))
这是示例 xml 文档。
<?xml version="1.0" encoding="UTF-8"?>
<root>
<ISA type="array">
<ISA>
<I02>
<name>Information1</name>
<value>
<raw>00</raw>
<description></description>
</value>
</I02>
<I02>
<name>Information2</name>
<value>
<raw></raw>
<description nil="true"/>
</value>
</I02>
</ISA>
</ISA>
</root>
我不得不使用 sax
,因为该文件有大约 650 万行
。
我想做的是将所有 name
值和 raw
值收集到单独的数组中,稍后我可以压缩这两个数组以获得键值对。
我的处理方式是否正确?还有其他方法吗?
编辑:
我所期望的
array1 = ["Information1","Information2"]
array2 = ["00", ""]
所有 name
值都分配给 array1,raw
值分配给 array2,如上所示。
我得到了什么
array1 = ["Information1","Information2"]
array2 = ["00"]
array2
没有与 array1
相同数量的元素,这意味着无法将名称映射到数组。我认为这是因为如果节点为空,则不会调用 characters
方法。
这是上面程序的输出(编辑了上面的脚本并添加了行号)
root
1
ISA
2
ISA
3
I02
4
name
5 Information1
name
6
value
7
raw
8 00
raw
9
description
description
10
value
11
I02
12
I02
13
name
14 Information2
name
15
value
16
raw
raw
17
description
description
18
value
19
I02
20
ISA
21
ISA
22
root
如您所见,在第 (9 & 10)、(16 & 17) 和 (17 & 18) 行之间,执行了 start_element
和 end_element
方法,但是characters
方法不是。
最佳答案
自 characters
可能不会被调用,您需要注意 <name>
和 <raw>
元素本身。如果我们可以假设 <name>
和 <raw>
总是成对出现并且按照这个顺序,我们可以在每次遇到前者时创建一个新的“空”对(例如 { name: nil, raw: nil }
),然后在(如果) characters
时填写值被称为:
class Parser < Nokogiri::XML::SAX::Document
def initialize(*args)
@vals = []
@current_el = nil
super
end
def start_element(el_name, attrs = [])
if el_name == "name"
@vals << { name: nil, raw: nil }
@current_el = "name"
elsif el_name == "raw"
@current_el = "raw"
else
@current_el = nil
end
end
def end_element(el_name)
if el_name == "name" || el_name == "raw"
@current_el = nil
end
end
def characters(str)
str = str.strip
if @current_el == "name"
@vals.last[:name] = str
elsif @current_el == "raw"
@vals.last[:raw] = str
end
end
def end_document
pp @vals
end
end
您可以在 repl.it 上看到它的实际效果(但请注意,第一次运行需要很长时间,因为 Nokogiri):https://repl.it/@jrunning/SpitefulRichLists
关于ruby - Nokogiri SAX 忽略空字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56735220/
我一直在尝试为 SAX 解析器设置 UI 线程和处理程序。这是我没有实现 UI 线程和处理程序的解析器: public class AndroidXMLReader extends ListActiv
我正在尝试读取一个大型 XML 文档,并且我想以 block 的形式读取它,而不是 XmlDocument 将整个文件读入内存的方式。我知道我可以使用 XmlTextReader 来做到这一点,但我想
我正在尝试将 11384 个 XML 文件解析到一个 SQLite 数据库中。其中之一: ]> 1 2 我正在使用 SAX 解析器: pub
我需要用 Perl 解析 XML 文件。文件的一部分单独存储,并作为系统实体插入。不过,这个问题很常见。但是我无法获得任何信息来解决它。 ]> &externalContent; 当使
我正在尝试从 xsd 解析 HL7 消息定义。我将模式定义分成两个文件。第一个文件包含实际的消息定义,第二个文件包含消息中的段定义。 我正在尝试调整示例代码以从此处解析 XML https://gis
我正在使用 SAX 解析 MathML 表达式(尽管它是 MathML 的事实可能并不完全相关)。输入字符串示例为 λ 为了让 SAX 解
我正在尝试使用 Java 和 SAX 为 Android 设备解析 XML 文件。我从互联网上获取,在解析它时,我得到一个 ExpatException :字符“é”的格式不正确(无效标记)。有没有办
我正在尝试使用java中的sax读取xml文件。我只获得 endElement 的输出,但无法找出 startElement 出了什么问题。 这是我的处理程序: public class XMLHan
我正在使用 SAX 解析器来解析具有父级及其子级标记的 XML,如下所示:
我正在尝试从 RSS 提要中提取数据。 RSS 链接 - http://www.thehindu.com/sport/?service=rss ? 这是我的默认处理程序的字符方法。 public vo
我尝试执行下面的代码,但我的 SAX 解析器没有调用 startElement 方法。 下面是我的代码: package getTableStructure; import java.util.Lis
我正在满足一项要求,即我需要拆分大型 XML 并进一步处理。 这是 XML 示例,它可以变成单行。 yongjin 这是我的代码: import java.util.Arrays; import ja
我正在尝试将标签的内容放入我的 java Sax 解析器中的变量中。但是,Characters 方法仅返回 Char 数组。有没有办法将 Char 数组转换为 Int??? public void c
我有下面的代码.. System.setProperty("http.proxyHost","176.6.129.25") ; System.setProp
如何使用 SAX 显示树中最大深度的节点名称。该算法很适合我理解这个概念.. 例如,我应该如何使用 startelement、endelement、startdocument、enddocument
有没有可行的方法使用默认的处理程序类来查找对应的XML标签?例如... 1 1 我想使用 startElement() 和 endElement() 方
我想使用 SAX 解析器从 xml 文件中解析一些数据。我的xml如下: Pies & past Fruits 为了解析这些数据,我扩展了 DefaultHandler。 解析后的输出
我正在构建一个创建 XML 的流程(从各种来源并出于我事先不知道的各种目的),并且我希望将生成的 XML 直接注入(inject)到标准 XML 处理中,例如 SAX、StAX和 DOM。我已经完成了
我有一个由 MS Excel 创建的 XML 文件,其中包含如下元素: 22. Department"GS "NAES "ABCDEF"
我在使用 java sax 解析器打开 stackoverflow 帖子 XML 转储时遇到问题。它识别每个元素的结尾,但似乎跳过了 startElement 方法。我使用示例代码: try {
我是一名优秀的程序员,十分优秀!