Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

You haven't given us much to work with, so hopefully the following examples are enough to get you started:

Here's a sample XML file which I've called 'greetings.xml' (my 'Chinese' characters came via babelfish so they probably don't make sense):

<?xml version='1.0' encoding='utf-8'?>
<doc>
  <para lang="en">Hello World</para>
  <para lang="zh">你好世界</para>
</doc>

Here's a short CGI script which reads the file using XML::Simple and outputs some of the data in an HTML page:

#!/usr/bin/perl use strict; use warnings; use XML::Simple qw(:strict); my $filename = 'greetings.xml'; # full path probably required my $xs = XML::Simple->new( ForceArray => [ 'para' ], KeyAttr => { para => 'lang' }, ); my $doc = $xs->xml_in($filename); binmode STDOUT, ':utf8'; print <<"EOF"; Content-type: text/html; charset=utf-8 <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" / +> <title>Test Page</title> </head> <body> <h1>$doc->{para}->{zh}->{content}</h1> </body> </html> EOF

Of course your script does not need to be a CGI script and does not need to generate HTML. I chose to do it this way so that the output could be viewed in a web browser. If you just run the script from the command-line, you may not be able to read all the characters in your terminal window - it depends if your terminal is set up to handle UTF-8 data.

The call to binmode(STDOUT, ':utf8') sets the output filehandle to UTF-8. This will suppress warnings about "Wide character in print". You might want to select an alternative output encoding.

Here's an example of writing XML:

#!/usr/bin/perl use strict; use warnings; use XML::Simple qw(:strict); use Data::Dumper; my $filename = 'greeting.xml'; my $xs = XML::Simple->new( ForceArray => [ 'para' ], KeyAttr => { para => 'lang' }, RootName => 'doc', ); my $data = { 'para' => { 'en' => { content => 'Hello World' }, 'zh' => { content => "\x{4f60}\x{597d}\x{4e16}\x{754c}" }, }, }; binmode STDOUT, ':utf8'; my $decl = "<?xml version='1.0' encoding='utf-8' standalone='yes'?>"; print $xs->xml_out($data, XMLDecl => $decl);

In this example, I used the \x{abcd} format to specify Unicode characters. You could type the Chinese characters in directly if your editor supports UTF-8 and you include use utf8; at the top of your script.

use utf8;

my $data = {
    'para' => {
        'en' => { content => 'Hello World' },
        'zh' => { content => "你好世界" },
    },
};

In reply to Re: How to parse Chinese Characters using XML-Simple? by grantm
in thread How to parse Chinese Characters using XML-Simple? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (5)
    As of 2020-07-06 17:27 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?