定義和用法
XML 是一種 mark-up 語言,用於在網絡上共享數據,XML 用於人類 read-able 和機器 read-able。 SimpleXMLElement 類表示 PHP 中的 XML 文檔。
這個SimpleXMLElement::__toString()函數檢索並返回當前 XML 元素的文本內容。
用法
SimpleXMLElement::__toString();
參數
該函數不接受任何參數。
返回值
此函數在成功的情況下返回一個表示當前 XML 元素內容的字符串值,在失敗的情況下返回一個空字符串。
PHP版本
這個函數最初是在 PHP 版本 5 中引入的,並且適用於所有後續版本。
示例
以下示例演示了 SimpleXMLElement::__toString() 函數的用法。
<html>
<head>
<body>
<?php
$str="<?xml version='1.0'?>
<text> Welcome to Tutorialspoint</text>";
$xml=new SimpleXMLElement($str);
$res=$xml->__toString();
print($res);
?>
</body>
</head>
</html>
這將產生以下結果 -
Welcome to Tutorialspoint
示例
以下示例讀取 XML 文件的內容並打印其中元素的名稱 -
data.xml:
<?xml version="1.0" encoding="utf-8"?>
<Tutorials>
<Tutorial>
<Name>JavaFX</Name>
<Pages>535</Pages>
<Author>Krishna</Author>
<Version>11</Version>
</Tutorial>
<Tutorial>
<Name>CoffeeScript</Name>
<Pages>235</Pages>
<Author>Kasyap</Author>
<Version>2.5.1</Version>
</Tutorial>
<Tutorial>
<Name>OpenCV</Name>
<Pages>150</Pages>
<Author>Maruti</Author>
<Version>3.0</Version>
</Tutorial>
</Tutorials>
sample.html:
<html>
<head>
<body>
<?php
$doc = new DOMDocument;
$xml = simplexml_load_file("trail.xml");
//file to SimpleXMLElement
$xml = simplexml_import_dom($xml);
print($xml->getName()."<br>");
foreach ($xml->children() as $child){
print("::". $child->getName() ."<br>");
foreach ($child->children() as $child){
print(":::::". $child->getName() ."::".$child->__toString() ."<br>");
}
}
?>
</body>
</head>
</html>
這將產生以下結果 -
Tutorials ::Tutorial :::::Name::JavaFX :::::Pages::535 :::::Author::Krishna :::::Version::11 ::Tutorial :::::Name::CoffeeScript :::::Pages::235 :::::Author::Kasyap :::::Version::2.5.1 ::Tutorial :::::Name::OpenCV :::::Pages::150 :::::Author::Maruti :::::Version::3.0
示例
以下是此函數的另一個示例 -
<html>
<head>
<body>
<?php
$data = "<Tutorials> </Tutorials>";
$xml = simplexml_load_string($data);
print_r($xml);
//Adding the child node
$child = $xml->addChild('Tutorial');
$child->addChild('Name', 'OpenCV');
$child->addChild('Pages', '230');
$child->addChild('Author', 'Maruthi');
$child->addChild('Version', '5.5');
foreach ($xml->children() as $child){
print("::". $child->getName() ."<br>");
foreach ($child->children() as $child){
print(":::::". $child->getName());
print(" -- ". $child->__toString() ."<br>");
}
}
?>
</body>
</head>
</html>
這將產生以下結果 -
SimpleXMLElement Object ( )::Tutorial :::::Name -- OpenCV :::::Pages -- 230 :::::Author -- Maruthi :::::Version -- 5.5
相關用法
- PHP SimpleXMLElement::__construct()用法及代碼示例
- PHP SimpleXMLElement::xpath()用法及代碼示例
- PHP SimpleXMLElement::getName()用法及代碼示例
- PHP SimpleXMLElement::registerXPathNamespace()用法及代碼示例
- PHP SimpleXMLElement::getDocNamespaces()用法及代碼示例
- PHP SimpleXMLElement::getNameSpaces()用法及代碼示例
- PHP SimpleXMLElement::count()用法及代碼示例
- PHP SimpleXMLElement::addChild()用法及代碼示例
- PHP SimpleXMLElement children()用法及代碼示例
- PHP SimpleXMLElement XPath()用法及代碼示例
- PHP SimpleXMLElement addAttribute()用法及代碼示例
- PHP SimpleXMLElement addChild()用法及代碼示例
- PHP SimpleXMLElement attributes()用法及代碼示例
- PHP SimpleXMLElement __toString()用法及代碼示例
- PHP SimpleXMLElement asXML()用法及代碼示例
- PHP SimpleXMLElement count()用法及代碼示例
- PHP SimpleXMLElement registerXPathNamespace()用法及代碼示例
- PHP SimpleXMLElement getNamespaces()用法及代碼示例
- PHP SimpleXMLElement getDocNamespaces()用法及代碼示例
- PHP SimpleXMLElement saveXML()用法及代碼示例
注:本文由純淨天空篩選整理自 PHP - SimpleXMLElement::__toString() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。