是否有解析一个父 XML 节点中包含的所有信息的最佳实践? [英] Is there a best practice for parsing all information contained within one parent XML node?

查看:20
本文介绍了是否有解析一个父 XML 节点中包含的所有信息的最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 VB.NET 应用程序来解析一个大型 XML 文件,该文件是日语词典.我对 XML 解析完全陌生,也不知道自己在做什么.整个字典位于两个 XML 标签 之间.下一层是,它包含了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.

比如我应该如何提取开头的标签的内容?并且用于从 XML 标记中提取信息的方法是否相同,即使它包含在父标记中,如在 <keb><ke_pri> 标记中包含在 标签中?或者我应该使用不同的方法?

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

  1. 您正在将 xml 解析为字符串
  2. 您在解析时将行插入到 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆