php 强制 SimpleXML 对象为字符串,而不考虑上下文
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/416548/
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
Forcing a SimpleXML Object to a string, regardless of context
提问by Mark Biek
Let's say I have some XML like this
假设我有一些这样的 XML
<channel>
<item>
<title>This is title 1</title>
</item>
</channel>
The code below does what I want in that it outputs the title as a string
下面的代码做了我想要的,它将标题输出为字符串
$xml = simplexml_load_string($xmlstring);
echo $xml->channel->item->title;
Here's my problem. The code below doesn't treat the title as a string in that context so I end up with a SimpleXML object in the array instead of a string.
这是我的问题。下面的代码不会将标题视为该上下文中的字符串,因此我最终在数组中使用 SimpleXML 对象而不是字符串。
$foo = array( $xml->channel->item->title );
I've been working around it like this
我一直在解决这个问题
$foo = array( sprintf("%s",$xml->channel->item->title) );
but that seems ugly.
但这看起来很丑。
What's the best way to force a SimpleXML object to a string, regardless of context?
无论上下文如何,将 SimpleXML 对象强制为字符串的最佳方法是什么?
回答by Aron Rotteveel
Typecast the SimpleXMLObject to a string:
将 SimpleXMLObject 类型转换为字符串:
$foo = array( (string) $xml->channel->item->title );
The above code internally calls __toString()on the SimpleXMLObject. This method is not publicly available, as it interferes with the mapping scheme of the SimpleXMLObject, but it can still be invoked in the above manner.
上面的代码在内部调用__toString()SimpleXMLObject。这个方法不是公开可用的,因为它干扰了 SimpleXMLObject 的映射方案,但它仍然可以通过上述方式调用。
回答by Zaje
You can use the PHP function
你可以使用PHP函数
strval();
This function returns the string values of the parameter passed to it.
此函数返回传递给它的参数的字符串值。
回答by Mixed Case
There is native SimpleXML method SimpleXMLElement::asXMLDepending on parameter it writes SimpleXMLElement to xml 1.0 file or just to a string:
有原生 SimpleXML 方法SimpleXMLElement::asXML根据参数,它将 SimpleXMLElement 写入 xml 1.0 文件或仅写入字符串:
$xml = new SimpleXMLElement($string);
$validfilename = '/temp/mylist.xml';
$xml->asXML($validfilename); // to a file
echo $xml->asXML(); // to a string
回答by Mikepote
Another ugly way to do it:
另一种丑陋的方法:
$foo = array( $xml->channel->item->title."" );
It works, but it's not pretty.
它有效,但并不漂亮。
回答by DJ Far
The accepted answer actually returns an array containing a string, which isn't exactly what OP requested (a string). To expand on that answer, use:
接受的答案实际上返回一个包含字符串的数组,这不是 OP 所要求的(字符串)。要扩展该答案,请使用:
$foo = [ (string) $xml->channel->item->title ][0];
Which returns the single element of the array, a string.
它返回数组的单个元素,一个字符串。
回答by Ariel Ruiz
Try strval($xml->channel->item->title)
试试 strval($xml->channel->item->title)
回答by Pete
To get XML data into a php array you do this:
要将 XML 数据放入 php 数组,请执行以下操作:
// this gets all the outer levels into an associative php array
$header = array();
foreach($xml->children() as $child)
{
$header[$child->getName()] = sprintf("%s", $child);
}
echo "<pre>\n";
print_r($header);
echo "</pre>";
To get a childs child then just do this:
要获得孩子的孩子,请执行以下操作:
$data = array();
foreach($xml->data->children() as $child)
{
$header[$child->getName()] = sprintf("%s", $child);
}
echo "<pre>\n";
print_r($data);
echo "</pre>";
You can expand $xml-> through each level until you get what you want You can also put all the nodes into one array without the levels or just about any other way you want it.
您可以通过每个级别扩展 $xml-> ,直到获得所需内容。您还可以将所有节点放入一个没有级别的数组中,或者以您想要的任何其他方式。
回答by billynoah
Not sure if they changed the visibility of the __toString()method since the accepted answer was written but at this time it works fine for me:
不确定他们是否改变了__toString()方法的可见性,因为接受的答案是写的,但此时它对我来说很好用:
var_dump($xml->channel->item->title->__toString());
OUTPUT:
输出:
string(15) "This is title 1"
回答by BAVA SHIEK BAREETH
There is native SimpleXML method SimpleXMLElement::asXML Depending on parameter it writes SimpleXMLElement to xml 1.0 file, Yes
有原生 SimpleXML 方法 SimpleXMLElement::asXML 根据参数,它将 SimpleXMLElement 写入 xml 1.0 文件,是的
$get_file= read file from path;
$itrate1=$get_file->node;
$html = $itrate1->richcontent->html;
echo $itrate1->richcontent->html->body->asXML();
print_r((string) $itrate1->richcontent->html->body->asXML());
回答by kk4You
Just put the ''. before any variable, it will convert into string.
只要把''。在任何变量之前,它将转换为字符串。
$foo = array( ''. $xml->channel->item->title );
$foo = array( ''. $xml->channel->item->title );

