http://qs1969.pair.com?node_id=482456

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

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.