#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    2
    Rep Power
    0

    Exclamation "XML document must have a top level element. Error processing resource "


    Hello,

    I am seeking your urgent attention in debugging the above mentioned error message that I have been receiving. I need my code to be working today.

    For the past four days I have been trying to get it to work.

    It goes as follows:


    <?php
    if (!$db = mysql_connect('localhost','root')) {
    die("Connection failed to the host 'localhost'");
    exit;
    }


    if (!mysql_select_db('zioncd',$db)) {
    die("Cannot connect to database 'Zion Cd'");
    exit;
    }


    $cd = 'cd';
    $query = "SELECT orderlev FROM $cd WHERE orderlev = 50";
    $dbresult = mysql_query($query, $db);

    // creates an xml document version 1.0; This returns the object reference.
    $file = domxml_new_doc("1.0");

    // create new element
    $cd = $file->create_element('cd');
    $cd = $file->append_child($cd);

    // process each row from the sql query
    while ($row = mysql_fetch_assoc($dbresult)) {
    // creates a subnode for each row
    $node = $file->create_element($cd); // $cd is the table name
    $node = $zioncd->append_child($occ);

    // add a child node for each field
    foreach ($row as $fieldname => $fieldvalue) {
    $childrow = $file->create_element($fieldname);
    $childrow = $file->append_child($child);

    $value = $file->create_text_node($fieldvalue);
    $value = $child->append_child($value);
    } // foreach
    } // while

    // get completed xml document

    $xml_string = $doc->dump_mem(true);
    echo $xml_string;
    ?>

    Regards,

    zion
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Germany
    Posts
    14
    Rep Power
    0
    Sorry for responding to your problem too long. But, I guess it wouldn't hurt to give you some help, right? This is just a rough guess, I think the problem is with your XML file. XML files should be like:

    Code:
    <product>
    <pcgame>SC</pcgame>
    <programming>VB</programming>
    ...etc
    </product>
    But, it souldn't be like:

    Code:
    <pcgame>SC</pcgame>
    <programming>VB</programming>
    ...etc
    So, I think you only miss the high most element, in this case is <product></product>.

    Does it help?
    Last edited by wendra; May 10th, 2004 at 02:07 AM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    2
    Rep Power
    0

    xml must have a top level element


    This xml is mixed with php. It is supposed to automate pulling records from the database where the stock level is below the order level.

    Thanks anyway for trying.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Germany
    Posts
    14
    Rep Power
    0
    You're welcome. But I don't know any PHP, so I am really sorry, I can not help you more than that. Can somebody help with his problem?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    3
    Rep Power
    0
    I think you're both right... whether it's PHP creating XML or straight XML, you still need a top-level element...

    So, before you echo out your $xml_string, just wrap it with some dummy tags:

    <?php
    echo "<doc>\n";
    echo $xml_string;
    echo "</doc>\n";
    ?>

    Does that fix the problem?

    (I'm fairly new to XML, with a few years of PHP geekdom under my belt. I've seen that error before... IE6.0 throws that when you display a .xml file that doesn't contain a single top-level element, eg:

    <foo>
    <bar> ... </bar>
    <bar> ... ... </bar>
    </foo>
    <foo>
    <bar> ... </bar>
    <bar> ... ... </bar>
    </foo>

    instead of

    <doc>
    <foo>
    <bar> ... </bar>
    <bar> ... ... </bar>
    </foo>
    <foo>
    <bar> ... </bar>
    <bar> ... ... </bar>
    </foo>
    </doc>
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    1
    Rep Power
    0
    Just thought I would stop by after googling for a fix to this error.

    Your dummy <doc></doc> tags works a treat - thanks very much for the help (even though it was not intended for me!).


IMN logo majestic logo threadwatch logo seochat tools logo