-2

我曾经在我的网站上有一个很好的工作 XML 文件(谷歌地图的来源)。直到几天前才得到这个错误:

此页面包含以下错误:第 2 行第 10896 列的错误:EntityRef:期待 ';' 下面是第一个错误之前的页面渲染。

在那个时间段内,我没有对 php 文件进行更改。还做了一些数据库检查,一切似乎都井井有条(数据库连接工作正常)。

我读到一个常见的原因是使用&而不是&。但是在其中parseToXML有一个替换功能,所以这不是我想的原因。

有小费吗?

谢谢!

标记

require("credentials.php");

function parseToXML($htmlStr)
{
    $xmlStr=str_replace('<','&lt;',$htmlStr);
    $xmlStr=str_replace('>','&gt;',$xmlStr);
    $xmlStr=str_replace('"','&quot;',$xmlStr);
    $xmlStr=str_replace("'",'&#39;',$xmlStr);
    $xmlStr=str_replace("&",'&amp;',$xmlStr);
    return $xmlStr;
}

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT id, name, address, lat, lng, descrip FROM people";
$result = $conn->query($sql);

header("Content-type: text/xml");

// Start XML file, echo parent node
echo '<markers>';
$ind=0;
// Iterate through the rows, printing XML nodes for each
 while($row = $result->fetch_assoc()) {
     // Add to XML document node
     echo '<marker ';
     echo 'id="' . $row['id'] . '" ';
     echo 'name="' . parseToXML($row['name']) . '" ';
     echo 'address="' . parseToXML($row['address']) . '" ';
     echo 'lat="' . $row['lat'] . '" ';
     echo 'lng="' . $row['lng'] . '" ';
     echo 'descrip="' . $row['descrip'] . '" ';
     echo '/>';
     $ind = $ind + 1;
 }

// End XML file
echo '</markers>';
4

2 回答 2

0

@CBroe @Kevin,哇,你发现了这一点。确实在最近几天发现,在数据库中创建了一个条目。那天,一位用户在该descrip字段中添加了一个包含 & 符号的文本。这杀死了 XML 文件输出。我现在也申请parseToXML了那条descrip线并解决了问题。

@Nigel Ren 和所有人都同意我现在使用的有点不稳定。开放和诚实,将我的整个网站设置描述为“古怪”:-)。不过会考虑您的建议,以进行进一步的改进。现在,非常感谢大家!

于 2020-07-15T07:54:56.227 回答
0

与手动写出字符串相比,使用为这项工作设计的各种工具生成 XML 更容易、更可靠。PHP 具有DOMDocument使此类任务非常简单的类,并且也需要更少的代码行;-/ 如果文本内容包含 V 形或其他字符,它还具有不卡顿的好处

<?php
    require 'credentials.php';
    $conn = new mysqli( $servername, $username, $password, $dbname );

    $sql = 'select id, name, address, lat, lng, descrip from people';
    $result = $conn->query( $sql );
    
    
    # generate new DOMDocument & set properties
    $dom = new DOMDocument;
    $dom->formatOutput = true;
    $dom->xmlStandalone = true;
    
    # create the root node and add to the DOM
    $root=$dom->createElement('markers');
    $dom->appendChild( $root ); 
    
    # for convenience it is quicker to iterate through this array to generate attributes
    # thse correspond to the column names / aliases from the sql query
    $attribs=[ 'id', 'name', 'address', 'lat', 'lng', 'descrip' ];
    
    # iterate through recordset, generate new node for each record
    while( $row = $result->fetch_object() ) {
        #add the new node to DOM
        $marker=$dom->createElement('marker');
        $root->appendChild( $marker );
        
        #add attributes
        foreach( $attribs as $attr ){
            $marker->setAttribute( $attr, $row->$attr );
        }
    }
    
    #save or echo?
    header('Content-Type: text/xml');
    echo $dom->saveXML();
    
    #$dom->save('/path/to/file.xml');
?>
于 2020-07-15T07:44:26.400 回答