Perl LibXML to write an XML document

I am very new to the XML world. I wrote the following code to generate XML using XML :: LibXML. I understand that it seems that I consider namespaces as attributes that I feel are incorrect. I am not sure how to do this. Therefore, seeking help from you to fix my code.

XML document:

<RootDocument protocol="OCI" xmlns="C" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <sessionId xmlns="">172.24.160.8,31436753,1298637565495</sessionId>
    <command xsi:type="AuthenticationRequest" xmlns="">
    <userId>automation</userId>
    </command>
</RootDocument>

Perl Script:

my $ociRequest = XML::LibXML::Document->new( '1.0', 'utf-8' );

my $root = $ociRequest->createElement ('RootDocument');

$root->addChild ($ociRequest->createAttribute ( protocol => 'OCI' ) );
$root->addChild ($ociRequest->createAttribute ( xmlns => 'C' ) );

$root->addChild ($ociRequest->createAttribute ( 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance' ) );
#$root->setNamespace ('http://www.w3.org/2001/XMLSchema-instance', 'xsi', 0);
$ociRequest->setDocumentElement ($root);


my $session = $ociRequest->createElement ('sessionId');
$session->addChild ($ociRequest->createAttribute ( xmlns => '') );
$session->addChild($ociRequest->createTextNode($sessionID));
$root->addChild($session);

my $command = $ociRequest->createElement ('command');
$command->addChild ($ociRequest->createAttribute ( 'xsi:type' => 'AuthenticationRequest' ) );
$command->addChild ($ociRequest->createAttribute ( 'xmlns' => '' ) );

    my $userid = $ociRequest->createElement ('userId');
    $userid->addChild($ociRequest->createTextNode('automation'));
$command->addChild($userid);

$root->addChild ($command);


$ociRequest->setDocumentElement($root);

my $xml = $ociRequest->toString;
+3
source share
1 answer

From Perldoc:

$element = $dom->createElementNS( $namespaceURI, $qname );

You need to specify the namespace when creating the item.

+5
source

Source: https://habr.com/ru/post/1794734/


All Articles