解析XML文件并填充另一个文件中的一些数据

时间:2013-11-28 18:30:32

标签: xml parsing xml-parsing

我有一个包含一些标签和值的XML文件。

<component id="system.core0.icache" name="icache">
    <param name="icache_config" value="65536,64,2,1,1,2,64,0"/>
    <stat name="read_accesses" value="526692.38"/> 
    <stat name="read_misses" value="2646.34"/>                                 
        <stat name="conflicts" value="2404.67"/>
</component>
<component id="system.core0.dcache" name="dcache">
    <param name="dcache_config" value="65536,64,2,1,1,2,64,0"/>
    <stat name="read_accesses" value="326692.38"/> 
    <stat name="read_misses" value="1646.34"/>                                 
        <stat name="conflicts" value="4404.67"/>
</component>

在另一个文件中,我有另一种格式的值。例如

 L1D:size 65536
 L1I:size 65536
 L1D:access 526692.38
 L1D:miss 2646.34
 L1D:conflict 2404.67
 L1I:access 326692.38
 L1I:miss 1646.34
 L1I:conflict 4404.67

我想要做的是编写一个基于字典的解析器,将stat文件中的值复制到XML文件中。例如

 L1D:size   ->   component.id = "system.core0.dcache" , fill the first value of param "dcache_config"

 L1I:access ->   component.id = "system.core0.icache" , fill the stat value of "read_accesses"

我已搜索但似乎解析XML文件意味着从XML文件中提取值。但我不希望这样。

我该怎么做?

1 个答案:

答案 0 :(得分:1)

听起来像是SAX解析器的解决方案。

while next element
    compare attributes and content
    if necessary then replace
    output
end

Ruby或Perl会成为工具。

相关问题