Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

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

I'm trying to use an XML based configuration file for a script and have hit a (common, I assume) problem where an element contains mixed content.

For example, a relevant snippet is:

<function name="showImage"> <data>A random picture</data> <argument><img src="test.jpg"></argument> <argument>0</argument> </function>

The <img src="..."> tag here is a simplified example of an argument which may have more than one HTML style tag included as an attribute for the argument element.

The point is that I want to be able to tell my XML parser that anything contained within the <argument></argument> element should /always/ be treated as a single attribute, because sometimes it may contain HTML tags, and sometimes it may not.

I initially tried using XML::Simple to slurp the config file in as a hash but it doesn't support mixed content, so I've moved onto XML::DOM which boasts support for this, but for which I find the documentation somewhat confusing/unclear.

If I throw the following snippet of code at the aforementioned xml file

#!/usr/bin/perl -w use strict; use XML::DOM; my $parser = new XML::DOM::Parser; my $doc = $parser->parsefile ("config.xml"); my $config = $doc->getDocumentElement; my @functions = $config->getElementsByTagName('function'); foreach my $function (@functions) { my @arguments = $function->getElementsByTagName('argument'); foreach my $argument (@arguments) { my @argumentValue = $argument->getFirstChild->getData; print "argument: @argumentValue\n"; } }

Then I end up with the error:

mismatched tag at line 3, column 36, byte 98 at /usr/lib/perl5/vendor_ +perl/5.8.0/i386-linux-thread-multi/XML/Parser.pm line 185

I assume that lots of people have at some stage wanted to include html tags inside an xml file, and not wanted their parser to try to offer it as a separate element with attributes. I might be wrong!

Any advice on what I can do would be appreciated.


In reply to Accessing mixed content in XML by anthski

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 cooling their heels in the Monastery: (7)
    As of 2021-03-07 11:53 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      My favorite kind of desktop background is:











      Results (120 votes). Check out past polls.

      Notices?