Xidel:将属性解析为新对象

时间:2015-08-17 12:32:50

标签: json xml shell xpath xidel

给出来自任何Java虚拟机的详细GC日志(可以是任何xml,因此不用java标记):

<?xml version="1.0" ?>

<verbosegc version="versioninformation">

<af type="nursery" id="49383" timestamp="Jan 01 01:34:54 2015" intervalms="33.821">
  <tenured freebytes="769243504" totalbytes="1610416128" percent="47" >
    <soa freebytes="198858272" totalbytes="805208064" percent="24" />
    <loa freebytes="570385232" totalbytes="805208064" percent="70" />
  </tenured>
  <gc></gc>
  <tenured freebytes="768800232" totalbytes="1610416128" percent="47" >
    <soa freebytes="198415" totalbytes="805208064" percent="24" />
    <loa freebytes="570385232" totalbytes="805208064" percent="70" />
  </tenured>
</af>

<af type="nursery" id="49384" timestamp="Jan 01 01:35:54 2015" intervalms="40.877">
  <tenured freebytes="768800232" totalbytes="1610416128" percent="47" >
    <soa freebytes="198415" totalbytes="805208064" percent="24" />
    <loa freebytes="570385232" totalbytes="805208064" percent="70" />
  </tenured>
  <gc></gc>
  <tenured freebytes="768320928" totalbytes="1610416128" percent="47" >
    <soa freebytes="197935696" totalbytes="805208064" percent="24" />
    <loa freebytes="570385232" totalbytes="805208064" percent="70" />
  </tenured>
</af>

所以,我想创建一个新对象,每个垃圾收集周期都会重复这个对象,包括时间戳和使用的字节数(总减去免费)。计算工作正常,但输出没有。这是我希望得到的:

[
  {
    "timestamp": "Jan 01 01:34:54 2015",
    "used": 8.41172624E8
  },
  {
    "timestamp": "Jan 01 01:35:54 2015",
    "used": 8.41615896E8
  },
]

我尝试了这个命令行,遗憾地创建了一个空时间戳和一长串堆信息:

xidel --input-format=xml   -e "//af/tenured[1]/(heap:={used:=(@totalbytes - @freebytes):timestamp:=@timestamp})" gc.log --output-format=json-wrapped

输出如下:

[
  {
    "timestamp": [null],
    "used": [8.41172624E8, 8.41615896E8]
  }
]

显然不是我所感谢的。

2 个答案:

答案 0 :(得分:1)

您实际上可以在提取表达式中编写整个JSON结构,而不需要输出格式:

xidel --input-format=xml   -e "[ //af/tenured[1]/{'used':(@totalbytes - @freebytes),'timestamp':../@timestamp} ]" gc.log 

答案 1 :(得分:0)

正确的想法是不将对象分配给变量(heap:={})并直接在xpath中使用json表示法。第三,时间戳是一个高于终身的水平,所以我把../放在前面。

所以这个有效:

xidel --input-format=xml   -e "//af/tenured[1]/('used':(@totalbytes - @freebytes),'timestamp':../@timestamp)" gc.log --output-format=json-wrapped

输出:

[
    [
        {
            "timestamp": "Jan 01 01:34:54 2015",
            "used": 841172624.0
        },
        {
            "timestamp": "Jan 01 01:35:54 2015",
            "used": 841615896.0
        },
    ]
]

它提供了一个额外的阵列,但我可以忍受。至少它是一个稳定的输出和可解析的。

相关问题