gpt4 book ai didi

javascript - foreach循环使用字符串输出XML

转载 作者:行者123 更新时间:2023-11-29 03:24:52 25 4
gpt4 key购买 nike

在 php 脚本中,如何使用字符串执行 foreach 循环以输出从 msql 数据库获取值的 XML 数据?我从 simpleXmlElement 内的 while 循环中获取了一些代码,希望它能输出 xml,但它不起作用。我正在使用的服务器不允许使用 SimpleXMLElement 类,因此我需要使用字符串来执行 foreach 循环并根据需要输出 xml。

我目前拥有的:编辑:

<?php
#Programmer: Moses

require('./.env');

// Opens a connection to a mySQL server
try {
$db = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME. ';charset=utf8', DB_USERNAME, DB_PASSWORD);
} catch (PDOException $e) {
echo "An Error occured, could not connect!";
}

$statement = $db->query('SELECT * FROM markers');
//$result = $statement->fetchAll(PDO::FETCH_ASSOC);

$xmlWriter = new XMLWriter();
$xmlWriter->openUri('php://stdout');

$xmlWriter->startDocument();
$xmlWriter->setIndent(2);
$xmlWriter->startElement('markers');
foreach ($statement as $row) {
$xmlWriter->startElement('marker');
$xmlWriter->writeAttribute('name', $row['name']);
$xmlWriter->writeAttribute('address', $row['address']);
$xmlWriter->writeAttribute('lat', $row['lat']);
$xmlWriter->writeAttribute('lng', $row['lng']);
$xmlWriter->writeAttribute('type', $row['type']);
$xmlWriter->endElement();
}
$xmlWriter->endElement();
$xmlWriter->endDocument();


/**
$str = <<<XML
<xml>
<marker name="Walvis Bay Live" lat="-22.956112" lng="14.508056" address="Walvis bay namibia Africa" type="Weather Station"></marker>
<marker name="Centro Surf Bracciano" lat="11.588599" lng="43.145851" address="djibouti djibouti" type="Weather Station"></marker>
<marker name="Bashewa Weather" lat="-25.825212" lng="28.312128" address="Garstfontein Rd Pretoria" type="Weather Station"></marker>
<marker name="Nelspruit Live" lat="-25.475298" lng="30.969416" address="nelspruit south africa" type="Weather Station"></marker>
<marker name="Richards Bay Live" lat="-28.780727" lng="32.038284" address="richards bay south africa" type="Weather Station"></marker>
<marker name="Cape Town Live" lat="-33.923775" lng="18.423346" address="cape town south africa" type="Weather Station"></marker>
</xml>
XML;

header("Content-type: text/xml");
echo $str;
exit;
*/


/**
$xml = new SimpleXMLElement('<xml/>');

while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
$node = $xml->addChild('marker');
$node->addAttribute('name', $row['name']);
$node->addAttribute('address', $row['address']);
$node->addAttribute('lat', $row['lat']);
$node->addAttribute('lng', $row['lng']);
$node->addAttribute('type', $row['type']);
}

header('Content-type: text/xml');
print($xml->asXML());
exit;
*/

最佳答案

foreach 可以迭代任何实现可遍历的数组或对象。 PDOStatement::fetch() 的结果是记录/行。只需将语句提供给 foreach。

foreach($statement as $row) { ...   

像这样的转储更好的 API 是 XMLWriter .它将结果直接写入流,而不是先将整个文档存储在内存中。使用 XML API 也将根据需要处理转义。这是一个小例子:

$statement = [
[ 'name' => 'one', 'location' => '...', /* ... */],
[ 'name' => 'two', 'location' => '...', /* ... */]
];

$xmlWriter = new XMLWriter();
$xmlWriter->openUri('php://stdout');

$xmlWriter->startDocument();
$xmlWriter->setIndent(2);
$xmlWriter->startElement('markers');
foreach ($statement as $row) {
$xmlWriter->startElement('marker');
$xmlWriter->writeAttribute('name', $row['name']);
/* other attributes ... */
$xmlWriter->endElement();
}
$xmlWriter->endElement();
$xmlWriter->endDocument();

输出:

<?xml version="1.0"?>
<markers>
<marker name="one"/>
<marker name="two"/>
</markers>

在 DOM 中,您可以创建、附加和配置节点。这是一个小例子:https://stackoverflow.com/a/21760903/2265374

关于javascript - foreach循环使用字符串输出XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38165712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com