-3

我正在尝试从我的数据库中输出各种表,但我似乎无法弄清楚如何执行我的想法。

我想像这样输出我的数据。

<locations>
    <destination>
       <name>A</name>
       <address>B</name>
    </destination>

    <destination>
       <name>C</name>
       <address>D</name>
    </destination>
</locations>

到目前为止,我有这个作为输出。

 <Locations>
<destination name="burlo"/>
<destination name="Raymund"/>
<destination name="Bacolod City"/>
<destination name="Victorias"/>
<destination name="Sipalay"/>
<destination name="Ambot"/>
<destination name="aweawea"/>
<destination name="ilo-ilo"/>
<destination name="ilo-ilo"/>
<destination name="Hinobaan"/>
<destination name="heart"/>
<destination name="heart"/>
<destination name="heart"/>
<destination name="heart"/>
<destination name="heart"/>
<destination name="Daddy"/>
<destination name="aguisan"/>
</Locations>

这就是我生成它们的方式。

// Start XML file, create parent node


$dom = new DOMDocument("1.0");
$node = $dom->createElement("Locations");
$parnode = $dom->appendChild($node); 

// Select all the rows in the markers table

$query = "SELECT  * FROM tbl_locations where status='active'";
$result = mysql_query($query);
if (!$result) {  
  die('Invalid query: ' . mysql_error());
} 

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

// Iterate through the rows, adding XML nodes for each

while ($row = @mysql_fetch_assoc($result)){  
  // ADD TO XML DOCUMENT NODE  

  $node = $dom->createElement("destination");  

  $newnode = $parnode->appendChild($node);   
  $newnode->setAttribute("name",$row['name']);




} 

echo $dom->saveXML();

?>

将不胜感激任何帮助。

4

3 回答 3

2

将此行更改为

$newnode->setAttribute("name",$row['name']);

到这些行

$nameNode = $doc->createElement('name');
$nameNode -> appendChild($doc->createTextNode($row['name']));
$node->appendChild($nameNode);

$addNode = $doc->createElement('address');
$addNode -> appendChild($doc->createTextNode($row['address'])); 
$node->appendChild($addNode);

因为您需要构建树。

于 2013-02-23T05:56:41.163 回答
1

做这个

$node = $dom->createElement("destination");  
$newnode = $parnode->appendChild($node);  
$node1 = $dom->createElement("name");   
$newnode = $parnode->appendChild($node1);  
$newnode->insertData(0,$row['name']);
于 2013-02-23T05:55:07.383 回答
0

希望这可以帮助,

<?php 
$xml          = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
$root_element = "locations"; 
$xml         .= "<$root_element>";
$sql ="****  YOUR QUERY  ****";
if (!$result = mysql_query($sql))
   die("Query failed.");

if(mysql_num_rows($result)>0)
{
   $i = 0;
   while($result_array = mysql_fetch_array($result))
   {
    $xml .= "<destination>";
    $result[$i]['name']=$result_array['name'];
    $xml .= "<name><![CDATA[{$result[$i]['name']}]]></name>";
    $result[$i]['dea']=$result_array['address'];
    $xml .= "<address><![CDATA[{$result[$i]['address']}]]></address>";
    $xml.="</destination>";

    $i++;
   }
}
//close the root element

$xml .= "</$root_element>";

//send the xml header to the browser
header ("Content-Type:text/xml"); 

//output the XML data
echo $xml;

如果您有任何疑问,请告诉我

于 2013-02-23T10:57:42.547 回答