我有以下XSD架构 http://release.niem.gov/niem/apco/2.1/apco.xsd,和 我编写了以下Perl脚本(我是Perl的新手,我可能最终会用C#编写它):
#!/usr/bin/perl
use warnings;
use strict;
# use module
use XML::Simple;
use Data::Dumper;
my $data = XMLin('apco.xsd');
print Dumper($data);
for my $key1 (keys %$data) {
print "$key1\n";
for my $array_value (@{ $data->{$key1} }) {
for my $key2 (keys %$array_value) {
print "$key2 : $array_value->{$key2}\n";
}
}
}
我得到了以下输出:
$VAR1 = {
'xsd:import' => [
{
'namespace' => 'http://niem.gov/niem/structures/2.0',
'schemaLocation' => '../../structures/2.0/structures.xsd'
},
{
'namespace' => 'http://niem.gov/niem/appinfo/2.0',
'schemaLocation' => '../../appinfo/2.0/appinfo.xsd'
}
],
'version' => '1',
'xmlns:s' => 'http://niem.gov/niem/structures/2.0',
'targetNamespace' => 'http://niem.gov/niem/apco/2.1',
'xmlns:i' => 'http://niem.gov/niem/appinfo/2.0',
'xsd:simpleType' => {
'AlarmEventCategoryCodeSimpleType' => {
'xsd:restriction' => {
'base' => 'xsd:token',
'xsd:enumeration' => [
{
'value' => 'Medical',
'xsd:annotation' => {
'xsd:documentation' => 'Medical Alarm'
}
},
{
'value' => 'FIRE',
'xsd:annotation' => {
'xsd:documentation' => 'Fire'
}
},
{
'value' => 'Comm',
'xsd:annotation' => {
'xsd:documentation' => 'Communication Fail'
}
},
{
'value' => 'BURG',
'xsd:annotation' => {
'xsd:documentation' => 'Burglary'
}
},
{
'value' => 'Holdup',
'xsd:annotation' => {
'xsd:documentation' => 'Holdup / Duress'
}
}
]
},
'xsd:annotation' => {
'xsd:appinfo' => {
'i:Base' => {
'i:name' => 'Object',
'i:namespace' => 'http://niem.gov/niem/structures/2.0'
}
},
'xsd:documentation' => 'A data type for kinds of alarm events.'
}
},
'AlarmEventResponseActionCodeSimpleType' => {
'xsd:restriction' => {
'base' => 'xsd:token',
'xsd:enumeration' => [
{
'value' => 'respond',
'xsd:annotation' => {
'xsd:documentation' => 'respond'
}
},
{
'value' => 'notify',
'xsd:annotation' => {
'xsd:documentation' => 'notify'
}
}
]
},
'xsd:annotation' => {
'xsd:appinfo' => {
'i:Base' => {
'i:name' => 'Object',
'i:namespace' => 'http://niem.gov/niem/structures/2.0'
}
},
'xsd:documentation' => 'A data type for actions requested of an alarm event responder.'
}
},
'AlarmEventLocationCategoryCodeSimpleType' => {
'xsd:restriction' => {
'base' => 'xsd:token',
'xsd:enumeration' => [
{
'value' => 'commercial',
'xsd:annotation' => {
'xsd:documentation' => 'commercial'
}
},
{
'value' => 'residential',
'xsd:annotation' => {
'xsd:documentation' => 'residential'
}
}
]
},
'xsd:annotation' => {
'xsd:appinfo' => {
'i:Base' => {
'i:name' => 'Object',
'i:namespace' => 'http://niem.gov/niem/structures/2.0'
}
},
'xsd:documentation' => 'A data type for the kinds of location at which an alarm event occurs.'
}
}
},
'xmlns:apco' => 'http://niem.gov/niem/apco/2.1',
'xsd:annotation' => {
'xsd:appinfo' => {
'i:ConformantIndicator' => 'true'
},
'xsd:documentation' => 'Association of Public-Safety Communications Officials (APCO) - International, Inc.'
},
'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
'xsd:complexType' => {
'AlarmEventCategoryCodeType' => {
'xsd:annotation' => {
'xsd:appinfo' => {
'i:Base' => {
'i:name' => 'Object',
'i:namespace' => 'http://niem.gov/niem/structures/2.0'
}
},
'xsd:documentation' => 'A data type for kinds of alarm events.'
},
'xsd:simpleContent' => {
'xsd:extension' => {
'base' => 'apco:AlarmEventCategoryCodeSimpleType',
'xsd:attributeGroup' => {
'ref' => 's:SimpleObjectAttributeGroup'
}
}
}
},
'AlarmEventResponseActionCodeType' => {
'xsd:annotation' => {
'xsd:appinfo' => {
'i:Base' => {
'i:name' => 'Object',
'i:namespace' => 'http://niem.gov/niem/structures/2.0'
}
},
'xsd:documentation' => 'A data type for actions requested of an alarm event responder.'
},
'xsd:simpleContent' => {
'xsd:extension' => {
'base' => 'apco:AlarmEventResponseActionCodeSimpleType',
'xsd:attributeGroup' => {
'ref' => 's:SimpleObjectAttributeGroup'
}
}
}
},
'AlarmEventLocationCategoryCodeType' => {
'xsd:annotation' => {
'xsd:appinfo' => {
'i:Base' => {
'i:name' => 'Object',
'i:namespace' => 'http://niem.gov/niem/structures/2.0'
}
},
'xsd:documentation' => 'A data type for the kinds of location at which an alarm event occurs.'
},
'xsd:simpleContent' => {
'xsd:extension' => {
'base' => 'apco:AlarmEventLocationCategoryCodeSimpleType',
'xsd:attributeGroup' => {
'ref' => 's:SimpleObjectAttributeGroup'
}
}
}
}
}
};
xsd:import
namespace : http://niem.gov/niem/structures/2.0
schemaLocation : ../../structures/2.0/structures.xsd
namespace : http://niem.gov/niem/appinfo/2.0
schemaLocation : ../../appinfo/2.0/appinfo.xsd
version
Can't use string ("1") as an ARRAY ref while "strict refs" in use at C:\Users\asraina\Downloads\script.pl line 16.
现在,
我不是Perl的专家,我更担心JSON表示的正确性。
答案 0 :(得分:1)
不,但错误与格式无关,Perl无法自行验证JSON。
这在很大程度上取决于您希望如何格式化JSON。这个问题没有一个答案。但是,首先,您应该熟悉JSON格式。
好的第一个位置可能是http://json.org,http://json.org/example.html,提供了一些比较JSON和XML的示例。
有关JSON的基本摘要:
<强>阵列强>
[ 1, 2, 3, ... , n ]
对象,哈希,关联数组等
{ k1 : v1, k2 : v2, ... , kn : vn }
其中所有k都是字符串(见下文),v可以是任何类型。
基本类型如下:
<强>字符串强>
"anything in double quotes"
<强>数强>
包含或不包含.
的任何数字字符串,例如
1
3.14
其他基本类型
null
true
false
这些可以合并如下:
{ "names" :
[ "Newton", "Copernicus", "Einstein" ],
"constants" :
[ 3.14159, 1.618033, 0 ],
"valid" : true,
"dates" : {
"first" : "2001-02-14",
"last" : "2001-11-25"
}
}
JSON::XS是一个很好的Perl模块,用于对来自JSON的Perl数据结构进行序列化和反序列化。
修改强>
此链接还可能包含一些有用的信息:Generate Json schema from XML schema (XSD)