php 将 xml DOMDocument 转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12988379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:35:33  来源:igfitidea点击:

converting xml DOMDocument to string

phpxmlajax

提问by Mallikarjuna Rao Kolluri

HI i am using ajax for a web site which displays courses available, i am having a problem where i am reading xml(which has courses details) getting node value(course name) comparing to my input(inout course name) if it is equal i am displaying the text(course description). now i am tracing it getting to correct course name(in xml file) but while comparing it to input course name its not at all comparing so.at one point i loaded the xml file and echo it:

嗨,我正在将 ajax 用于显示可用课程的网站,我在读取 xml(其中包含课程详细信息)获取节点值(课程名称)与我的输入(输入输出课程名称)进行比较时遇到问题(如果它相等)我正在显示文本(课程描述)。现在我正在跟踪它获得正确的课程名称(在 xml 文件中),但是在将它与输入课程名称进行比较时,它根本没有进行比较。有一次我加载了 xml 文件并回显了它:

$doc->load('data/ICT.xml'); 
echo"$doc";

It gives me an error

它给了我一个错误

Catchable fatal error: Object of class DOMDocument could not be converted to string in /home/students/....../www/htdocs/client/unit_details.php on line 23

可捕获的致命错误:第 23 行 /home/students/....../www/htdocs/client/unit_details.php 中的类 DOMDocument 的对象无法转换为字符串

so what i understood from this is that xml dom object should be converted to string so that i can get required data and use it, is it true? if so can some one tell me how to do so please(like any functions etc) thanks in advance :-)

所以我从这里了解到应该将 xml dom 对象转换为字符串,以便我可以获得所需的数据并使用它,是真的吗?如果可以的话,有人可以告诉我怎么做吗(如任何功能等),提前致谢:-)

回答by noetix

The DOMDocumentobject can't be used as a string.

DOMDocument对象不能用作字符串。

Here is how you would display the DOMDocumentobject as an XML string:

以下是将DOMDocument对象显示为 XML 字符串的方式:

echo $doc->saveXML();

回答by Vyktor

DOMDocumenthas a method SaveHTML(), it's designed to do exactly what you need.

DOMDocument有一种方法SaveHTML(),它旨在完全满足您的需求。

You may display whole document by:

您可以通过以下方式显示整个文档:

$doc->saveHTML();

Or just certain node:

或者只是某个节点:

$doc->saveHTML( $bodyNode);

DOMDocumentalso provides few options how to enhance how the XML is print, such as:

DOMDocument还提供了一些如何增强 XML 打印方式的选项,例如:

回答by CodeAngry

Try this:

尝试这个:

$xml = $doc->saveXML($doc->documentElement);

回答by StaticVariable

You can use Type casting

您可以使用类型转换

$doc->load('data/ICT.xml'); 
$doc2=(string)$doc;
echo $doc2;

Or You can use the PHP function

或者你可以使用 PHP 函数

strval();

This function returns the string values of the parameter passed to it.

此函数返回传递给它的参数的字符串值。