php 让 SimpleXMLElement 在输出中包含编码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/869650/
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 00:09:22  来源:igfitidea点击:

Getting SimpleXMLElement to include the encoding in output

phpxmldomsimplexml

提问by dirtside

This:

这个:

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

...outputs this:

...输出这个:

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

But I want it to output the encoding, too:

但我也希望它输出编码:

<?xml version="1.0" encoding="UTF-8"?>
<foo/>

Is there some way to tell SimpleXMLElement to include the encoding attribute of the <?xml?> tag? Aside from doing this:

有没有办法告诉 SimpleXMLElement 包含 <?xml?> 标签的编码属性?除了这样做:

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

Which works, but it's annoying to have to manually specify the version and encoding.

哪个有效,但必须手动指定版本和编码很烦人。

Assume for the purposes of this question that I cannot use DOMDocument instead.

出于这个问题的目的,假设我不能使用 DOMDocument 代替。

回答by Cristian Toma

You can try this, but you must use simplexml_load_stringfor $xml

你可以试试这个,但你必须对 $xml使用simplexml_load_string

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

Or you can still use other means to add the encoding to your output.

或者您仍然可以使用其他方式将编码添加到您的输出中。

Simple Replacement

简单更换

$outputXML=str_replace('<?xml version="1.0"?>', '<?xml version="1.0" encoding="UTF-8"?>', $outputXML);

Regular Expressions

常用表达

$outputXML=preg_replace('/<\?\s*xml([^\s]*)\?>/' '<?xml  encoding="UTF-8"?>', $outputXML);

DOMDocument- I know you said you don't want to use DOMDocument, but here is an example

DOMDocument-我知道你说过你不想使用 DOMDocument,但这里有一个例子

$xml=dom_import_simplexml($simpleXML);
$xml->xmlEndoding='UTF-8';
$outputXML=$xml->saveXML();

You can wrap this code into a function that receives a parameter $encoding and adds it to the

您可以将此代码包装到一个函数中,该函数接收一个参数 $encoding 并将其添加到

回答by DarckBlezzer

Simple and clear only do this

简单明了只做这个

$XMLRoot = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><element></element>');

OutPut

输出

<?xml version="1.0" encoding="UTF-8"?>
      <element></element>

to add attributes in element only use

在元素中添加属性只使用

$XMLRoot->addAttribute('name','juan');

to add child use

添加儿童使用

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

回答by LevinM

The DOMDoc proposal of Cristian Tomaseems a good approach if the document isn't too heavy. You could wrap it up in something like this:

如果文档不太重,Cristian Toma的 DOMDoc 提案似乎是一个不错的方法。你可以把它包装成这样:

private function changeEncoding(string $xml, string $encoding) {
    $dom = new \DOMDocument();
    $dom->loadXML($xml);
    $dom->encoding = $encoding;
    return $dom->saveXML();
}

Comes in useful when you don't have access to the serializer producing the xml.

当您无权访问生成 xml 的序列化程序时很有用。

回答by Toby Allen

I would say you will need to do this on creation of each XML object. Even if SimpleXMLElement had a way of setting it you would still need to set it as I guess it would be possible for the object to pick a valid default.

我会说您需要在创建每个 XML 对象时执行此操作。即使 SimpleXMLElement 有设置它的方法,您仍然需要设置它,因为我猜对象有可能选择一个有效的默认值。

Maybe create a constant and Create objects like this

也许创建一个常量并像这样创建对象

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

回答by TML

If you don't specify an encoding, SimpleXML cannot (sanely) guess which one you intended.

如果您不指定编码,SimpleXML 无法(理智地)猜测您想要的编码。