SimpleXml 到 string

2022-08-30 07:58:48

有没有函数可以从PHP制作字符串?SimpleXMLElement


答案 1

您可以使用 SimpleXMLElement::asXML() 方法来完成此操作:

$string = "<element><child>Hello World</child></element>";
$xml = new SimpleXMLElement($string);

// The entire XML tree as a string:
// "<element><child>Hello World</child></element>"
$xml->asXML();

// Just the child node as a string:
// "<child>Hello World</child>"
$xml->child->asXML();

答案 2

您可以使用转换:

<?php

$string = "<element><child>Hello World</child></element>";
$xml = new SimpleXMLElement($string);

$text = (string)$xml->child;

$text将是“Hello World”


推荐