을 사용하는 일부 코드를 내 fatcow 서버에서 사용할 수 없으므로 XML::DOM
대신 사용하려고합니다.펄에 새로운 XML :: DOM :: Document 루트를 만드는 법
XML::DOM::Document
개체의 루트 문서는 어떻게 설정합니까? XML::DOM::Document
에는 setDocumentElement
메서드가 없습니다.
다음은 문제의 코드입니다 (XML::XML2JSON
). 별표로 표시된 항목을 바꾸는 방법을 알아 내려고합니다.
=head2 obj2dom
Takes a perl data structure as input. (Must be a hashref.)
Returns an XML::DOM::Document object. #JMO
This method expects the object to be in the same format as
would be returned by the xml2obj method.
=cut
sub obj2dom
{
my ($Self, $Obj) = @_;
croak "Object must be a hashref" unless ref($Obj) eq 'HASH';
my $Version = $Obj->{ $Self->{attribute_prefix} . 'version' } || $Obj->{'version'} || '1.0';
my $Encoding = $Obj->{ $Self->{attribute_prefix} . 'encoding' } || $Obj->{'encoding'} || 'UTF-8';
#my $Dom = $XMLPARSER->createDocument($Version, $Encoding); #JMO
my $Dom = XML::DOM::Document->new(); #JMO
$Dom->setXMLDecl($Dom->createXMLDecl($Version, $Encoding)); #JMO
my $GotRoot = 0;
#delete @$Obj{ grep { /^$Self->{attribute_prefix}/ } keys %$Obj };
foreach my $Key (keys %$Obj)
{
$Obj->{$Key} = "" unless defined($Obj->{$Key});
my $RefType = ref($Obj->{$Key});
warn "Value ref type for $Key is: $RefType (value seems to be $Obj->{$Key})" if $Self->{debug};
my $Name = $Key;
# replace a "$" in the name with a ":"
$Name =~ s/([^^])\$/$1\:/;
if ($RefType eq 'HASH')
{
warn "Creating root element: $Name" if $Self->{debug};
croak "You may only have one root element: $Key" if $GotRoot;
$GotRoot = 1;
my $Root = $Dom->createElement($Name);
$Dom->setDocumentElement($Root); # **********
$Self->_process_element_hash($Dom, $Root, $Obj->{$Key});
}
elsif ($RefType eq 'ARRAY')
{
croak "You cant have an array of root nodes: $Key";
}
elsif (!$RefType)
{
if ($Obj->{$Key} ne '')
{
unless ($GotRoot)
{
my $Root;
eval { $Root = $Dom->createElement($Name) };
if ([email protected]) {
die "Problem creating root element $Name: [email protected]";
}
$Dom->setDocumentElement($Root); # **********
$Root->appendText($Obj->{$Key});
$GotRoot = 1;
}
}
else
{
croak "Invalid data for key: $Key";
}
}
else
{
warn "unknown reference: $RefType";
}
}
return $Dom;
}
(항상 XML : LibXML을 필요한 경우 libxml2에 설치할 수 있습니다.) – ikegami
C 코드를 설치하지 못하게합니다. ( –
나는 실수라고 생각합니다. 그리고'libxml2'가 C로 쓰여지지 않는다는 문제도 있습니다.) 홈 디렉토리에 설치하는 데 특별한 권한이 필요하지 않습니다. – ikegami