Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SimpleXml to string

Tags:

php

xml

simplexml

Is there any function that makes string from PHP SimpleXMLElement?

like image 734
liysd Avatar asked Sep 11 '10 12:09

liysd


People also ask

What is simplexml_ load_ string in PHP?

The simplexml_load_string() function converts a well-formed XML string into an object.

Which of the following method can be used to parse an XML document using PHP?

The PHP simplexml_load_string() function is used to read XML data from a string.


2 Answers

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(); 
like image 164
Tim Cooper Avatar answered Sep 20 '22 15:09

Tim Cooper


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'

like image 43
Nicolay77 Avatar answered Sep 18 '22 15:09

Nicolay77