Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获取SimpleXMLElement以在输出中包含编码_Php_Xml_Dom_Simplexml - Fatal编程技术网

Php 获取SimpleXMLElement以在输出中包含编码

Php 获取SimpleXMLElement以在输出中包含编码,php,xml,dom,simplexml,Php,Xml,Dom,Simplexml,这: $XML=newsimplexmlement(“”); echo($XML->asXML()); …输出以下内容: $XML = new SimpleXMLElement("<foo />"); echo($XML->asXML()); 但我希望它也能输出编码: <?xml version="1.0"?> <foo/> 有没有办法告诉SimpleXMLElement包含标记的编码属性?除此之外: <?xml version="1

这:

$XML=newsimplexmlement(“”);
echo($XML->asXML());
…输出以下内容:

$XML = new SimpleXMLElement("<foo />");
echo($XML->asXML());

但我希望它也能输出编码:

<?xml version="1.0"?>
<foo/>

有没有办法告诉SimpleXMLElement包含标记的编码属性?除此之外:

<?xml version="1.0" encoding="UTF-8"?>
<foo/>
$XML=newsimplexmlement(“”);
echo($XML->asXML());
这是可行的,但是手动指定版本和编码很烦人


为了解决这个问题,假设我不能使用DOMDocument。

我想说,在创建每个XML对象时,您都需要这样做。即使SimpleXMLElement有一种设置它的方法,您仍然需要设置它,因为我想对象可能会选择一个有效的默认值

也许创建一个常量,然后创建这样的对象

$XML = new SimpleXMLElement("<?xml version='1.0' encoding='utf-8'?><foo />");
echo($XML->asXML());
$XML=新的SimpleXMLElement($XMLNamespace“”);
echo($XML->asXML());

如果未指定编码,SimpleXML无法(正常地)猜测您想要的编码。

您可以尝试此方法,但必须为$xml使用SimpleXML\u load\u string

$XML = new SimpleXMLElement($XMLNamespace . "<foo />");
echo($XML->asXML());
或者您仍然可以使用其他方法将编码添加到输出中

简单更换

$xml // Your main SimpleXMLElement
$xml->addAttribute('encoding', 'UTF-8');

您可以将此代码包装到一个函数中,该函数接收参数$encoding,并将其添加到“简单清除”中,仅此操作

$xml=dom_import_simplexml($simpleXML);
$xml->xmlEndoding='UTF-8';
$outputXML=$xml->saveXML();
添加子用途

$XMLRoot->addAttribute('name','juan');
如果文档不太重的话,DOMDoc的建议似乎是一个很好的方法。你可以用这样的方式来概括:

$childElement = $XMLRoot->addChild('elementChild');
$childElement->addAttribute('attribName','somthing');

当您无法访问生成xml的序列化程序时,它会很有用。

为什么需要输出中存在编码?因为这是一个外部要求。:)我知道,但为SimpleXML指定编码的唯一方法是使其在构造函数中显式化——然后还必须指定XML版本属性,否则会引发错误。很明显,我可以这样做,不得不这样做真让人讨厌。我想SimpleXML有点太简单了:)我在文档中找不到对此的任何引用,但我觉得您不能指定编码,因为SimpleXML需要UTF-8。有史以来最简单的解决方案,只需确保在$XMLNamespace之后添加“\n”。您的DOMDocument中存在语法错误:
$xml->Xmlending='UTF-8'
应该是
$xml->xmlcodencing='UTF-8'。清晰易用!谢谢。一个提示:
$XMLRoot=newsimplexmleElement(“”)使其更短
$XMLRoot = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><element></element>');
<?xml version="1.0" encoding="UTF-8"?>
      <element></element>
$XMLRoot->addAttribute('name','juan');
$childElement = $XMLRoot->addChild('elementChild');
$childElement->addAttribute('attribName','somthing');
private function changeEncoding(string $xml, string $encoding) {
    $dom = new \DOMDocument();
    $dom->loadXML($xml);
    $dom->encoding = $encoding;
    return $dom->saveXML();
}