How do I make & a literal character in an XML parse?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ivan Marsh

    How do I make & a literal character in an XML parse?

    Hey Folks,

    ....and here I thought I was done with my AJAX application.

    The XML my PHP app is returning to my JavaScript function has elements in
    it that contain special characters... specifically the ampersand & which
    hoses up the data. ex: Company Name = "K & B Construction".

    Can anyone give me some idea of how I make the data coming out of my
    database translate as literal characters in the XML output?

    -thx

    --
    The USA Patriot Act is the most unpatriotic act in American history.
    Feingold-Obama '08 - Because the Constitution isn't history,
    It's the law.

  • Janwillem Borleffs

    #2
    Re: How do I make & a literal character in an XML parse?

    Ivan Marsh wrote:[color=blue]
    > Can anyone give me some idea of how I make the data coming out of my
    > database translate as literal characters in the XML output?
    >[/color]

    Simply by applying the htmlentities or htmlspecialchar s function.


    JW


    Comment

    • Bart Van der Donck

      #3
      Re: How do I make & a literal character in an XML parse?

      Ivan Marsh wrote:
      [color=blue]
      > The XML my PHP app is returning to my JavaScript function has elements in
      > it that contain special characters... specifically the ampersand & which
      > hoses up the data. ex: Company Name = "K & B Construction".
      >
      > Can anyone give me some idea of how I make the data coming out of my
      > database translate as literal characters in the XML output?[/color]

      I think you should deal with this at the PHP side because '&' is
      reserved for the declaration of entities. The best way is to encode
      your special chars to html entities before adding them to the XML file.
      I'ld say that PHP should have ready-to-go regexes for this kind of
      things.

      If necessary, you could add !ENTITY !DOCTYPE !ELEMENT headers etc, but
      don't rely too much on parser-specific implementations . You'll do
      yourself a big favour with that :-)

      --
      Bart

      Comment

      • Rik

        #4
        Re: How do I make & a literal character in an XML parse?

        Bart Van der Donck wrote:[color=blue]
        > Ivan Marsh wrote:
        >[color=green]
        >> The XML my PHP app is returning to my JavaScript function has
        >> elements in it that contain special characters... specifically the
        >> ampersand & which hoses up the data. ex: Company Name = "K & B
        >> Construction".
        >>
        >> Can anyone give me some idea of how I make the data coming out of my
        >> database translate as literal characters in the XML output?[/color]
        >
        > I think you should deal with this at the PHP side because '&' is
        > reserved for the declaration of entities. The best way is to encode
        > your special chars to html entities before adding them to the XML
        > file. I'ld say that PHP should have ready-to-go regexes for this kind
        > of
        > things.[/color]

        There is, htmlentities().
        Unfortunately, it doesn't recognize every character, and coverts the
        ampersand in already converted entitities. In the comments, someone claims
        to have written a better function for it:


        I haven't tried the code yet, maybe it's usefull here.

        Grtz,
        --
        Rik Wasmus


        Comment

        • R. Rajesh Jeba Anbiah

          #5
          Re: How do I make & a literal character in an XML parse?

          Ivan Marsh wrote:
          <snip>[color=blue]
          > The XML my PHP app is returning to my JavaScript function has elements in
          > it that contain special characters... specifically the ampersand & which
          > hoses up the data. ex: Company Name = "K & B Construction".[/color]
          <snip>

          Not sure, if you need CDATA.

          --
          <?php echo 'Just another PHP saint'; ?>
          Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

          Comment

          • Ivan Marsh

            #6
            Re: How do I make &amp; a literal character in an XML parse?

            On Sun, 25 Jun 2006 11:22:57 -0700, R. Rajesh Jeba Anbiah wrote:
            [color=blue]
            > Ivan Marsh wrote:
            > <snip>[color=green]
            >> The XML my PHP app is returning to my JavaScript function has elements
            >> in it that contain special characters... specifically the ampersand &
            >> which hoses up the data. ex: Company Name = "K & B Construction".[/color]
            > <snip>
            >
            > Not sure, if you need CDATA.[/color]

            I just parsed them into &amp; and it works fine. Thanks.

            --
            The USA Patriot Act is the most unpatriotic act in American history.
            Feingold-Obama '08 - Because the Constitution isn't history,
            It's the law.

            Comment

            Working...