jreed21 jreed21 - 2 months ago 10
Perl Question

How to insert XML using XML::Writer perl

Im trying to make a system that can dynamically create xml based on what is entered. I have a seleton that will always be the same, and the only thing that need to be created dynamically is the formfields. So for example, I need to insert $testXML into my $writer XML. Here is what I have so far

use strict;
use warnings;
use XML::Writer;
my $test = XML::Writer->new(OUTPUT => 'self', DATA_MODE => 1, DATA_INDENT => 2, );
$test->startTag('FormField', name => 'CheckBox1');
$test->startTag('Value');
$test->characters('');
$test->endTag('Value');
$test->endTag('FormField');
my $testXML = $test->end();


Inserted into

my $writer = XML::Writer->new(OUTPUT => 'self', DATA_MODE => 1, DATA_INDENT => 2, );
$writer->startTag('Section', name => 'FormSectionOne');
$writer->startTag('FormField', name => 'Person1');
$writer->startTag('Value');
$writer->characters('Bob Test');
$writer->endTag('Value');
$writer->endTag('FormField');
$testXML;
my $xml = $writer->end();
print $xml;


Now using $writer->raw($testXML); inserts but the formatting has problems

<Section name="FormSectionOne">
<FormField name="Person1">
<Value>Bob Test</Value>
</FormField><FormField name="CheckBox1">
<Value>test</Value>




Answer
my $writer = XML::Writer->new(
   OUTPUT      => 'self',
   DATA_MODE   => 1,
   DATA_INDENT => 2,
   UNSAFE      => 1,
);

...
$writer->raw($testXML);
...