Results 1 to 4 of 4

Thread: write xml file

  1. #1
    Join Date
    Feb 2009
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default write xml file

    <?php
    function writeXML($xmlfile, $tablename)
    {
    $query= "SELECT * FROM $tablename";
    $result = mysql_query($query);

    $doc = new DOMDocument('1.0', 'UTF-8');

    $root = $doc->createElement('TableName');
    $doc->appendChild($root);

    while ($row = mysql_fetch_assoc($result)) {
    $root_child = $doc->createElement('Login');
    $root->appendChild($root_child);

    $root_attr1 = $doc->createAttribute('Id');
    $root_child->appendChild($root_attr1);

    $root_text = $doc->createTextNode($row['id']);
    $root_attr1->appendChild($root_text);

    $title = $doc->createElement( "Username" );
    $title->appendChild(
    $doc->createTextNode( $row['username'] )
    );
    $root_child->appendChild( $title );

    $publisher = $doc->createElement( "Password" );
    $publisher->appendChild(
    $doc->createTextNode( $row['pwd'] )
    );
    $root_child->appendChild( $publisher );

    $root->appendChild( $root_child );
    }
    //$myFile = "test.xml";
    $fh = fopen($xmlfile, 'w') or die("can't open file");
    fwrite($fh, $doc->saveXML());
    }
    ?>
    I have this function and i call it like this:
    include_once("connection.php");

    $link = openConnection();
    $bd = SelectBD($link);
    writeXML('sample.xml', 'userpwd');
    It have the problem when the data in the database have the character special. The problems are in Firefox it display like this � and in IE it display like this :

    The XML page cannot be displayed
    Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.

    An invalid character was found in text content. Error processing resource 'file:///C:/Program Files/Apache Group/Apache2/ht...

    <TableName><Login Id="1"><Username>titi</Username><Password>123456</Password></Login&...

    if it don't have problem it must display like this :

    <?xml version="1.0" encoding="UTF-8"?>
    <TableName><Login Id="1"><Username>titi</Username><Password>123456</Password></Login><Login Id="2"><Username>éclajdkfjd</Username><Password>jjjjjjjjjjjjjh</Password>
    </Login></TableName>

  2. #2
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    1) Make sure you database collation is UTF-8.

    2) Unless you have a good reason to use DOM, don't. To make your xml file I would do it like this. Once you have the xml file you can always use it to create an instance of DOM if you need to.

    PHP Code:
    <?php

    // You can make a function out of this if you want
    // Query Database here. Get your $result

    $xml '<?xml version="1.0" encoding="UTF-8" ?>' "\r\n";

    $xml .= '<tablename>' "\r\n";

    while (
    $row mysql_fetch_assoc($result)) {

        
    $xml .= '<login id="'$row['id'] .'">' "\r\n";
        
    $xml .= '<username>' $row['username'] . '</username>' "\r\n";
        
    $xml .= '<password>' $row['password'] . '</password>' "\r\n";
        
    $xml .= '</login>' "\r\n";

    }

    $xml .= '</tablename>';

    $filefopen('yourfile.xml''w');

    fwrite($file$xml);

    fclose($file);

    ?>

  3. The Following User Says Thank You to JasonDFR For This Useful Post:

    lay (03-10-2009)

  4. #3
    Join Date
    Feb 2009
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Thank u very much for ur helping.
    But after i try ur codes. It still have the same problem.

  5. #4
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    You still get this error?

    An invalid character was found in text content. Error processing resource 'file:///C:/Program Files/Apache Group/Apache2/ht...

    Could be a problem with the encoding type of the file that is being created. But I'm not sure.

    If you figure it out, please post the solution.

    Good luck.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •