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

This question is a follow-on to a question I asked recently about parsing an XML file that does not have a DOCTYPE line specifying a dtd file, but does contain entity references. It turns out that the XML file that I was working with was defined as an entity in a sort of master XML file, and it was in that file that the dtd file was specified. The master file looks sort of like this:
<!DOCTYPE allfiles SYSTEM "allfiles.dtd" [<!ELEMENT file1 SYSTEM "file1.xml"> <!ELEMENT file2 SYSTEM "file2.xml"> ....]> <alldata> &file1; &file2; .... </alldata>
I wrote this code example from memory, so I hope I didn't add any syntax errors. I would like to use an XML parser that parses each of the nested XML files when it encounters the entity references for them. So far, I have been using XML::Parser, and it does call the ExternEnt handler function when it gets to an entity like &file1;, but it doesn't open the file, and I don't know what to do to get it to parse those nested files within the scope of the dtd file, so that entity references, etc., within those nested files are interpreted correctly. I assume I can't invoke the parser recursively from within the ExternEnt handler function, and the parser settings that I played around with to try to get different behavior (NoExpand, ParseParamEnt) did not seem to make a difference in how these entity references were dealt with. Does anyone have any suggestions?

Replies are listed 'Best First'.
Re: Parsing nested XML files
by iburrell (Chaplain) on Jun 23, 2004 at 16:11 UTC
    I suggest you find whoever wrote that XML file with entities, and hit them over the head. It makes processing them much more difficult.

    It looks like XML::Parser needs the ExternEnt handler to either return te content or an open file handle. I guess your code needs to handle finding and opening the file. The default handler is supposed to fetch the file or URL.