I've having issue parsing an XML document with PHP and DOMDocument. Given the following XML:
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item>
<id>1</id>
<name>Example</name>
</item>
...
</items>
I can't seem to parse it correctly. Any attempts at returning a nodeValue
from an XMLNode
returns:
"""
\n
\t\t -> repeated for the amount of indentation
"""
Here is the method I'm using to get my XML. I upload an XML file:
<form enctype="multipart/form-data" method="post">
<input type="file" name="xml_file"/>
</form>
I access the file via Laravel:
$dom = new \DOMDocument;
$xml_doc = $request->file("xml_file");
$xml = file_get_contents($xml_doc);
$dom->loadXML($xml);
$items = $dom->getElementsByTagName("item");
foreach($items AS $item){
$id = $item->firstChild;
dd($id->nodeValue);
}
There seems to be a problem with the contents of file_get_contents($xml_doc)
, as dd($xml)
returns this:
"""
<items>\r\n
\t<item>\r\n
\t\t<id>1</id>\r\n
\t\t<name>Example</name>\r\n
\t</item>\r\n
</items>
"""
I cannot figure out why my PHP is adding (or not removing?) the \n
, \r
and \t
characters from my XML, or where the """
at the top and bottom of the document are coming from. Can anyone shed any light on what is happening?
Also, dd()
is shorthand for die(var_dump($var));
, in Laravel known as Dump and Die
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire