in reply to Seeking for advice: XML parsing with special requirements [Solved]

I'm not sure, but it sounds like you might have invalid XML which means a "real" XML parser isn't going to work.

But, in any case, since you've already spent a ton of time fighting against a variety of full-fledged parsers, I'd just write my own parser. It actually is quite easy to write a real parser for whatever subset of XML one has to deal with. And that makes it trivial to deal with unusual things (that might not even strictly be valid XML) and trivial to get access to whatever matters to you.

Re^2: parsing XML fragments (xml log files) with... a regex shows how easy it was for me to deal with the types of XML I ran into. And the code is trivial to extend to cover more parts of XML to meet your needs.

Note that, as written, my code expects the full XML string. But it would be easy to modify it to just read a reasonably large chunk of text and, when pos gets 1/2 way through (or when an unclosed < is encountered), to just strip what has been parsed so far and append more.

- tye        

  • Comment on Re: Seeking for advice: XML parsing with special requirements (regex)

Replies are listed 'Best First'.
Re^2: Seeking for advice: XML parsing with special requirements (regex)
by Nocturnus (Scribe) on Apr 23, 2012 at 18:13 UTC

    Thank you very much for your reply and helpful comments.

    You are right in that my XML might be invalid in the rigid sense of the specification. Nevertheless, it's well-balanced and does not have any problems except the ones which are related to the entities.

    Regarding writing an own parser: I have thought about that since some time ago I already have written some simple parsers for other tasks. But after having looked into the XML specification, I have come to the conclusion that it is not possible to write a full XML parser (excluding entity resolving) in reasonable time.

    In fact, to achieve what I need, I have to use a full XML parser with all bells and whistles: Think of encoding, namespaces, the various sorts of declarations (attribute, element, ...), CDATA, PIs, and so on.

    Probably it would be easier to modify one of the existing reliable parsers.

    Thanks again,

    Nocturnus