Load PHP variables into XML file
Get the solution ↓↓↓Solution:
You don't create the initial XML file, the library you are using creates it for you.
XML DOM is a fine choice for this job.
$xml = new DOMDocument(); # Create a document
$xml_firstname = $xml->createElement("firstname", "Over"); # Create an element
$xml_lastname = $xml->createElement("lastname", "Coder"); # Create an element
$xml->appendChild($xml_firstname); # Add the element to the document
$xml->appendChild($xml_lastname); # Add the element to the document
$xml->save("myfancy.xml"); # Save the document to a file
The output would be
<?xml version="1.0" encoding="utf-8"?>
<firstname>Over</firstname>
<lastname>Coder</lastname>
Answer
Solution:
Enable error reporting (likeerror_reporting( E_ALL );
) and you will understand quickly why it does not work:
Warning: simplexml_load_file(): xml.xml:3: parser error : Extra content at the end of the document
// your XML is not correctly formatted (XML requires a root node)
Warning: Creating default object from empty value
// $xml->firstname when $xml does not exists
To resolve that, your XML should looks like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<data><!-- here comes the root node -->
<firstname></firstname>
<lastname></lastname>
</data>
And the PHP should looks like previous answers:
$xml = simplexml_load_file("filename.xml");
$xml->firstname = "Mark";
$xml->lastname = "Zuckerberg";
file_put_contents("filename_copy.xml", $xml->asXML());
Answer
Solution:
First off: where do you establish$xml
?
You start with$file = ...
but then refer to the object as$xml
.
Either change the object name to$xml
or change the references to$file
:
$xml = simplexml_load_file("filename.xml"); /* note the object name change */
$xml->firstname = "Mark";
$xml->lastname = "Zuckerberg";
Next, yourfile_put_contents()
command is incorrect. The first parameter is accepts is the filename, however in your example$file
is not a name but asimplexml
object.
file_put_contents("path/to/file.xml", $xml->asXML());
Alternatively, use theasXML()
method with the path by doing this (thanks bassxzero):
$xml->asXML("path/to/file.xml");
Finally, your script is outputting the error:
Call to undefined method stdClass::asXML()
This implies that you can't call$xml->axXML()
as (I'm assuming) either the method doesn't exist, or the object doesn't have the correct method.
Changing the name of the object initially (the first issue) should fix this!
Answer
Solution:
From the code, you load the XML into $file. But you edit $xml. The below code should work
$xml = simplexml_load_file("filename.xml");
$xml->firstname = "Mark";
$xml->lastname = "Zuckerberg";
file_put_contents("output.xml", $xml->asXML());
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: method illuminate\database\eloquent\collection::paginate does not exist.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.