perl LibXML to write XML document
I am very new to XML world. I have written following code to generate the XML using XML::LibXML. I understand that it seems that I am treating the namespaces as attributes which I f开发者_开发百科eel is not correct. I am not sure how to do that. So looking for a help from you to please correct 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;
From the Perldoc:
$element = $dom->createElementNS( $namespaceURI, $qname );
You need to specify the namespace when you create the element.
精彩评论