简体   繁体   中英

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>

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM