0

So, what i am trying to achieve is to load variables from PHP into an XML file.

this is how my XML looks at the moment:

<?xml version="1.0" encoding="ISO-8859-1"?>
<firstname></firstname>
<lastname></lastname>

And this is my PHP where i try to save the variables into the XML

        $file = simplexml_load_file("filename.xml");

        $xml->firstname = "Mark";

        $xml->lastname = "Zuckerberg";

        file_put_contents($file, $xml->asXML());

If i try to print this i get following error message:

Call to undefined method stdClass::asXML() in ... on line 1374

Any suggestions?

1
  • $file = ... + $xml->asXML() Commented Jun 21, 2016 at 14:18

4 Answers 4

1

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>
Sign up to request clarification or add additional context in comments.

3 Comments

Which gives me a "Class 'DOMDocument' not found in TestController.php on line 1360"
@MarcelWasilewski What system do you use? and if Linux, what distro?
You need to install it, if you have Ubuntu or Debian, execute sudo apt-get install php5-dom in your system terminal, if you have Red Hat/Fedora/Cent OS you need yum install php-xml
1

Enable error reporting (like error_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());

Comments

0

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, your file_put_contents() command is incorrect. The first parameter is accepts is the filename, however in your example $file is not a name but a simplexml object.

file_put_contents("path/to/file.xml", $xml->asXML());

Alternatively, use the asXML() 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!

2 Comments

instead of the file_put_contents call he could use $xml->asXML('path/to/file.xml');
I have done it like you mentioned above, still getting the same Error message.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.