首页 > 解决方案 > 将 xml 文件差异转换为图形可视化

问题描述

我有两个具有结构的列表,并且希望将结构差异呈现为层次结构或图形,并且如果值存在差异,则还可以将具有相同结构的元素的值差异呈现。我是否需要展平两个列表并逐个元素进行比较,然后将 relist 与逐个元素的差异一起使用来创建一个结构化的差异列表以发送到渲染包?或者有捷径吗?我对渲染两个列表之间不常见的结构以及存在价值差异的常见结构感兴趣。到目前为止的代码

library(XML)
library(plyr)
f1="sample1.xml"
f2="sample2.xml"
skel1=xmlToList(f1,simplify=TRUE)
skel2=xmlToList(f2,simplify=TRUE)
unlistf1=unlist(skel1)
unlistf2=unlist(skel2)
f1NOTf2=!((unlistf1) %in% (unlistf2)) #value and structure differnce combined
f2NOTf1=!((unlistf2) %in% (unlistf1))
strf1NOTf2=!(names(unlistf1) %in% names(unlistf2)) # structural difference only
strf2NOTf1=!(names(unlistf2) %in% names(unlistf1))
listf1NOTf2=relist(f1NOTf2,skel1)
listf2NOTf1=relist(f2NOTf1,skel2)
strlistf1NOTf2=relist(strf1NOTf2,skel1)
strlistf2NOTf1=relist(strf2NOTf1,skel2)

sample1.xml 和 sample2.xml 下面

<?xml version="1.0"?>
<data>
    <country xmlns="http://auto.org/schema/r4.0" name="Liechtenstein">
        <rank>-1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
	<mountain name = "Himalaya"/> 
    <country name="Singapore">
		<size name="small" />
		<Temp>Hot</Temp>
        <rank>4</rank>
        <year>2015</year>
        <gdppc>69900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
	<country name="Japan">
	</country>
</data>

<?xml version="1.0"?>
<data>
    <country name="liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100 </gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapora" color="Green">
        <rank>40</rank>
        <gdppc>59900<free>TRUE</free></gdppc>
        <neighbor name="Malaysia" direction="N"/>
		<year>2011</year>
    </country>
    <country name="Panama">
        <rank>100</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>

标签: rlistcomparerender

解决方案


推荐阅读