You can use the SimpleXMLElement::asXML() method to accomplish this:
$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();
Answer from user142162 on Stack Overflow Top answer 1 of 8
143
You can use the SimpleXMLElement::asXML() method to accomplish this:
$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 of 8
75
You can use casting:
<?php
$string = "<element><child>Hello World</child></element>";
$xml = new SimpleXMLElement($string);
$text = (string)$xml->child;
$text will be 'Hello World'
Videos
W3Schools
w3schools.com › php › func_simplexml_load_string.asp
PHP simplexml_load_string() Function
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
W3Schools
w3schools.com › php › php_xml_simplexml_read.asp
PHP SimpleXML Parser
simplexml_load_file() - Reads XML data from a file · The PHP simplexml_load_string() function is used to read XML data from a string.
php.cn
m.php.cn › home › backend development › php problem › how to convert xml to string in php
How to convert xml to string in php-PHP Problem-php.cn
August 26, 2020 - function xmlToArray($xml){ if (file_exists($xml)) { libxml_disable_entity_loader(false); $xml_string = simplexml_load_file($xml,'SimpleXMLElement', LIBXML_NOCDATA); }else{ libxml_disable_entity_loader(true); $xml_string = simplexml_load_string($xml,'SimpleXMLElement', LIBXML_NOCDATA); } $result = json_decode(json_encode($xml_string),true); return $result; } ... function array2xml($arr){ $str="<xml>"; foreach ($arr as $key=>$value){ $str.= "<$key>$value</$key>"; } $str.="</xml>"; return $str; } The above is the detailed content of How to convert xml to string in php.
BCCNsoft
doc.bccnsoft.com › docs › php-docs-7-en › simplexmlelement.asxml.html
Return a well-formed XML string based on SimpleXML element
If the parameter is specified, it returns TRUE if the file was written successfully and FALSE otherwise. ... <?php $string = <<<XML <a> <b> <c>text</c> <c>stuff</c> </b> <d> <c>code</c> </d> </a> XML; $xml = new SimpleXMLElement($string); echo $xml->asXML(); ?>
GeeksforGeeks
geeksforgeeks.org › php › php-simplexml_load_string-function
PHP | simplexml_load_string() Function - GeeksforGeeks
July 11, 2025 - <?php $note=<<<XML <?xml version="1.0" encoding="ISO-8859-1"?> <book> <name>PHP</name> <name>Java</name> <name>C++</name> <name>Python</name> </book> XML; $xml=simplexml_load_string($note); echo $xml->getName() . "\n"; foreach($xml->children() as $child){ echo $child->getName() . ": " . $child . "\n"; } ?> Output: book name : PHP name : Java name : C++ name : Python Reference : https://www.php.net/manual/en/function.simplexml-load-string.php ... Help us improve. Share your suggestions to enhance the article.
Top answer 1 of 3
20
Try with simple XML, here's an example:
do.php:
<?php
$xml_str = file_get_contents('xmlfile.xml');
$xml = new SimpleXMLElement($xml_str);
$items = $xml->xpath('*/item');
foreach($items as $item) {
echo $item['title'], ': ', $item['description'], "\n";
}
xmlfile.xml:
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<items>
<item title="Hello World" description="Hellowing the world.." />
<item title="Hello People" description="greeting people.." />
</items>
</xml>
2 of 3
16
For those situations where SimpleXML is not available, I use this function originally posted in a comment on php.net. It works great 99% of the time.
<?php
/**
* Convert XML to an Array
*
* @param string $XML
* @return array
*/
function XMLtoArray($XML)
{
$xml_parser = xml_parser_create();
xml_parse_into_struct($xml_parser, $XML, $vals);
xml_parser_free($xml_parser);
// wyznaczamy tablice z powtarzajacymi sie tagami na tym samym poziomie
$_tmp='';
foreach ($vals as $xml_elem) {
$x_tag=$xml_elem['tag'];
$x_level=$xml_elem['level'];
$x_type=$xml_elem['type'];
if ($x_level!=1 && $x_type == 'close') {
if (isset($multi_key[$x_tag][$x_level]))
$multi_key[$x_tag][$x_level]=1;
else
$multi_key[$x_tag][$x_level]=0;
}
if ($x_level!=1 && $x_type == 'complete') {
if ($_tmp==$x_tag)
$multi_key[$x_tag][$x_level]=1;
$_tmp=$x_tag;
}
}
// jedziemy po tablicy
foreach ($vals as $xml_elem) {
$x_tag=$xml_elem['tag'];
$x_level=$xml_elem['level'];
$x_type=$xml_elem['type'];
if ($x_type == 'open')
$level[$x_level] = $x_tag;
$start_level = 1;
$php_stmt = '$xml_array';
if ($x_type=='close' && $x_level!=1)
$multi_key[$x_tag][$x_level]++;
while ($start_level < $x_level) {
$php_stmt .= '[$level['.$start_level.']]';
if (isset($multi_key[$level[$start_level]][$start_level]) && $multi_key[$level[$start_level]][$start_level])
$php_stmt .= '['.($multi_key[$level[$start_level]][$start_level]-1).']';
$start_level++;
}
$add='';
if (isset($multi_key[$x_tag][$x_level]) && $multi_key[$x_tag][$x_level] && ($x_type=='open' || $x_type=='complete')) {
if (!isset($multi_key2[$x_tag][$x_level]))
$multi_key2[$x_tag][$x_level]=0;
else
$multi_key2[$x_tag][$x_level]++;
$add='['.$multi_key2[$x_tag][$x_level].']';
}
if (isset($xml_elem['value']) && trim($xml_elem['value'])!='' && !array_key_exists('attributes', $xml_elem)) {
if ($x_type == 'open')
$php_stmt_main=$php_stmt.'[$x_type]'.$add.'[\'content\'] = $xml_elem[\'value\'];';
else
$php_stmt_main=$php_stmt.'[$x_tag]'.$add.' = $xml_elem[\'value\'];';
eval($php_stmt_main);
}
if (array_key_exists('attributes', $xml_elem)) {
if (isset($xml_elem['value'])) {
$php_stmt_main=$php_stmt.'[$x_tag]'.$add.'[\'content\'] = $xml_elem[\'value\'];';
eval($php_stmt_main);
}
foreach ($xml_elem['attributes'] as $key=>$value) {
$php_stmt_att=$php_stmt.'[$x_tag]'.$add.'[$key] = $value;';
eval($php_stmt_att);
}
}
}
return $xml_array;
}
?>
GeeksforGeeks
geeksforgeeks.org › php › php-simplexmlelement-__tostring-function
PHP | SimpleXMLElement __toString() Function - GeeksforGeeks
April 28, 2025 - Below examples illustrate the XMLReader::__toString() function in PHP: ... <?php // Create the XML $xmlstr = <<<XML <?xml version='1.0'?> <library> GeeksforGeeks </library> XML; // Create a new SimpleXMLElement $SXE = new SimpleXMLElement($xmlstr); ...
SitePoint
sitepoint.com › blog › php › parsing xml with simplexml
PHP Master | Parsing XML With SimpleXML
November 7, 2024 - Attributes in an XML element can be accessed as properties of the SimpleXML object. For example, if you have an element · , you can access the title attribute with $book->title. You can convert a SimpleXML object to a string using the asXML() function.
ReqBin
reqbin.com › code › php › r1egt0hd › php-parse-xml-example
How to Parse XML in PHP?
To parse XML in PHP, you can to use the SimpleXML extension. The SimpleXML is an extension allowing us to easily manipulate and retrieve data from XML strings. The SimpleXML turns XML into a data structure that can be iterated over like a collection ...
Tutorialspoint
tutorialspoint.com › php › php_function_simplexml_load_string.htm
PHP simplexml_load_string() Function
<html> <head> <body> <?php $data="<?xml version='1.0' encoding='UTF-8'?> <Employee> <Name>Raju</Name> <Age>25</Age> <Salary>2000</Salary> </Employee>"; $xml = simplexml_load_string($data); print_r($xml); ?> </body> </head> </html> ... Following is another example of this function, in here we are trying to interpret an XML sting which has multiple records −