92

Is there any function that makes string from PHP SimpleXMLElement?

Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34
liysd
  • 4,413
  • 13
  • 35
  • 38

8 Answers8

138

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();
74

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'

Nicolay77
  • 2,085
  • 25
  • 20
18

You can use the asXML method as:

<?php

// string to SimpleXMLElement
$xml = new SimpleXMLElement($string);

// make any changes.
....

// convert the SimpleXMLElement back to string.
$newString = $xml->asXML();
?>
codaddict
  • 445,704
  • 82
  • 492
  • 529
17

Actually asXML() converts the string into xml as it name says:

<id>5</id>

This will display normally on a web page but it will cause problems when you matching values with something else.

You may use strip_tags function to get real value of the field like:

$newString = strip_tags($xml->asXML());

PS: if you are working with integers or floating numbers, you need to convert it into integer with intval() or floatval().

$newNumber = intval(strip_tags($xml->asXML()));
tolginho
  • 583
  • 9
  • 12
9

You can use ->child to get a child element named child.

This element will contain the text of the child element.

But if you try var_dump() on that variable, you will see it is not actually a PHP string.

The easiest way around this is to perform a strval(xml->child); That will convert it to an actual PHP string.

This is useful when debugging when looping your XML and using var_dump() to check the result.

So $s = strval($xml->child);.

hakre
  • 193,403
  • 52
  • 435
  • 836
Leander
  • 612
  • 7
  • 8
6

Here is a function I wrote to solve this issue (assuming tag has no attributes). This function will keep HTML formatting in the node:

function getAsXMLContent($xmlElement)
{
  $content=$xmlElement->asXML();

  $end=strpos($content,'>');
  if ($end!==false)
  {
    $tag=substr($content, 1, $end-1);

    return str_replace(array('<'.$tag.'>', '</'.$tag.'>'), '', $content);
  }
  else
    return '';
}


$string = "<element><child>Hello World</child></element>";
$xml = new SimpleXMLElement($string);

echo getAsXMLContent($xml->child); // prints Hello World
paralaks
  • 183
  • 2
  • 7
2

Sometimes you can simply typecast:

// this is the value of my $xml
object(SimpleXMLElement)#10227 (1) {
  [0]=>
  string(2) "en"
}

$s = (string) $xml; // returns "en";
Luca Reghellin
  • 7,426
  • 12
  • 73
  • 118
  • 2
    This is the kind of object you get for the description field of an RSS feed. It's counter-intuitive that it works, but it was the only solution here that gave me the string inside `$xmlitem->description`. Thanks. – Henrik Erlandsson Aug 08 '19 at 09:12
1

Probably depending on the XML feed you may/may not need to use __toString(); I had to use the __toString() otherwise it is returning the string inside an SimpleXMLElement. Maybe I need to drill down the object further ...

halfer
  • 19,824
  • 17
  • 99
  • 186
Keenan Stewart
  • 594
  • 6
  • 9