Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

CDATA probelm in XML::SMart

by KarthikK (Sexton)
on Nov 21, 2007 at 17:06 UTC ( [id://652174]=perlquestion: print w/replies, xml ) Need Help??

KarthikK has asked for the wisdom of the Perl Monks concerning the following question:

hello monks,
I have a probelm with XML::SMart parser.
This is my XML structure.
<ISSUES> <ISSUE> <ISSUE-DESC> <P nd="description"></P> <P nd="category"></P> <P nd="name"></P> <P nd="level"></P> <P nd="occurance"></P> </ISSUE-DESC> </ISSUE> </ISSUES>

Below is my code
#!/usr/bin/perl -w use strict; use XML::SMART; my $strTestString =' J&K; combine'; my $XML = XML::Smart->new(q`<?xml version="1.0" encoding="UTF-8" ?><IS +SUES><ISSUE><ISSUE-DESC><P></P></ISSUE-DESC></ISSUE></ISSUES>`, 'XML: +:Smart::Parser'); my $xmlfile = "C:/perl/xml/test.xml"; my $para={SI => 'description'}; $XML->{ISSUES}{ISSUE}{'ISSUE-DESC'}{P}[0]=$para; $XML->{ISSUES}{ISSUE}{'ISSUE-DESC'}{P}[0]->set_binary(0); $XML->{ISSUES}{ISSUE}{'ISSUE-DESC'}{P}[0]->set_cdata(1); $XML->{ISSUES}{ISSUE}{'ISSUE-DESC'}{P}[0]->content(0,"$strTestString") +; $XML->{ISSUES}{ISSUE}{'ISSUE-DESC'}{P}[1]->set_cdata(0); $XML->{ISSUES}{ISSUE}{'ISSUE-DESC'}{P}[1] = "test"; XML->save($xmlfile, nometagen => 1, forceutf8 => 1);

I have the following problems

1. When there is a text which contains the & and ; together with out any space this & is not parsed as & instead it throws error. This is the reason why i asked for a regex to add a space before ; in the string. http://perlmonks.org/?node_id=652084

2. I would be able to overcome this if the parser sets the data as CDATA. This i do it by set_cdata(1) but this is not considered atall.
So my

node always contains text and not CDATA.
Also there are some binary data in my text. this parser parse it as binary and puts it in an encoded way. But i dont want this behaviour instead i wanted them in CDATA. So i also set set_binary(0) but this is also ignored

I want CDATA only for my <P nd="description"></P> node and not any other <P>NODES. Is this possible?
I use XML::Smart v 1.6.9 and PERL 5.6.1 (this is the perl which is bundled with IBM Rational Clearcase)
Also to mention my XML encoding is UTF-8
Any help would be really appreciated

Best Regards
Karthik

Replies are listed 'Best First'.
Re: CDATA probelm in XML::SMart
by ikegami (Patriarch) on Nov 21, 2007 at 18:40 UTC

    When there is a text which contains the & and ; together with out any space this & is not parsed as & instead it throws error.

    It sounds like content expects XML, but what you are passing isn't valid XML. A simple way to convert plain text to XML is

    for ($text) { s/&/&amp;/g; s/'/&apos;/g; s/"/&quot;/g; s/</&lt;/g; s/>/&gt;/g; }

    However, I'd recommend avoiding content completely. The whole point of the module is to provide much better means of accessing and changing data than low-level functions like content.

    Also there are some binary data in my text.

    That may be a problem.
    XML/1.0 doesn't allow many characters.
    XML/1.1 doesn't allow the NUL character (chr(0)).
    You might have to alter your scheme to allow the binary bits to be base64-encoded.

    use XML::SMART;

    That should be use XML::Smart;. Case *is* important, even if what you have *appears* to work.

      In content() i am passing the data. does content requires XML? The problem now is the XML::Smart does the encoding. but i dont want to do this but pass the data in CDATA node.
        Why? CDATA is actually worse than entity-encoding, and they're completely equivalent to XML parsers.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://652174]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-04-25 08:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found