+ Reply to Thread
Results 1 to 2 of 2

Thread: ERROR with XML

  1. #1

    ERROR with XML

    I am using mysql_real_escape_string function while inserting the data into MySQL database.
    Error :
    Code:
    "End tag 'test_field' does not match the start tag 'End'. Error processing resource"
    while generating the XML , Here is my PHP code :
    Code:
    $result = mysql_query("SELECT * from table1");
    
    header("Content-Type: application/xml");
    echo sqlToXml($result, "SalesOrder", "sales");
    
    function sqlToXml($queryResult, $rootElementName, $childElementName)
    {
    
    $xmlData = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n"; 
    $xmlData .= "<" . $rootElementName . ">";
    
    while($record = mysql_fetch_object($queryResult))
    { 
        $xmlData .= "<" . $childElementName . ">";
        for ($i = 0; $i < mysql_num_fields($queryResult); $i++)
        { 
            $fieldName = mysql_field_name($queryResult, $i); 
            $xmlData .= "<" . $fieldName . ">";
            if(!empty($record->$fieldName))
                $xmlData .= $record->$fieldName; 
            else
                $xmlData .= "null"; 
    
            $xmlData .= "</" . $fieldName . ">"; 
        } 
    
            $detail = mysql_query("SELECT * from table2 WHERE salesordernumber = '".$record->SalesOrderNumber."'");
    while ($row = mysql_fetch_object($detail)) { 
       $xmlData .= "<Details>";
       $xmlData .= "<SalesOrderDetailID>" . $row->SalesOrderDetailID . "</SalesOrderDetailID>";
       $xmlData .= "<Name>" . $row->Name . "</Name>";
       $xmlData .= "<CarrierTrackingNumber>" . $row->CarrierTrackingNumber . "</CarrierTrackingNumber>";
       $xmlData .= "</Details>";
    }
    
    
    
        $xmlData .= "</" . $childElementName . ">"; 
    } 
    $xmlData .= "</" . $rootElementName . ">"; 
    }

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    45
    I'd suggest using PHPs XMLWriter for generating the XML. You can easily generate valid XML-files with it and imo it is much more readable:
    Code:
    function sqlToXml($queryResult, $rootElementName, $childElementName) {
        $w = new XMLWriter();
        $w->openMemory();
        $w->setIndent(true);
        $w->setIndentString("   ");
        $w->startDocument("1.0", 'ISO-8859-1');
        $w->startElement($rootElementName);
    
        while($record = mysql_fetch_object($queryResult)) {
            $w->startElement($childElementName);
            for ($i = 0; $i < mysql_num_fields($queryResult); $i++) { 
                $fieldName = mysql_field_name($queryResult, $i); 
                $w->startElement($fieldName);
                if(!empty($record->$fieldName)) {
                    $w->writeCData($record->$fieldName);
                }
                else {
                    $w->writeCData("null");
                }
                $w->endElement();
            } 
            $w->endElement();
        } 
        $w->endElement();
        $w->endDocument();
        return $w->outputMemory();
    }

+ Reply to Thread

Tags for this Thread

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