how to create a valid xml file with php and mysql

Discussion in 'XML' started by Kentor, May 29, 2006.

  1. Kentor

    Kentor Guest

    I have some info that im grabbing from a mysql database with php and i
    want to make an xml file but i know that i need to be carefull with the
    & sign and so on... how can i clean the data coming from the db to have
    a valid xml file?
     
    Kentor, May 29, 2006
    #1
    1. Advertising

  2. Kentor wrote:

    > I have some info that im grabbing from a mysql database with php and i
    > want to make an xml file but i know that i need to be carefull with the
    > & sign and so on... how can i clean the data coming from the db to have
    > a valid xml file?


    The latest 5.1.something PHP release has XmlWriter to generate XML:
    <www.php.net/XMLWriter>
    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, May 29, 2006
    #2
    1. Advertising

  3. Kentor

    Kentor Guest

    thanx... it's pretty complex... =/
     
    Kentor, May 29, 2006
    #3
  4. Kentor

    Kentor Guest

    anything else =/
     
    Kentor, May 30, 2006
    #4
  5. Kentor wrote:
    > anything else =/
    >


    I think you should go with the XMLWriter

    However

    I guess, if you're just putting some character data in semi-fixed tags
    (rather than trying to generate the document structure from the db) you
    could maybe escape the character data with something like this:


    $original = "this&that ]]><foo";
    $pattern = array("&", "<", "]]>");
    $replacement = array("&amp;", "&lt;", "]]&gt;");
    $escaped = preg_replace($pattern, $replacement, $original);

    I didn't test this, and whether this works is up to the implementation
    of preg_replace()...

    Note if you're planning to put strings into attribute values the ' and "
    characters should be escaped as well. (With &apos; and &quot; respectively.)

    There is also a caveat; the XML 1.0 spec doesn't allow for some
    "special" characters and therefore it is possible that the document is
    still not well-formed even if the code does work as I intended. If
    you're afraid of encountering characters not allowed to occur in XML 1.0
    you should declare the XML version to be 1.1, which allows for almost
    all unicode characters. That means you put the string

    <?xml version="1.1"?>

    first in the document.

    --

    - Tuomas
     
    Tuomas Rannikko, May 30, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JL
    Replies:
    0
    Views:
    1,178
  2. James
    Replies:
    0
    Views:
    432
    James
    May 13, 2004
  3. Rajive Narain
    Replies:
    0
    Views:
    2,091
    Rajive Narain
    Sep 18, 2009
  4. neha shena
    Replies:
    1
    Views:
    282
    Dennis Lee Bieber
    Oct 29, 2010
  5. julian
    Replies:
    8
    Views:
    517
    Avatar
    Apr 6, 2006
Loading...

Share This Page