in reply to Escaping XML Reserved characters

Wait, could you clarify please?   Are you saying you _already_ have XML files with text like you showed above?   Or are you generating the XML files?

If you're generating the XML files then what samtregar gave you should be enough.   If you really want to escape ' " ' you can add " to his code.

Replies are listed 'Best First'.
Re: Re: Escaping XML Reserved characters
by skinnymofo (Scribe) on Sep 14, 2003 at 15:26 UTC
    I already have the XML. That's why samtregar's suggestion isn't ideal.
    Also, as we've seen, escaping the & and the quotes isn't a big deal, it's the < and > that I'm having trouble with.
    A regex like s/>/&gt;/g; would eliminate all of the >, which isn't a good thing.

      You may have more of a dilemma than you realize here: You're saying (I think) that you have existing XML that's non-conforming because it hasn't already used entities for the 3 required characters in the data: <, >, and &. < and > are difficult because you (obviously) want to leave them in the tags. That would mean to get it right, you'd have to parse the XML. However most parsers will, I think, get confused by the unescaped < and > characters. i.e., most parsers expect conforming XML. At any rate, if you can't simply regenerate the XML and do the entity conversion, I think you have to start by trying to parse it. Something like XML::Parser would be where I'd start, but I just tried feeding it some XML without escaped < and > characters and it flagged those as errors. Maybe there are settings that let you get around that.

      For entity conversion (to or from) I like HTML::Entities. XML::Writer is nice in other respects, but it's entity conversion could be better. It only does the three above, it always does them named, and it's not smart enough to realize when there are & characters introducing entities that are already in the data.

        You're saying (I think) that you have existing XML that's non-conforming because it hasn't already used entities for the 3 required characters in the data...

          Yes! That's exactly my problem. The creation of the XML is not under my control, which drops this problem in my lap.
          As a sidenote, I should probably tell the source system that they shouldn't call this XML.

          Thanks for your replies. I was hoping to do a simple, fast script that would rip through the data (~150MB) and escape out the reserved chars.
          I'm going to look at your suggestion of parsing the data. Even though it might take longer, and/or more overhead, it is a good way of insuring I'm doing the right thing.
        Thanks!