使用 Python 解析复杂的 xml 文件

Parse a complex xml file with Python

我正在尝试解析 XML 格式的网络流量矩阵文件。它包括源(源)和目标节点之间的流量。我有一个序列流量矩阵,这意味着流量随着时间的推移而变化,并由时间序列 0 到时间序列 N 表示。例如,我附上了下面的文件,其中包含 XML 流量矩阵,其中,有两个时间序列(即序列 0 和 1),每个时间序列有两个起点(即 20 和 21)和一组目的地(即 32、33、34、35、28、29、30、31 ).浮点数是以 Mbps 为单位的流量。

我正在寻找的是解析这样的矩阵并找到每个 [origin--destination] 随时间的流量。

例如:我有兴趣只从文件中获取以下信息,

            Time seq"0"
                       Origin --  Destination --  Traffic volume

                        20    --      32     --   0.278971163226 
                        .
                        . 
                        .
                        20   - -      31     --   0
                        21    --      32     --   0.0720389596699 
                        .
                        . 
                        .
                        21   - -      31     --   0.646495362713
            Time seq"1"
                       
                        20    --      32     --   0.348919367445 
                        .
                        . 
                        .
                        20   - -      31     --   0.348919367445
                        21    --      32     --   0.264369876118 
                        .
                        . 
                        .
                        21   - -      31     --   0

这是 XML 文件。

-<traffic-matrix type="sequence">
 -<time seq="0">
   <property name="volume_unit" type="string">Mbps</property>
  -<origin id="20" id.type="int">
     <destination id="32" id.type="int">0.278971163226</destination>
     <destination id="33" id.type="int">0.21534822478</destination>
     <destination id="34" id.type="int">0.417878716061</destination>
     <destination id="35" id.type="int">0.126812437403</destination>
     <destination id="28" id.type="int">0.0</destination>
     <destination id="29" id.type="int">0.377649042951</destination>
     <destination id="30" id.type="int">0.230012161609</destination>
     <destination id="31" id.type="int">0.0</destination>
   </origin>
  -<origin id="21" id.type="int">
    <destination id="32" id.type="int">0.0720389596699</destination>
    <destination id="33" id.type="int">0.0194404241696</destination>
    <destination id="34" id.type="int">0.416078923017</destination>
    <destination id="35" id.type="int">0.271277529292</destination>
    <destination id="28" id.type="int">0.0219082425921</destination>
    <destination id="29" id.type="int">0.0</destination>
    <destination id="30" id.type="int">0.0645726634169</destination>
    <destination id="31" id.type="int">0.646495362713</destination>
   </origin>
 </time>
-<time seq="1">
   <property name="volume_unit" type="string">Mbps</property>
  -<origin id="20" id.type="int">
    <destination id="32"id.type="int">0.348919367445</destination>
    <destination id="33" id.type="int">0.0</destination>
    <destination id="34" id.type="int">0.0</destination>
    <destination id="35" id.type="int">0.0</destination>
    <destination id="28" id.type="int">0.0</destination>
    <destination id="29" id.type="int">0.11926219607</destination>
    <destination id="30" id.type="int">0.0</destination> 
    <destination id="31" id.type="int">0.128091658298</destination>
   </origin>
  -<origin id="21" id.type="int">
    <destination id="32" id.type="int">0.264369876118</destination>
    <destination id="33" id.type="int">0.0</destination>
    <destination id="34" id.type="int">0.136801239629</destination>
    <destination id="35" id.type="int">0.172101959022</destination> 
    <destination id="28" id.type="int">0.289540759798</destination>
    <destination id="29" id.type="int">0.0</destination>
    <destination id="30" id.type="int">0.519869498363</destination>
    <destination id="31" id.type="int">0.0</destination>
   </origin>
 </time>
</traffic-matrix>

这是使用 beautifulsoup 库的解决方案 - 此处用于解析 XML。该脚本将从数据中创建 pandas DataFrame(xml_data 是问题中的 XML):

import pandas as pd
from bs4 import BeautifulSoup


soup = BeautifulSoup(xml_data, 'html.parser')

all_data = []
for d in soup.select('destination'):
    all_data.append({
        'Time_seq': int(d.find_previous('time')['seq']),
        'Origin': int(d.find_previous('origin')['id']),
        'Destination': int(d['id']),
        'Traffic volume': d.text
    })

df = pd.DataFrame(all_data)
print(df)

打印:

    Time_seq  Origin  Destination   Traffic volume
0          0      20           32   0.278971163226
1          0      20           33    0.21534822478
2          0      20           34   0.417878716061
3          0      20           35   0.126812437403
4          0      20           28              0.0
5          0      20           29   0.377649042951
6          0      20           30   0.230012161609
7          0      20           31              0.0
8          0      21           32  0.0720389596699
9          0      21           33  0.0194404241696
10         0      21           34   0.416078923017
11         0      21           35   0.271277529292
12         0      21           28  0.0219082425921
13         0      21           29              0.0
14         0      21           30  0.0645726634169
15         0      21           31   0.646495362713
16         1      20           32   0.348919367445
17         1      20           33              0.0
18         1      20           34              0.0
19         1      20           35              0.0
20         1      20           28              0.0
21         1      20           29    0.11926219607
22         1      20           30              0.0
23         1      20           31   0.128091658298
24         1      21           32   0.264369876118
25         1      21           33              0.0
26         1      21           34   0.136801239629
27         1      21           35   0.172101959022
28         1      21           28   0.289540759798
29         1      21           29              0.0
30         1      21           30   0.519869498363
31         1      21           31              0.0