使用循环填充 XML
我试图找出一种更动态地填充 XML 对象的方法,而不必不断更改节点的名称。我有一些与此类似的代码:
编辑:将数据更改为与实际数据库结构内联:
$query = ("Select order_id, created_on, updated_on, status from table orders;");
// Execute query
$result = mysql_query($query, $link) or die("Could not complete database query");
// Populate array
while(($resultArray[] = mysql_fetch_assoc($result)) || array_pop($resultArray));
$doc = new DOMDocument();
$doc->formatOutput = true;
$r = $doc->createElement( "data" );
$doc->appendChild( $r );
foreach( $resultArray as $record )
{
$b = $doc->createElement( "record" );
$record1 = $doc->createElement( "Order" );
$record1->appendChild(
$doc->createTextNode( $record['order_id'] )
);
$b->appendChild( $record1 );
$record2 = $doc->createElement( "Created" );
$record2->appendChild(
$doc->createTextNode( $record['created_on'] )
);
$b->appendChild( $record2 );
$record3 = $doc->createElement( "Updated" );
$record3->appendChild(
$doc->createTextNode( $record['updated_on'] )
);
$b->appendChild( $record3 );
$record4 = $doc->createElement( "Status" );
$record4->appendChild(
$doc->createTextNode( $record['status'] )
);
$b->appendChild( $record4 );
$r->appendChild( $b );
}
echo $doc->saveXML();
// Close connection
mysql_close($link);
这很好,但是如果我想在查询中添加第三列,我还必须将其添加到循环中。我确信有更好的方法来实际做到这一点。
有什么建议吗?
谢谢。
I'm trying to figure out a way to populate an XML object more dynamically without having to constantly change the name of the nodes. I some code similar to this:
EDIT: Changed the data to be inline with actual db structure:
$query = ("Select order_id, created_on, updated_on, status from table orders;");
// Execute query
$result = mysql_query($query, $link) or die("Could not complete database query");
// Populate array
while(($resultArray[] = mysql_fetch_assoc($result)) || array_pop($resultArray));
$doc = new DOMDocument();
$doc->formatOutput = true;
$r = $doc->createElement( "data" );
$doc->appendChild( $r );
foreach( $resultArray as $record )
{
$b = $doc->createElement( "record" );
$record1 = $doc->createElement( "Order" );
$record1->appendChild(
$doc->createTextNode( $record['order_id'] )
);
$b->appendChild( $record1 );
$record2 = $doc->createElement( "Created" );
$record2->appendChild(
$doc->createTextNode( $record['created_on'] )
);
$b->appendChild( $record2 );
$record3 = $doc->createElement( "Updated" );
$record3->appendChild(
$doc->createTextNode( $record['updated_on'] )
);
$b->appendChild( $record3 );
$record4 = $doc->createElement( "Status" );
$record4->appendChild(
$doc->createTextNode( $record['status'] )
);
$b->appendChild( $record4 );
$r->appendChild( $b );
}
echo $doc->saveXML();
// Close connection
mysql_close($link);
This is fine, but if I want to add a 3rd column in the query I have to also add it to the loop. I'm sure there is a better way to actually do this.
Any advice?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许我写的这个类会对您有所帮助...
如何使用它...
您还可以使用第三个参数
$elements
用自定义元素覆盖该级别的 $key。例如...Maybe this class I wrote will help you out...
How to use it...
You can also use the third argument
$elements
to override the $key at the level with a custom element. For exmple...你可以这样做:
当然,你没有给我们确切的数据库结构,所以我假设你想要添加的列以
_Column
结尾(这就是为什么有!preg_match( '/_Column$/', $r_key)
)。如果您想要所有列,只需删除循环开头的这两行即可。编辑:该死,我晚了 16 秒:<
You can do:
Of course, you have not given us the exact DB structure, so I have assumed that the columns you want to add are ending with
_Column
(that's why there's that!preg_match('/_Column$/', $r_key)
). If you want all columns, just remove those 2 lines at the start of the loop.EDIT: damn, I was 16 seconds too late :<
我会做类似的事情:
I would do something like that: