property.xml을 읽고 install.properties 파일을 작성하는 Perl 스크립트
여러 줄을 읽고 keyName과 keyValue를 분할하는 방법
#!/usr/bin/perl
use strict;
use warnings;
open my $fh, '<', "property.xml" or die "property.xml: $!";
open(CTS,">install.properties") or die $!;
while ( my $line = <$fh> ) {
if ($line =~ m/\<entry.*\<\/entry\>$/i ){ # how to read multiple line
my ($keyName, $keyValue) = split(//, $line); # how to split
print CTS $keyName = $keyValue;
}
}
property.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="TYPE">
Rel
</entry>
<!-- tst -->
<entry key="LOCATION">
C:/Rel-LOCATION
</entry>
<entry key="HOST">
localhost
</entry>
</properties>
install.properties
TYPE = Rel
LOCATION = C:/Rel-LOCATION
HOST = localhost
XML :: LibXML을 사용하여 다음과 같이 코드를 작성합니다.
use strict;
use warnings;
use feature 'say';
use XML::LibXML;
# Parse the XML
my $xml = XML::LibXML->load_xml(location => 'test.xml');
# Iterate the entries
for my $entry ($xml->findnodes('/properties/entry')) {
my $key = $entry->findvalue('@key');
# trim the value
s/\A\s*//, s/\s*\z// for my $value = $entry->textContent;
say "$key = $value";
}
이 진술 s/\A\s*//, s/\s*\z// for my $value = $entry->textContent
은
my $value = $entry->textContent;
$value =~ s/\A\s*//;
$value =~ s/\s*\z//;
대체는 트리밍을 수행합니다. \A
문자열의 시작 부분, \z
끝 부분에 고정됩니다 (대략적으로 동등 ^
하고 $
resp.).
이 say
기능은 perl5 v10부터 사용할 수 있으며, say
처럼 작동 print
하지만 개행 문자를 추가하는 기능을 제공합니다 .
간단한 XPath 표현식은 파일 경로와 동일하게 작동하지만 @key
속성을 선택합니다.
여기에서 사용한 다양한 DOM 노드 메서드는 다음과 같습니다.
findnodes
– XPath 표현식과 일치하는 노드 목록을 가져옵니다.findvalue
– XPath와 일치하는 노드를 가져 와서 텍스트 값을 반환합니다.textContent
– 텍스트 노드의 값을 반환합니다.이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다