logo       

Re: Patch for XML::Writer: msg#00073

lang.perl.xml

Subject: Re: Patch for XML::Writer

Oops! I sent a wrong patch.

Sending it again.

-Thanks

--- srinithan <srinithan@xxxxxxxxxxx> wrote: > Hi Ed,
>
> As of now, we cannot add a CDATA ( character data )
> to an XML document using XML::Writer.
>
> I patched it in the following way and attached the
> same with this mail.
>
> diff -c -b -r old_xml_writer/ new_xml_writer/ >
> xml_writer.patch
>
> we can add a cdata in the following ways:
> ----------------------------------------
> Ex 1:
> $writer->startTag("greeting");
> # Insert CDATA "Hello, world"
> $writer->cData("Hello, world");
> $writer->endTag("greeting");
>
> Ex2:
> $writer->cdataElement("greeting", "Hello world");
>
> Thanks
> Srinivas
>
>

________________________________________________________________________
Yahoo! India Mobile: Download the latest polyphonic ringtones.
Go to http://in.mobile.yahoo.comdiff -c -b -r old_xml_writer/Writer.pm new_xml_writer/Writer.pm
*** old_xml_writer/Writer.pm Tue Dec 23 01:31:40 2003
--- new_xml_writer/Writer.pm Tue Dec 23 00:50:40 2003
***************
*** 322,327 ****
--- 322,344 ----
}
};

+ my $cData = sub {
+ my $data = $_[0];
+ $data =~ s/\]\]>/\]\]&gt\;/g;
+ $output->print("<![CDATA[$data]]>");
+ $hasData = 1;
+ };
+
+ my $SAFE_cData = sub {
+ if ($elementLevel < 1) {
+ croak("Attempt to insert characters outside of document element");
+ } elsif ($dataMode && $hasElement) {
+ croak("Mixed content not allowed in data mode: characters");
+ } else {
+ &{$cData};
+ }
+ };
+

# Assign the correct closures based on
# the UNSAFE parameter
***************
*** 334,340 ****
'STARTTAG' => $startTag,
'EMPTYTAG' => $emptyTag,
'ENDTAG' => $endTag,
! 'CHARACTERS' => $characters};
} else {
$self = {'END' => $SAFE_end,
'XMLDECL' => $SAFE_xmlDecl,
--- 351,358 ----
'STARTTAG' => $startTag,
'EMPTYTAG' => $emptyTag,
'ENDTAG' => $endTag,
! 'CHARACTERS' => $characters,
! 'CDATA' => $cData};
} else {
$self = {'END' => $SAFE_end,
'XMLDECL' => $SAFE_xmlDecl,
***************
*** 344,350 ****
'STARTTAG' => $SAFE_startTag,
'EMPTYTAG' => $SAFE_emptyTag,
'ENDTAG' => $SAFE_endTag,
! 'CHARACTERS' => $SAFE_characters};
}

# Query methods
--- 362,369 ----
'STARTTAG' => $SAFE_startTag,
'EMPTYTAG' => $SAFE_emptyTag,
'ENDTAG' => $SAFE_endTag,
! 'CHARACTERS' => $SAFE_characters,
! 'CDATA' => $SAFE_cData};
}

# Query methods
***************
*** 492,497 ****
--- 511,526 ----
}

#
+ # Write a simple CDATA element.
+ #
+ sub cdataElement {
+ my ($self, $name, $data, %atts) = (@_);
+ $self->startTag($name, %atts);
+ $self->cData($data);
+ $self->endTag($name);
+ }
+
+ #
# Write character data.
#
sub characters {
***************
*** 500,505 ****
--- 529,542 ----
}

#
+ # Write CDATA.
+ #
+ sub cData {
+ my $self = shift;
+ &{$self->{CDATA}};
+ }
+
+ #
# Query the current element.
#
sub in_element {
diff -c -b -r old_xml_writer/test.pl new_xml_writer/test.pl
*** old_xml_writer/test.pl Tue Dec 23 01:31:40 2003
--- new_xml_writer/test.pl Tue Dec 23 00:52:44 2003
***************
*** 550,555 ****
--- 550,564 ----
});
};

+ # Test 44: inserting cdata ( character data )
+ TEST: {
+ $writer->startTag("foo");
+ $writer->cData("cdata testing - test44");
+ $writer->endTag("foo");
+ $writer->end();
+ checkResult(44, "<foo><![CDATA[cdata testing - test44]]></foo>\n");
+ };
+
1;

__END__
<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise