2013-02-22 2 views
0

WSMAN에서 제공 한 XML 출력을 여러 XML 파일로 나누어 출력을 구문 분석 할 수 있습니다. 기본적으로 각각은 자신의 루트 노드와 두 가지 XML 파일이있는 아래로XML 파일을 여러 XML 파일로 분할

WSMAN 나에게 출력을 제공 : I 출력 위와 같이 XML::Simple로 출력 위의 구문 분석 할 수 없습니다

<?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

질문/계산서의 측면에서 : 나는 각 containi 두 개의 별개의 XML 파일로 출력 돌파 할

다음과 같이 자신의 루트 요소를 NG :

<?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 행 및 추가 행을이 새 파일에 작성하십시오. n ?xml version 패턴이 있습니다. 2 단계 당신이 발생할 때마다 따라

3) ?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_1BIOS_2를 생성하지 않습니다하지만의 마지막 줄을 쓴다 위의 XML 출력 위에 :

두 개의 distin을 만들기 위해 스크립트를 수정하도록 도와 줄 수 있습니까? ct XML 파일 ...

+1

당신은 입력의 모든 행에 출력 파일을 열어 (및 절단)된다. '$ $ sub_xml_file;을 (를) 선언하십시오. while 루프 외부에서는 if 블록 내에서 open ($ sub_xml_file, ...)을 사용합니다. – runrig

+0

+1, @ runrig, 귀하의 설명이 도움이됩니다. 감사. – slayedbylucifer

답변

0

향후 루프 전달을 위해 $line을 보존하지 마십시오. 메모리 접근

로드의 모든 : 한 번에 접근에서

my $count; 
my $file; { local $/; $file = <>; } 
for my $xml (split /^(?=<\?xml)/m, $file) { 
    my $fn = sprintf("BIOS_%d.xml", ++$count); 
    open(my $fh, '>', $fn) or die $!; 
    print $fh $xml; 
} 

라인 :

my $fh; 
my $count; 
while (<>) { 
    if (/^<\?xml/) { 
     my $fn = sprintf("BIOS_%d.xml", ++$count); 
     open($fh, '>', $fn) or die $!; 
    } 

    print $fh $_; 
} 
+0

감사합니다. 나는 "한 번에 한 라인 씩"접근 방식으로 작업 중이며 작동 중이다. 제 코드를 수정하도록 도와주세요. 나는 당신이 "$ line을 결코 지키지 않는다"는 말을 이해하지 못했습니다. – slayedbylucifer

+0

읽은 행은 저장하지 않습니다. 따라서 인쇄 할 때 사용할 수 없습니다. – ikegami

관련 문제