Re: XML::LibXML: How to tell parse_balanced_chunk what DTD to use?

Reuben Thomas <[email protected]> Sun, 19 Sep 2010 20:44:44 +0100
Newsgroups gmane.comp.lang.perl.xml
Message-ID <[email protected]>
On 17 September 2010 12:10, Robin Berjon <[email protected]> wrote:
> On Sep 17, 2010, at 12:51 , Reuben Thomas wrote:
>> On 17 September 2010 11:13, Robin Berjon <[email protected]> wrote:
>>> None that I know of, but there could be some way I haven't thought of. You might think it strange, but parse_balanced_chunk isn't a typical XML parser functionality in the first place so I wouldn't be surprised if it weren't all that generic.
>>
>> Sure, but the underlying libxml2 API would seem to support this
>> functionality; at least it has the information it needs:
>>
>> int   xmlParseBalancedChunkMemory     (xmlDocPtr doc,
>>                                        xmlSAXHandlerPtr sax,
>>                                        void * user_data,
>>                                        int depth,
>>                                        const xmlChar * string,
>>                                        xmlNodePtr * lst)
>>
>> In particular, it takes a doc, whereas parse_balanced_chunk is a
>> parser method, not a document method.
>
> Well, if libxml2 supports it, and since you've just shown that you know way more C than I ever will, I might be tempted to facetiously suggest writing a patch ;)

I've had a look. It seems that the relevant C is already pretty much
written: in dom.c, the routine domReadWellBalancedString does what is
needed, but is only ever called with a NULL doc argument (from
_parse_xml_chunk in LibXML.xs).

The reason I'm going no further is that I know nothing about either
Perl XS programming or libxml2, but it looks as though what is needed
is:

1. Make parse_balanced_chunk a method of XML::LibXML::Document as well
as of XML::LibXML (in LibXML.pm).

2. In _parse_xml_chunk, detect when it has been called as a method of
XML::LibXML::Document, and pass the self argument as the first
argument to domReadWellBalancedString, rather than NULL.

This should only be a few lines of code. I played with it for some
time, but I'm no XML, libxml2 or Perl expert, so in the end I gave up,
though I think I got quite close. The main problem is that
XML::LibXML::Document inherits from XML::LibXML::Node, which does not
have anything to do with XML::LibXML(::Parser), so you have to kludge
the document argument in to _parse_xml_chunk. After that I was getting
crashes deep inside libxml2 while parsing entities (I have no idea).

Since I can use the suggested workaround, I'm going to go for that.

-- 
http://rrt.sc3d.org
_______________________________________________
Perl-XML mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs