使用PHP创建XML文件

时间:2014-10-07 22:18:14

标签: php xml

我正在尝试使用PHP创建一个XML文件,但我无法让它运行正确。我可以让xml自己运行,php可以自己运行,但我不能让它们一起运行。

这是我的PHP代码:

<?xml version="1.0"?>
<!DOCTYPE squad SYSTEM "squad.dtd">
<?xml-stylesheet href="squad.xsl?" type="text/xsl"?>

<squad nick="MyTAG">
<name>GROUP NAME</name>
<email>admin@website.com</email>
<web>http://website.com/</web>
<picture>Logo.png</picture>
<title>Multi-gaming Team</title>

<?php
mysql_connect("", "", "") or die(mysql_error()); 
mysql_select_db("") or die(mysql_error());

$sql = "SELECT fid37, fid38, fid39 FROM mytable WHERE fid37 IS NOT NULL";
$res = mysql_query($sql);

$xml = new XMLWriter();
$xml->openURI("php://output");
$xml->startDocument();
$xml->setIndent(true);

while ($row = mysql_fetch_assoc($res)) {
  $xml->startElement('member');
  $xml->writeAttribute('id', $row['fid38']);
  $xml->writeAttribute('nick', $row['fid37']);

  $xml->writeElement('name', $row['fid37']);
  $xml->writeElement('email', 'N/A');  
  $xml->writeElement('icq', 'N/A');  
  $xml->writeElement('remark', $row['fid39']);  

$xml->endElement('member');
}

//header('Content-type: text/xml');
$xml->flush();

?>
</squad>

输出为

<squad nick="MyTAG">
    <name>GROUP NAME</name>
    <email>admin@website.com</email>
    <web>http://website.com/</web>
    <picture>Logo.png</picture>
    <title>Multi-gaming Team</title>    

    <member id="NUMBER" nick="NAME">
    <name>NAME</name>
    <email>N/A</email>
    <icq>N/A</icq>
    <remark>My REMARK</remark>
    </member>

    <member id="NUMBER" nick="NAME">
    <name>NAME</name>
    <email>N/A</email>
    <icq>N/A</icq>
    <remark>My REMARK</remark>
    </member>
</squad>

有人能帮我纠正这个吗?

3 个答案:

答案 0 :(得分:1)

最大的问题在于这一行:

$xml->endElement('member');

对于面向对象的样式,XMLWriter::endElement不需要参数。来自http://php.net/manual/en/function.xmlwriter-end-element.php

  

bool XMLWriter :: endElement(void)

参数是使endElement失败,导致意外的嵌套。删除参数,您的嵌套问题应该解决:

$xml->endElement();

另一个问题是XMLWriter::startDocument将在文档中间输出<?xml version="1.0"?>,它在打开php标记之前开始。不要将原始XML与XMLWriter混合,而是让XMLWriter对象处理所有内容。

尝试以下修改后的代码版本:

<?php
//mysql_connect("", "", "") or die(mysql_error()); 
//mysql_select_db("") or die(mysql_error());
//$sql = "SELECT fid37, fid38, fid39 FROM mytable WHERE fid37 IS NOT NULL";
//$res = mysql_query($sql);

$rows = array
(
    array ("fid37" => "1fid37", "fid38" => "1fid38", "fid39" => "1fid39"),
    array ("fid37" => "2fid37", "fid38" => "2fid38", "fid39" => "2fid39"),
    array ("fid37" => "3fid37", "fid38" => "3fid38", "fid39" => "3fid39"),
    array ("fid37" => "4fid37", "fid38" => "4fid38", "fid39" => "4fid39")
);

$xml = new XMLWriter();
$xml->openURI("php://output");
$xml->startDocument();
$xml->setIndent(true);
$xml->writePI('xml-stylesheet', 'href="squad.xsl?" type="text/xsl"');
$xml->writeDTD('squad', null, 'squad.dtd');

$xml->startElement('squad');
$xml->writeAttribute('nick', 'MyTAG');

$xml->writeElement('name', 'GROUP NAME');
$xml->writeElement('email', 'admin@website.com');
$xml->writeElement('web', 'http://website.com/');
$xml->writeElement('picture', 'Logo.png');
$xml->writeElement('title', 'Multi-gaming Team');

//while ($row = mysql_fetch_assoc($res))
foreach ($rows as $row)
{
    $xml->startElement('member');
    $xml->writeAttribute('id', $row['fid38']);
    $xml->writeAttribute('nick', $row['fid37']);

    $xml->writeElement('name', $row['fid37']);
    $xml->writeElement('email', 'N/A');  
    $xml->writeElement('icq', 'N/A');  
    $xml->writeElement('remark', $row['fid39']);  

    $xml->endElement();
}

$xml->endElement();

header('Content-type: text/xml');
$xml->flush();

代码应该生成以下XML:

<?xml version="1.0"?>
<?xml-stylesheet href="squad.xsl?" type="text/xsl"?>
<!DOCTYPE squad
SYSTEM "squad.dtd">
<squad nick="MyTAG">
 <name>GROUP NAME</name>
 <email>admin@website.com</email>
 <web>http://website.com/</web>
 <picture>Logo.png</picture>
 <title>Multi-gaming Team</title>
 <member id="1fid38" nick="1fid37">
  <name>1fid37</name>
  <email>N/A</email>
  <icq>N/A</icq>
  <remark>1fid39</remark>
 </member>
 <member id="2fid38" nick="2fid37">
  <name>2fid37</name>
  <email>N/A</email>
  <icq>N/A</icq>
  <remark>2fid39</remark>
 </member>
 <member id="3fid38" nick="3fid37">
  <name>3fid37</name>
  <email>N/A</email>
  <icq>N/A</icq>
  <remark>3fid39</remark>
 </member>
 <member id="4fid38" nick="4fid37">
  <name>4fid37</name>
  <email>N/A</email>
  <icq>N/A</icq>
  <remark>4fid39</remark>
 </member>
</squad>

答案 1 :(得分:0)

问题是您的脚本没有对您在其顶部发布的XML代码执行任何操作。您需要使用XMLWriter输出它 - 您不能像以前那样将XML添加到脚本中。

你的脚本应该是这样的(我省略了一些细节 - 你可以随意填写它们!):

<?php

## these should be changed to mysqli_* functions!
mysql_connect("", "", "") or die(mysql_error()); 
mysql_select_db("") or die(mysql_error());
$sql = "SELECT fid37, fid38, fid39 FROM mytable WHERE fid37 IS NOT NULL";
$res = mysql_query($sql);

# open the XML document
$xml = new XMLWriter();
$xml->openURI("php://output");
$xml->setIndent(true);

$xml->startDocument('1.0');

# writes the DTD line
$xml->writeDTD('squad', NULL, 'squad.dtd');

# writes the stylesheet line
$xml->writePI('xsl-stylesheet', 'href="squad.xsl?" type="text/xsl"');

# now output the squad element and its child nodes
$xml->startElement("squad");
$xml->writeAttribute('nick', 'MYTAG');
$xml->writeElement('name', 'GROUP NAME');
... # etc. for the rest of the elements

# add the members    
while ($row = mysql_fetch_assoc($res)) {
  $xml->startElement('member');
  $xml->writeAttribute('id', $row['fid38']);
  ... # etc.
  # close the member element
  $xml->endElement();
}
# close the squad element and the document
$xml->endElement();
$xml->endDocument();

答案 2 :(得分:0)

这就是我最终使用的(Starson Hochschild的代码和一个小编辑):

$sql = "SELECT * FROM mytable WHERE `fid37` IS NOT NULL";
$res = mysql_query($sql);




$xml = new XMLWriter();
$xml->openURI("php://output");
$xml->startDocument();
$xml->setIndent(true);
$xml->writePI('xml-stylesheet', 'href="squad.xsl?" type="text/xsl"');
$xml->writeDTD('squad', null, 'squad.dtd');

$xml->startElement('squad');
$xml->writeAttribute('nick', 'MyTag');

$xml->writeElement('name', 'GroupName');
$xml->writeElement('email', 'GroupEmail');
$xml->writeElement('web', 'GroupWebsite');
$xml->writeElement('picture', 'GroupLogo.png');
$xml->writeElement('title', 'Multi-gaming Team');

while ($row = mysql_fetch_assoc($res))
//foreach ($rows as $row)
{
    $xml->startElement('member');
    $xml->writeAttribute('id', $row['fid38']);
    $xml->writeAttribute('nick', $row['fid37']);

    $xml->writeElement('name', $row['fid37']);
    $xml->writeElement('email', 'N/A');  
    $xml->writeElement('icq', 'N/A');  
    $xml->writeElement('remark', $row['fid39']);  

    $xml->endElement();
}

$xml->endElement();

header('Content-type: text/xml');
$xml->flush();

?>

它看起来是什么,如果我实际编写xml代码将如何。