The var_dump() output is correct. $xml->StatusCode is a SimpleXMLElement instance. This is of course needed in case you have to, for example, add a child element to it:
$xml->StatusCode->addChild("test", "value");
If $xml->StatusCode contained only the value of the element rather than an instance of SimpleXMLElement, you wouldn't be able to do any modifications on the loaded XML.
So, what you need to do, is cast the value of StatusCode to a string. There are various ways of doing this:
var_dump($xml->StatusCode); // returns SimpleXMLElement instance
var_dump((string)$xml->StatusCode); // explicitly
var_dump($xml->StatusCode->__toString()); // explicitly, calling the magic function
echo $xml->StatusCode; // implicitly
Some demos
Answer from ishegg on Stack OverflowThe var_dump() output is correct. $xml->StatusCode is a SimpleXMLElement instance. This is of course needed in case you have to, for example, add a child element to it:
$xml->StatusCode->addChild("test", "value");
If $xml->StatusCode contained only the value of the element rather than an instance of SimpleXMLElement, you wouldn't be able to do any modifications on the loaded XML.
So, what you need to do, is cast the value of StatusCode to a string. There are various ways of doing this:
var_dump($xml->StatusCode); // returns SimpleXMLElement instance
var_dump((string)$xml->StatusCode); // explicitly
var_dump($xml->StatusCode->__toString()); // explicitly, calling the magic function
echo $xml->StatusCode; // implicitly
Some demos
/**
* @param $xmlObject
* @param array $out
* @return array
*/
private function simpleXmlToArray($xmlObject, $out = array ())
{
foreach ($xmlObject as $index => $node ){
if(count($node) === 0){
$out[$node->getName()] = $node->__toString ();
}else{
$out[$node->getName()][] = $this->simpleXmlToArray($node);
}
}
return $out;
}
check the version of installed php using:-
$ php -v
If it's version 7, then install php7.0-xml
if it's version 7.2.*, then use php7.2-xml package on your machine along with php.
Just install php-xml package.For reference, just install all the additional php libraries:
apt-get install php-pear php7.2-curl php7.2-dev php7.2-gd php7.2-mbstring php7.2-zip php7.2-mysql php7.2-xml
Converting the XML into JSON that way means loosing data. I suggest keeping the XML if possible.
SimpleXMLElement::asXML() is a method. Do not forget the brackets.
$parent = $this->getParent($keywords);
foreach ($parent->children() as $child) {
dispatch(new ProcessChild($child->asXML(), true), $this->otherVar);
}
Calling it as a property means that SimpleXML tries to interpret it as a child element node. This means it will be an (empty) SimpleXMLElement.
Here is a small example showing the behavior:
$node = new SimpleXMLElement('<foo/>');
var_dump($node->asXml);
var_dump($node->asXml->getName());
var_dump($node->asXml());
Output:
object(SimpleXMLElement)#2 (0) {
}
string(0) ""
string(29) "<?xml version="1.0"?>
<foo/>
"
The SimpleXmlElement can be converted to array as follows:
$xml = <<<'XML'
<root>
<x a="a1">1</x>
<y b="b2">2</y>
<z>3</z>
</root>
XML;
$xe = simplexml_load_string($xml);
$a = $xe->xpath('*');
$a = array_map(function ($e) {
$item = (array) $e;
$item['nodeName'] = $e->getName();
return $item;
}, $a);
// Now `$a` is an array (serializable object)
echo json_encode($a, JSON_PRETTY_PRINT);
Output
[
{
"@attributes": {
"a": "a1"
},
"0": "1",
"nodeName": "x"
},
{
"@attributes": {
"b": "b2"
},
"0": "2",
"nodeName": "y"
},
{
"0": "3",
"nodeName": "z"
}
]
Note, you can get the string value of a SimpleXmlElement by casting it to string:
$item['value'] = (string) $e;
Since xpath method supports relative XPath expressions, the asterisk should work even with namespaced XMLs. Consider using the DOM extension, as it is much more flexible than SimpleXML. In particular, its DOMXPath class allows to register namespaces and use the registered identifiers in the XPath expressions:
$xpath->registerNamespace('myprefix', 'http://example.com/ns');
$xpath->query('/root/myprefix:*');
Assuming that the content of xml is in $xml variable, you can use one of the followings:
return response($xml, 200)->header('Content-Type', 'application/xml');
OR
return response($xml, 200, ['Content-Type' => 'application/xml']);
In your view create sitemap.blade.php.
Insert content xml in your sitemap.blade.php.
Create a function called sistemap.
public function sitemap(){
return response()->view('sitemap')->header('Content-Type', 'text/xml');
}
In your routes:
Route::get('sitemap', function () {
return view('sitemap');
});