Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

theProfit's avatar

simplexml_load_string only shows first line

I have a file https://tmpfiles.org/dl/951513/request_inspector_item_update.xml and when i parse it i get to see only the first line the xml is valid, so what can it be?

  $xml_data = $request->getContent();
  $xml=simplexml_load_string($xml_data);

this is the response:

SimpleXMLElement {#765
  +"@attributes": array:2 [
    "internalId" => "18818"
    "externalId" => "1.590072"
  ]
}
0 likes
15 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Pretty sure it still loads it. You just need to parse the tree using the methods availble on the returned object

For instance, a quite check on the namespaces

dd(simplexml_load_string($xml_data)->getNamespaces(true));

You could try specifying that namespace when loading

simplexml_load_string($xml_data, "SimpleXMLElement", 0, "ns1", true);
theProfit's avatar

Thanks @Sinnbeck is there also a way to get only the AssemblyItem from the first namescape this so i can verify i get the right XML.

Sinnbeck's avatar

@theProfit I honestly cant really remember how to work with xml in php. I havent done so in years. But I can highly recommend using a package that makes parsing it a bit easier. The simplexml_load_string() class is a bit tricky to work with

Sinnbeck's avatar

@theProfit I would probably go with the package that @mohamedtammam suggested. It is made by Crynobone who is an awesome developer (He made testbench for laravel)

theProfit's avatar

Thanks @Sinnbeck last question:


       Storage::disk('public')->put($name,$xml_data);
        $xml = XmlParser::load('/storage/app/public/'.$name);

i have this code but i think my storage path is not correct do you know something to get the right location now i must first store the request i am getting store it then read it again.

Sinnbeck's avatar

@theProfit personally I use the storage facade to get the path

$path = Storage::disk('public')->path($name);
theProfit's avatar

Thanks @Sinnbeck i am getting the following error

[2023-02-21 11:36:52] local.ERROR: Symfony\Component\HttpFoundation\Response::setContent(): Argument #1 ($content) must be of type ?string, Orchestra\Parser\Xml\Document given

this is the code:

        $xml_data = $request->getContent();
        $name = rand(0000, 9999) . '.xml';

        Storage::disk('public')->put($name, $xml_data);
        $path = Storage::disk('public')->path($name);
        $xml = XmlParser::load($path);

        return $xml;
Sinnbeck's avatar

@theProfit Then perhaps your problem is that you try to return the $xml variable as a response?

theProfit's avatar

@Sinnbeck Yes only when i now do a die dump i get this --> https://prnt.sc/s60f54otKr3y it is also showing only first 2 lines and the other solution is also not showing all the lines in the xml so i need a solution to read the xml.

Sinnbeck's avatar

@theProfit But why do you need to see more? Does it matter? I assume you want to parse the xml, not just view it?

LaryAI's avatar
Level 58

It looks like you are only getting the first line of the XML file because you are not looping through the XML file. You can use a foreach loop to loop through the XML file and get all the data.

$xml_data = $request->getContent();
$xml=simplexml_load_string($xml_data);

foreach($xml as $item) {
    echo $item->internalId;
    echo $item->externalId;
}

This should loop through the XML file and print out the internalId and externalId for each item.

Please or to participate in this conversation.