使用Perl在XML到JSON转换中获取额外的变量

拉杰什

我的程序在输出中给出了额外的变量$ t。谁可以帮我这个事?

use XML::XML2JSON;

xml内容

my $XML = '<file><sno>1</sno><process>VALID</process><validation_type>C</validation_type><file_type>HTML</file_type><line>2</line><column>78</column><status>0</status><type>Warning</type><code>001</code><rule>aligning content.</rule><desc>Check that non-breaking space.</desc></file>';
  


my $XML2JSON = XML::XML2JSON->new();
my $JSON = $XML2JSON->convert($XML);
print $JSON;

输出-额外的变量即将到来$ t

{
    "@encoding": "UTF-8",
    "@version": "1.0",
    "file": {
        "status": {
            "$t": "0"
        },
        "rule": {
            "$t": "aligning content."
        },
        "validation_type": {
            "$t": "C"
        },
        "process": {
            "$t": "VALID"
        },
        "sno": {
            "$t": "1"
        },
        "file_type": {
            "$t": "HTML"
        },
        "desc": {
            "$t": "Check that non-breaking space."
        },
        "type": {
            "$t": "Warning"
        },
        "code": {
            "$t": "001"
        },
        "line": {
            "$t": "2"
        },
        "column": {
            "$t": "78"
        }
    }
}

预期输出为:

{
   "sno": "1",
   "process": "VALID",
   "validation_type": "C",
   "file_type": "HTML",
   "line": "2",
   "column": "78",
   "status": "0",
   "type": "Warning",
   "code": "001",
   "rule": "aligning content.",
   "desc": "Check that non-breaking space."
}
vkk05

$tcontent key作为中提到的XML :: XML2JSON文档。


如果您打算将转换XMLJSON,则建议使用XML::Simple,以后可以使用进行编码JSON.pm

代码如下:

#!/usr/bin/perl

use strict;
use warnings;

use JSON;
use XML::Simple;

#Create an object
my $xmlSimple = new XML::Simple;

my $XML = '<file><sno>1</sno><process>VALID</process><validation_type>C</validation_type><file_type>HTML</file_type><line>2</line><column>78</column><status>0</status><type>Warning</type><code>001</code><rule>aligning content.</rule><desc>Check that non-breaking space.</desc></file>';

#either we can pass a variable or a xml file which contains xml data
my $dataXML = $xmlSimple->XMLin($XML); 

my $jsonString = encode_json($dataXML);

print $jsonString;

输出:

{
    "process":"VALID",
    "line":"2",
    "column":"78",
    "type":"Warning",
    "file_type":"HTML",
    "sno":"1",
    "status":"0",
    "rule":"aligning content.",
    "code":"001",
    "desc":"Check that non-breaking space.",
    "validation_type":"C"
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章