【问题标题】:<markers></markers> at the top of the XLM file, instead of </markers> at bottom<markers></markers> 在 XML 文件的顶部,而不是在底部的 </markers>
【发布时间】:2020-11-05 16:11:57
【问题描述】:

早上好!我的“声誉”只有“1”,但我希望有人能帮助我解决这个过度烦人的错误 (ENE)。但是,如果我能弄清楚如何在此处正确格式化代码,那该死的。我想这个问题对每个人来说都是显而易见的,但我。谢谢。

首先,XLM 结果:

<?xml version="1.0" encoding="utf-8"?>
<markers></markers>
<marker site_name="Mineral Point Mine" mrds_id="D010915"</marker>

然后是代码(如果我能管理 irt):

<?php
require("db.php");

$query = $_GET['query']; 

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;
}

$doc = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true;
$doc->preserveWhiteSpace = false;
$snode = $doc->createElement('markers', '');
$doc->appendChild($snode);

$link = mysqli_connect("localhost", $username, $password, $database);

if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}

$query = "{HIDDEN}";

$result = $link->query($query) or die($link->error);

while ($row = $result->fetch_assoc()) {

$node = $doc->createElement("marker","");
$newnode = $doc->appendChild($node);
$newnode->setAttribute("site_name", parseToXML($row['site_name']));
$newnode->setAttribute("mrds_id", parseToXML($row['mrds_id']));
}
$result->close();
echo $doc->saveXML();
?>

【问题讨论】:

  • 您需要将标记元素附加到$snode,因为这是markers 元素。
  • 谢谢。我以为它已经被附加了。
  • 据我所知,我已经在上面的代码中这样做了。

标签: php xml tags


【解决方案1】:

在 DOM 中添加节点是一个两步过程。您首先使用文档对象上的一种方法创建它们,然后使用父节点上的方法附加/插入它。

这是一个没有数据库调用的例子:

$data = [
    [ 
        'site_name' => "Mineral Point Mine", 
        'mrds_id'=> "D010915"
    ]
];

$document = new DOMDocument('1.0', 'UTF-8');
$document->appendChild(
    $markers = $document->createElement('markers')
);

foreach ($data as $row) {
    // create the marker node using document method
    $marker = $document->createElement('marker');
    // add attributes
    $marker->setAttribute('site_name', $row['site_name']);
    $marker->setAttribute('mrds_id', $row['mrds_id']);
    // append to markers parent element
    $markers->appendChild($marker);
}

$document->formatOutput = TRUE;
echo $document->saveXML();

输出:

<?xml version="1.0" encoding="UTF-8"?>
<markers>
  <marker site_name="Mineral Point Mine" mrds_id="D010915"/>
</markers>

通常 DOM 方法采用 UTF-8 值而不进行转义。它会根据需要在序列化过程中转义特殊字符。您的parseXML() 调用将导致双重转义。

例外是DOMDocument::createElement()DOMNode::$nodeValue 赋值的第二个参数。他们只转义一些字符,如&lt;。我建议避开它们。附加文本节点或使用DOMNode::$textContent 属性。

演示:

$document = new DOMDocument('1.0', 'UTF-8');
$document->appendChild(
    $example = $document->createElement('example')
);
// assign content with special characters
$example->textContent = 'a & b, <>';
echo $document->saveXML();

输出:

<?xml version="1.0" encoding="UTF-8"?>
<example>a &amp; b, &lt;&gt;</example>

【讨论】:

  • 谢谢。我会试试这个。在 IT 工作了 16 年之后,人们会认为我会知道这个害羞。 :-)
  • 谢谢;这是工作。嗯。我的代码不优雅。如果你每个人都在科罗拉多州,请查一下我:我欠你一个预卷。 :-)
猜你喜欢
  • 2020-10-21
  • 1970-01-01
  • 2012-12-11
  • 2015-05-08
  • 1970-01-01
  • 1970-01-01
  • 2014-09-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多