是否存在解析包含在一个父 XML 节点中的所有信息的最佳实践? [英] Is there a best practice for parsing all information contained within one parent XML node?
问题描述
我正在编写一个 VB.NET 应用程序来解析一个大型 XML 文件,该文件是一个日语字典.我对 XML 解析完全陌生,我真的不知道我在做什么.整个字典位于两个 XML 标记 <jmdict>
和 </jmdict>
之间.下一层是<entry>
,它包含了100万个条目的所有信息,包括单词的形式、发音、含义等.
I'm writing a VB.NET application to parse a large XML file which is a Japanese dictionary. I'm completely new to XML parsing and don't really know what I'm doing. The whole dictionary fits between two XML tags <jmdict>
and </jmdict>
. The next level is the <entry>
, which contains all information for the 1 million entries, including the form, pronunciation, meaning of the word and so on.
典型的条目可能如下所示:
A typical entry might look like this:
<entry>
<ent_seq>1486440</ent_seq>
<k_ele>
<keb>美術</keb>
<ke_pri>ichi1</ke_pri>
<ke_pri>news1</ke_pri>
<ke_pri>nf02</ke_pri>
</k_ele>
<r_ele>
<reb>びじゅつ</reb>
<re_pri>ichi1</re_pri>
<re_pri>news1</re_pri>
<re_pri>nf02</re_pri>
</r_ele>
<sense>
<pos>&n;</pos>
<pos>&adj-no;</pos>
<gloss>art</gloss>
<gloss>fine arts</gloss>
</sense>
<sense>
<gloss xml:lang="dut">kunst</gloss>
<gloss xml:lang="dut">schone kunsten</gloss>
</sense>
<sense>
<gloss xml:lang="fre">art</gloss>
<gloss xml:lang="fre">beaux-arts</gloss>
</sense>
<sense>
<gloss xml:lang="ger">Kunst</gloss>
<gloss xml:lang="ger">die schönen Künste</gloss>
<gloss xml:lang="ger">bildende Kunst</gloss>
</sense>
<sense>
<gloss xml:lang="ger">Produktionsdesign</gloss>
<gloss xml:lang="ger">Szenographie</gloss>
</sense>
<sense>
<gloss xml:lang="hun">művészet</gloss>
<gloss xml:lang="hun">művészeti</gloss>
<gloss xml:lang="hun">művészi</gloss>
<gloss xml:lang="hun">rajzóra</gloss>
<gloss xml:lang="hun">szépművészet</gloss>
</sense>
<sense>
<gloss xml:lang="rus">изящные искусства; искусство</gloss>
<gloss xml:lang="rus">{~{的}} художественный, артистический</gloss>
</sense>
<sense>
<gloss xml:lang="slv">umetnost</gloss>
<gloss xml:lang="slv">likovna umetnost</gloss>
</sense>
<sense>
<gloss xml:lang="spa">bellas artes</gloss>
</sense>
</entry>
我有一个类对象,Entry
,它用于存储条目中包含的所有信息,例如上面的条目.我知道所有标签的含义,我在语义上解释数据没有问题,我只是不确定我需要什么工具来实际解析所有这些信息.
I have a class object, Entry
, which is used to store all of the information contained in an entry like the one above. I know what all the tags mean, I don't have an issue with interpreting the data semantically, I'm just not sure what tools I need to actually parse all of this information.
例如,我应该如何提取开头的<ent_seq>
标签的内容?并且即使它包含在父标记中,用于从 XML 标记中提取信息的方法是否与在 <keb>
和 <ke_pri>
标记中一样是否包含在 <k_ele>
标记中?还是应该使用其他方法?
For example, how should I extract the contents of the <ent_seq>
tag at the beginning? And is the method used to extract information from an XML tag the same even it's contained within a parent tag, as in the <keb>
and <ke_pri>
tags which are contained within the <k_ele>
tags? Or should I use a different method?
我知道这读起来像是作业帮助 - 我不是要求有人提供完整的解决方案并构建解析器.我只是不知道从哪里开始以及使用什么工具.对于开始解析 XML 文件需要哪些方法的指导,我真的很感激,一旦我知道自己在做什么,我就会自己构建解决方案.
I know this reads like homework help - I'm not asking for someone to provide the complete solution and build the parser. I just don't know where to start and what tools to use. I'd really appreciate some guidance on what methods I need to start parsing the XML file, and then I'll work on building the solution myself once I know what I'm doing.
-
编辑
所以我从 这个网站发现了这段代码 使用 XMLReader 一次通过一个节点:
So I've come across this code from this website which uses XMLReader to go through one node at a time:
Dim readXML As XmlReader = XmlReader.Create(New StringReader(xmlNode))
While readXML.Read()
Select Case readXML.NodeType
Case XmlNodeType.Element
ListBox1.Items.Add("<" + readXML.Name & ">")
Exit Select
Case XmlNodeType.Text
ListBox1.Items.Add(readXML.Value)
Exit Select
Case XmlNodeType.EndElement
ListBox1.Items.Add("")
Exit Select
End Select
End While
但我在第一行得到错误
'XmlNode'是类类型,不能作为表达式使用
'XmlNode' is a class type and cannot be used as an expression
我不确定如何处理这个错误 - 有什么想法吗?
I'm not exactly sure what to do about this error - any ideas?
推荐答案
你可以使用这些类快速反序列化你的xml
You can use these classes to deserialize your xml quickly
Imports System.IO
Imports System.Xml.Serialization
<XmlRoot>
Public Class jmdict
<XmlElement("entry")>
Public Property entries As List(Of entry)
End Class
Public Class entry
Public Property ent_seq As Integer
Public Property k_ele As k_ele
Public Property r_ele As r_ele
<XmlElement("sense")>
Public Property senses As List(Of sense)
End Class
Public Class sense
<XmlElement("pos")>
Public Property posses As List(Of String)
<XmlElement("gloss")>
Public Property glosses As List(Of gloss)
End Class
Public Class k_ele
Public Property keb As String
<XmlElement("ke_pri")>
Public Property ke_pris As List(Of String)
End Class
Public Class r_ele
Public Property reb As String
<XmlElement("re_pri")>
Public Property re_pris As List(Of String)
End Class
Public Class gloss
<XmlAttribute("xml:lang")>
Public Property lang As String
<XmlText>
Public Property Text As String
Public Overrides Function ToString() As String
Return Text
End Function
End Class
要反序列化的代码是
Dim serializer As New XmlSerializer(GetType(jmdict))
Dim d As jmdict
Using sr As New StreamReader("filename.xml")
d = CType(serializer.Deserialize(sr), jmdict)
End Using
现在您可以遍历每个条目,以及条目的感官和感官的光泽
Now you can iterate over each entry, and the entries' senses, and the senses' glosses
For Each e In d.entries
Console.WriteLine($"seq: {e.ent_seq}")
For Each s In e.senses
For Each g In s.glosses
Console.WriteLine($"Text: {g.Text}, Lang: {g.lang}")
Next
Next
Next
您的代码需要这么长时间的原因是
The reasons your code takes so long are
- 您正在将 xml 解析为字符串
- 在解析行时将行插入到 ListBox 中
你想在 ListBox 中放什么?如果你像我展示的那样反序列化了,你可以从数据中绑定一个特定的列表,或者多个列表的查询结果.
What do you want to put in the ListBox? If you have deserialized as I show, you can databind a specific list from the data, or a queried result of multiple lists.
这篇关于是否存在解析包含在一个父 XML 节点中的所有信息的最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!