dongxing9219 2009-11-02 11:46
浏览 14
已采纳

从PHP函数生成XML(我做错了什么?)

Heres a simple php script that generates some XML:

<?php
// create doctype
$dom = new DOMDocument("1.0");

// display document in browser as plain text
header("Content-Type: text/plain");

// create root element
$root = $dom->createElement("page");
$dom->appendChild($root);

// loop through all posts
while (have_posts()) : the_post();

    // create child element
    $item = $dom->createElement("title");
    $root->appendChild($item);

    // add title data
    $text = $dom->createTextNode(the_title());
    $item->appendChild($text);

endwhile;

// save and display tree
echo $dom->saveXML();
?>

It seems that using a function the_title() within my XML ends up printing outside of the xml tree (below is the browser output from the above code):

This is my second postHello world!<?xml version="1.0"?>
<page><title></title><title></title></page>

The above code seems to work fine if i replace the function the_title() with some static text, the xml is generated as required, e.g if

$text = $dom->createTextNode("Title-goes-here");

The XML generated is (which is exactly how i want it):

<?xml version="1.0"?>
<page><title>Title-goes-here</title><title>Title-goes-here</title></page>

the_title() is a Wordpress function, and i am trying to render the page in XML format.

  • 写回答

2条回答 默认 最新

  • dqthn68688 2009-11-02 11:51
    关注

    I think the functions like the_post() and the_title() will immediately output the value rather than returning it. Some of them have equivalents like get_the*() which will return the value instead of printing; this is probably what you want.

    This answer might be useful

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据