PHP在XML中的输出
我试图用XML输出PHP脚本的结果,但结果并不理想 这是我得到的一行XML文件(没有错误消息)PHP在XML中的输出,php,xml,Php,Xml,我试图用XML输出PHP脚本的结果,但结果并不理想 这是我得到的一行XML文件(没有错误消息) 这就是我想看到的 <data> <thread> <id>my data here</id> <author> <name>my data here/name> <url>my data here</url>
这就是我想看到的
<data>
<thread>
<id>my data here</id>
<author>
<name>my data here/name>
<url>my data here</url>
<id>my data here</id>
</author>
<title>my data here</title>
<reactions>my data here</reactions>
<dislikes>my data here</dislikes>
<userScore>my data here</userScore>
<createdAt>my data here</createdAt>
<slug>my data here</slug>
<postNumber>my data here</postNumber>
<link>my data here</link>
<likes>my data here</likes>
<message>my data here</message>
<category>my data here</category>
<score>my data here</score>
<categoryLink>my data here</categoryLink>
</thread>
</data>
我的数据在这里
我的数据在此/姓名>
我的数据在这里
我的数据在这里
我的数据在这里
我的数据在这里
我的数据在这里
我的数据在这里
我的数据在这里
我的数据在这里
我的数据在这里
我的数据在这里
我的数据在这里
我的数据在这里
我的数据在这里
我的数据在这里
我的数据在这里
这是我的代码:
//Create XML
$xml = new DOMDocument("1.0", 'UTF-8');
$root = $xml->createElement("data");
$xml->appendChild($root);
$thread = $xml->createElement("thread");
$threadID = $xml->createElement("id");
$threadID->appendChild($xml->createTextNode($details->id));
$thread->appendChild($threadID);
$author = $xml->createElement("author");
$thread->appendChild($author);
$authorName = $xml->createElement("name");
$authorName->appendChild($xml->createTextNode($parsed_authorName));
$author->appendChild($authorName);
$authorUrl = $xml->createElement("url");
$authorUrl->appendChild($xml->createTextNode($parsed_authorUrl));
$author->appendChild($authorUrl);
$authorID = $xml->createElement("id");
$authorID->appendChild($xml->createTextNode($details->author));
$author->appendChild($authorID);
$threadTitle = $xml->createElement("title");
$threadTitle->appendChild($xml->createTextNode($details->title));
$thread->appendChild($threadTitle);
$threadReactions = $xml->createElement("reactions");
$threadReactions->appendChild($xml->createTextNode($details->reactions));
$thread->appendChild($threadReactions);
$threadDislikes = $xml->createElement("dislikes");
$threadDislikes->appendChild($xml->createTextNode($details->dislikes));
$thread->appendChild($threadDislikes);
$threadUserScore = $xml->createElement("userScore");
$threadUserScore->appendChild($xml->createTextNode($details->userScore));
$thread->appendChild($threadUserScore);
$threadCreatedAt = $xml->createElement("createdAt");
$threadCreatedAt->appendChild($xml->createTextNode($details->createdAt));
$thread->appendChild($threadCreatedAt);
$threadSlug = $xml->createElement("slug");
$threadSlug->appendChild($xml->createTextNode($details->slug));
$thread->appendChild($threadSlug);
$threadPostNumber = $xml->createElement("postNumber");
$threadPostNumber->appendChild($xml->createTextNode($details->posts));
$thread->appendChild($threadPostNumber);
$threadLink = $xml->createElement("link");
$threadLink->appendChild($xml->createTextNode($details->link));
$thread->appendChild($threadLink);
$threadLikes = $xml->createElement("likes");
$threadLikes->appendChild($xml->createTextNode($details->likes));
$thread->appendChild($threadLikes);
$threadMessage = $xml->createElement("likes");
$threadMessage->appendChild($xml->createTextNode($parsed_threadMessage));
$thread->appendChild($threadMessage);
$threadCategory = $xml->createElement("category");
$threadCategory->appendChild($xml->createTextNode($details->category));
$thread->appendChild($threadCategory);
$threadScore = $xml->createElement("likes");
$threadScore->appendChild($xml->createTextNode($parsed_threadScore));
$thread->appendChild($threadScore);
$threadCategoryLink = $xml->createElement("likes");
$threadCategoryLink->appendChild($xml->createTextNode($parsed_threadCategoryLink));
$thread->appendChild($threadCategoryLink);
$thread->appendChild($root);
$xml->formatOutput = true;
echo "<xmp>". $xml->saveXML() ."</xmp>";
$xml->save("test1.xml") or die("Error");
//创建XML
$xml=新的DOMDocument(“1.0”,“UTF-8”);
$root=$xml->createElement(“数据”);
$xml->appendChild($root);
$thread=$xml->createElement(“线程”);
$threadID=$xml->createElement(“id”);
$threadID->appendChild($xml->createTextNode($details->id));
$thread->appendChild($threadID);
$author=$xml->createElement(“author”);
$thread->appendChild($author);
$authorName=$xml->createElement(“名称”);
$authorName->appendChild($xml->createTextNode($parsed_authorName));
$author->appendChild($authorName);
$authorUrl=$xml->createElement(“url”);
$authorUrl->appendChild($xml->createTextNode($parsed_authorUrl));
$author->appendChild($authorUrl);
$authorID=$xml->createElement(“id”);
$authorID->appendChild($xml->createTextNode($details->author));
$author->appendChild($authord);
$threadTitle=$xml->createElement(“标题”);
$threadTitle->appendChild($xml->createTextNode($details->title));
$thread->appendChild($threadTitle);
$threadReactions=$xml->createElement(“反应”);
$threadReactions->appendChild($xml->createTextNode($details->reactions));
$thread->appendChild($threadReactions);
$threadDislikes=$xml->createElement(“dislikes”);
$threadDislikes->appendChild($xml->createTextNode($details->dislikes));
$thread->appendChild($threadDislikes);
$threadUserScore=$xml->createElement(“userScore”);
$threadUserScore->appendChild($xml->createTextNode($details->userScore));
$thread->appendChild($threadUserScore);
$threadCreatedAt=$xml->createElement(“createdAt”);
$threadCreatedAt->appendChild($xml->createTextNode($details->createdAt));
$thread->appendChild($threadCreatedAt);
$threadSlug=$xml->createElement(“slug”);
$threadSlug->appendChild($xml->createTextNode($details->slug));
$thread->appendChild($threadSlug);
$threadPostNumber=$xml->createElement(“postNumber”);
$threadPostNumber->appendChild($xml->createTextNode($details->posts));
$thread->appendChild($threadPostNumber);
$threadLink=$xml->createElement(“链接”);
$threadLink->appendChild($xml->createTextNode($details->link));
$thread->appendChild($threadLink);
$threadLikes=$xml->createElement(“likes”);
$threadLikes->appendChild($xml->createTextNode($details->likes));
$thread->appendChild($threadLikes);
$threadMessage=$xml->createElement(“likes”);
$threadMessage->appendChild($xml->createTextNode($parsed_threadMessage));
$thread->appendChild($threadMessage);
$threadCategory=$xml->createElement(“类别”);
$threadCategory->appendChild($xml->createTextNode($details->category));
$thread->appendChild($threadCategory);
$threadScore=$xml->createElement(“likes”);
$threadScore->appendChild($xml->createTextNode($parsed_threadScore));
$thread->appendChild($threadScore);
$threadCategoryLink=$xml->createElement(“喜欢”);
$threadCategoryLink->appendChild($xml->createTextNode($parsed_threadCategoryLink));
$thread->appendChild($threadCategoryLink);
$thread->appendChild($root);
$xml->formatOutput=true;
“回声”$xml->saveXML();
$xml->save(“test1.xml”)或die(“Error”);
可能是我附加XML元素的顺序不对吗?在接近结尾的地方,您写道
$thread->appendChild($root);
我想这是
$root->appendChild($thread);
因为要将线程节点添加到靠近末尾的根节点,所以要编写
$THREAD->appendChild($ROOT)代码>,我想这是$root->appendChild($thread)代码>谢谢!这就解决了。
$root->appendChild($thread);