我想将WSMAN给出的XML输出分解为多个XML文件,以便我可以解析输出。将XML文件分解成多个XML文件
WSMAN给我输出如下面基本上具有与每个具有其自己的根节点两个不同的XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<s:Body>
<wsen:PullResponse>
<wsen:Items>
<n1:DCIM_SoftwareIdentity>
<n1:ComponentType>BIOS</n1:ComponentType>
<n1:InstanceID>DCIM:CURRENT#741__BIOS.Setup.1-1</n1:InstanceID>
<n1:VersionString>1.3.6</n1:VersionString>
</n1:DCIM_SoftwareIdentity>
</wsen:Items>
</wsen:PullResponse>
</s:Body>
<?xml version="1.0" encoding="UTF-8"?>
<s:Body>
<wsen:PullResponse>
<wsen:Items>
<n1:DCIM_SoftwareIdentity>
<n1:ComponentType>BIOS</n1:ComponentType>
<n1:InstanceID>DCIM:INSTALLED#741__BIOS.Setup.1-1</n1:InstanceID>
<n1:VersionString>1.3.6</n1:VersionString>
</n1:DCIM_SoftwareIdentity>
</wsen:Items>
</wsen:PullResponse>
</s:Body>
我不能XML::Simple
解析上述输出如上述输出包含2个元件,其是“垃圾“在XML
问题/声明方面:
我想突破输出与各containi两个不同的XML文件如下面纳克它自己的根元素:
<?xml version="1.0" encoding="UTF-8"?>
<s:Body>
<wsen:PullResponse>
<wsen:Items>
<n1:DCIM_SoftwareIdentity>
<n1:ComponentType>BIOS</n1:ComponentType>
<n1:InstanceID>DCIM:CURRENT#741__BIOS.Setup.1-1</n1:InstanceID>
<n1:VersionString>1.3.6</n1:VersionString>
</n1:DCIM_SoftwareIdentity>
</wsen:Items>
</wsen:PullResponse>
</s:Body>
......
<?xml version="1.0" encoding="UTF-8"?>
<s:Body>
<wsen:PullResponse>
<wsen:Items>
<n1:DCIM_SoftwareIdentity>
<n1:ComponentType>BIOS</n1:ComponentType>
<n1:InstanceID>DCIM:INSTALLED#741__BIOS.Setup.1-1</n1:InstanceID>
<n1:VersionString>1.3.6</n1:VersionString>
</n1:DCIM_SoftwareIdentity>
</wsen:Items>
</wsen:PullResponse>
</s:Body>
我的逻辑:
1)解析由线的输出线
2 )如果遇到?xml version
模式,则创建一个新的XML文件并将?xml version
行和其他行写入此新文件,直到agai你遇到?xml version
模式。
3)按照步骤2每次遇到一次?xml version
模式
这里是我的代码:
#!/usr/bin/perl -w
use strict;
use XML::Simple;
use Data::Dumper;
my $counter = 0;
my $fileName;
while (my $line = <DATA>)
{
if ($line =~ /\?xml version/)
{
$counter++;
print "Creating the BIOS file \n";
$fileName = "BIOS"."_".$counter;
}
open (my $sub_xml_file, ">" , $fileName) or die "Canot create $fileName: $!\n";
print $sub_xml_file $line;
}
__DATA__
## omitting this part as this contains the XML info listed above.
现在,我的脚本将创建一个文件BIOS_1
和BIOS_2
但只写的最后一行以上XML输出到它:
# cat BIOS_1
</s:Body>
# cat BIOS_2
</s:Body>
你能帮我修复我的脚本,以创建两个区别ct XML文件...
您正在打开(和截断)输出文件输入的每一行。声明'my $ sub_xml_file;'在while循环之外,并在if块中打开($ sub_xml_file,...)。 – runrig 2013-02-22 15:49:32
+ 1,@ runrig,您的解释很有帮助。谢谢。 – slayedbylucifer 2013-02-22 16:08:04