遍历多级XML文件 [英] Iterating through a multi-level XML file

查看:52
本文介绍了遍历多级XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假定具有以下结构的XML文件(如下).所有深度"不同的家庭"节点都在根"元素下.(对可能不正确的术语表示歉意;这是XML的新功能.)

Assume an XML file with the following structure (below). All "Family" nodes of varying depth' are under the 'root' element. (Apologies for possible incorrect terms; new at this XML stuff.)

在家庭"节点下,有几层祖先"或几代人.因此,在下面的示例中,"David"(级别1)的父亲名为"Samuel"(级别2),父亲的名称为"Fred"(级别3),等等.

Under the "Family" node, there are layers of "Ancestors" or generations. So in the example below, "David" (level 1) has a father named "Samuel" (level 2), who has a father named "Fred" (level 3), etc.

有多个家庭"节点.每个家族可以具有可变数量的祖先"层.在下面的示例中,祖先"的深度为5代.其他家庭"节点的深度可能为3代或8代.每个家庭"节点中的世代数都是未知的.

There are multiple "Family" nodes. Each Family may have a variable number of layers of "Ancestors". In the example below, the "Ancestors" are 5 generations deep. Other "Family" Nodes might be 3 generations deep, or 8 generations deep. The number of generations in each "Family" node is unknown.

我需要显示以下名称:

David ->Samuel->Fred->John->Frank->Robert  (for a 5 generation list)
Mary->Lisa->Karen  (for a 3 generation list)

因此,我需要遍历每个Family节点,就像Family一样深"(世代数"),并产生一个类似于上面的名称列表.每个家庭的世代数可能不同.

So I need to loop through each Family node, as 'deep' (number of 'generations') as that Family goes, and produce a list of names like above. The number of generations might be different for each family.

 <root>
    <Family>
        <ID>7588784011</ID>
        <Name>David</Name>
        <Ancestors>
            <Family>
                <ID>157050011</ID>
                <Name>Samuel</Name>
                <Ancestors>
                    <Family>
                        <ID>157028011</ID>
                        <Name>Fred</Name>
                        <Ancestors>
                            <Family>
                                <ID>154606011</ID>
                                <Name>John</Name>
                                <Ancestors>
                                    <Family>
                                        <ID>133141011</ID>
                                        <Name>Frank</Name>
                                        <IsCategoryRoot>1</IsCategoryRoot>
                                        <Ancestors>
                                            <Family>
                                                <ID>133140011</ID>
                                                <Name>Robert</Name>
                                            </Family>
                                        </Ancestors>
                                    </Family>
                                </Ancestors>
                            </Family>
                        </Ancestors>
                    </Family>
                </Ancestors>
            </Family>
        </Ancestors>
    </Family>
    <Family>
        // more ancestors of a varying number
    </Family>
 </root>

谢谢.

已添加

我已经在此方面取得了一些进展,其代码如下所示:我的代码位于 https://3v4l.org/GV0UV .该代码具有XML内容,家族成员的名称以每个家族的不同编号开头.

I've made some progress on this, with the code as shown in My code is here https://3v4l.org/GV0UV . The code has the XML content, with the names of family members starting with a different number for each family.

我得到每个家庭成员输出的名称,但是所有名称都在一行上.目的是使每个家庭的名称显示在单独的行上.

I get the names of each family member output, but all names are on one line. The intent is to have each family's names displayed on a separate line.

因此,当系列更改时,我需要插入< br> .

So I need to insert a <br> when the family changes.

推荐答案

这可以归结为基础计算机科学.您具有递归树结构,要对其进行操作,通常需要使用递归树遍历算法.这就是为什么许多人认为XSLT是处理XML的最简单解决方案的原因,因为递归树遍历模式内置于其处理模型(xsl:apply-templates和模板规则)中.

This comes down to basic computer science. You have a recursive tree structure, and to manipulate it you will generally want to use a recursive tree-walking algorithm. This is why many people find XSLT the simplest solution for processing XML, because the recursive tree-walking pattern is built in to its processing model (xsl:apply-templates and template rules).

但是您可以在任何现代编程语言中执行相同的操作.基本构建块是执行以下功能的函数:

But you can do the same thing in any modern programming language. The basic building block is a function that does:

function process(element X) {
  do something with X
  for each child C of X {
     process(C)
  }
  do something more with X
}

不幸的是,XML树的术语与您的家谱的术语冲突,因为您正在使用XML中的子节点来代表您家中的祖先.这只是要提防的事情,这样您就不会糊涂了.

The terminology of XML trees unfortunately clashes with the terminology of your family tree, because you are using child nodes in the XML to represent ancestors in your family. That's just something to watch out for so you don't get muddled.

由于您的树实际上没有分支(似乎没有人有一个以上的直接祖先),因此实际上可以简化解决方案.在XPath 2.0中,它只是 string-join(//Name,'->').但这也许是利用您的示例的属性,而该属性不能推广到真实数据.

Since your tree doesn't actually branch (no-one seems to have more than one immediate ancestor) the solution can actually be simplified; in XPath 2.0 it's simply string-join(//Name, '->'). But perhaps that's exploiting a property of your example which doesn't generalise to the real data.

这篇关于遍历多级XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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