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?
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>
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());
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!
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());
Related
I just wanted to ask how I can insert a new node in an XML using PHP. my XML file (questions.xml) is given below
<?xml version="1.0" encoding="UTF-8"?>
<Quiz>
<topic text="Preparation for Exam">
<subtopic text="Science" />
<subtopic text="Maths" />
<subtopic text="english" />
</topic>
</Quiz>
I want to add a new "subtopic" with the "text" attribute, that is "geography". How can I do this using PHP? Thanks in advance though.
well my code is
<?php
$xmldoc = new DOMDocument();
$xmldoc->load('questions.xml');
$root = $xmldoc->firstChild;
$newElement = $xmldoc->createElement('subtopic');
$root->appendChild($newElement);
// $newText = $xmldoc->createTextNode('geology');
// $newElement->appendChild($newText);
$xmldoc->save('questions.xml');
?>
I'd use SimpleXML for this. It would look somehow like this:
// Open and parse the XML file
$xml = simplexml_load_file("questions.xml");
// Create a child in the first topic node
$child = $xml->topic[0]->addChild("subtopic");
// Add the text attribute
$child->addAttribute("text", "geography");
You can either display the new XML code with echo or store it in a file.
// Display the new XML code
echo $xml->asXML();
// Store new XML code in questions.xml
$xml->asXML("questions.xml");
The best and safe way is to load your XML document into a PHP DOMDocument object, and then go to your desired node, add a child, and finally save the new version of the XML into a file.
Take a look at the documentation : DOMDocument
Example of code:
// open and load a XML file
$dom = new DomDocument();
$dom->load('your_file.xml');
// Apply some modification
$specificNode = $dom->getElementsByTagName('node_to_catch');
$newSubTopic = $xmldoc->createElement('subtopic');
$newSubTopicText = $xmldoc->createTextNode('geography');
$newSubTopic->appendChild($newSubTopicText);
$specificNode->appendChild($newSubTopic);
// Save the new version of the file
$dom->save('your_file_v2.xml');
You can use PHP's Simple XML. You have to read the file content, add the node with Simple XML and write the content back.
I want to create dynamic tags in XML using PHP
like this : <wsse:Username>fqsuser01</wsse:Username>
the main thing is that I want the tags will change the value inside ---> "wsse"
(like this value)
what I need to do? to create this XML file wite PHP?
Thanks,
For this purpose you can use XMLWriter for example (another option is SimpleXML). Both option are in PHP core so any third party libraries aren't needed. wsse is a namespace - more about them you can read here
I also share with you some example code:
<?php
//create a new xmlwriter object
$xml = new XMLWriter();
//using memory for string output
$xml->openMemory();
//set the indentation to true (if false all the xml will be written on one line)
$xml->setIndent(true);
//create the document tag, you can specify the version and encoding here
$xml->startDocument();
//Create an element
$xml->startElement("root");
//Write to the element
$xml->writeElement("r1:id", "1");
$xml->writeElement("r2:id", "2");
$xml->writeElement("r3:id", "3");
$xml->endElement(); //End the element
//output the xml
echo $xml->outputMemory();
?>
Result:
<?xml version="1.0"?>
<root>
<r1:id>1</r1:id>
<r2:id>2</r2:id>
<r3:id>3</r3:id>
</root>
You could use a string and convert it to XML using simplexml_load_string(). The string must be well formed.
<?php
$usernames= array(
'username01',
'username02',
'username03'
);
$xml_string = '<wsse:Usernames>';
foreach($usernames as $username ){
$xml_string .= "<wsse:Username>$username</wsse:Username>";
}
$xml_string .= '</wsse:Usernames>';
$note=
<<<XML
$xml_string
XML; //backspace this line all the way to the left
$xml=simplexml_load_string($note);
?>
If you wanted to be able to change the namespaces on each XML element you would do something very similar to what is shown above. (Form a string with dynamic namespaces)
The XML portion that I instructed you to backspace all of the way has weird behavior. See https://www.w3schools.com/php/func_simplexml_load_string.asp for an example that you can copy & paste.
While adding child, this error is thrown :
Cannot add child. Parent is not a permanent member of the XML tree.
I cannot resolve this.
This is my code :
if($visited=='FIRST')
{
$xml=new SimpleXMLElement("<xml/>");
$topology=$xml->addChild("Topology_Configuration");
$flavor=$topology->addChild("Flavor");
$networks=$topology->addChild("Networks");
$vms=$topology->addChild("VMs");
$vnfs=$topology->addChild("VNFs");
$xml->asXML('saddening.xml');
}
else
{
$xml= simplexml_load_file('saddening.xml');
$Topology_Configuration = new SimpleXMLElement($xml->asXML());
$vmcount=$_POST['arguments']['vmcount'];
$flavor=$Topology_Configuration->Flavor;
$flavor_name=$flavor->addChild($_POST['arguments']['flavorName']);
$Topology_Configuration->asXML('saddening.xml');
}
When it is executed for the first time, the file is created(in if part). Otherwise else part is executed. It cannot add the child and is throwing the error in line :
$flavor_name=$flavor->addChild($_POST['arguments']['flavorName']);. Please help!!
The XML from your first run results in an XML like this:
<?xml version="1.0"?>
<xml>
<Topology_Configuration>
<Flavor/>
<Networks/>
<VMs/><VNFs/>
</Topology_Configuration>
</xml>
So if you strip down the problem you can reproduce it with:
$Topology_Configuration = simplexml_load_file($fileName);
$flavor=$Topology_Configuration->Flavor;
$flavor->addChild('abc');
echo $Topology_Configuration->asXml();
Results in:
Warning: SimpleXMLElement::addChild(): Cannot add child.
Parent is not a permanent member of the XML tree in
The message is a little wrong, you just try to add the element to an element that does not exists. $Topology_Configuration contains the xml element node, not the Topology_Configuration.
Here are two possible solutions:
Change the XML structure
Create the XML with the Topology_Configuration as the root element.
$topology =new SimpleXMLElement("<Topology_Configuration/>");
Change the access to the Flavor
$xml = simplexml_load_file($fileName);
$flavor=$xml->Topology_Configuration->Flavor;
$flavor->addChild('abc');
At the first time, you can use example to add child nodes
$new_xml = new SimpleXMLElement("<root></root>");
$new_xml->addAttribute('newAttr', 'value');
$newsIntro = $new_xml->addChild('content');
$newsIntro->addAttribute('type', 'value');
Header('Content-type: text/xml');
echo $new_xml->asXML();
and result
<?xml version="1.0"?>
<news newAttr="value">
<content type="value"/>
</news
I have a script in php which creates an XML file.
$xml1 = "<?xml version='1.0' encoding='utf-8'?>\n";
$xml1 .= "\t<invoices>\n";
$xml1 .= "\t\t<journal>\n";
I would need to add a schema link to "invoices" so that the XML output looks like the following:
<invoices xsi:noNamespaceSchemaLocation="http://schema.aptic.net/aptic-link-import-ledgeraccounts-v2.xsd">
Whatever I try I always receive an error and the XML file is not created. How could I solve this or where could I find information on how to insert the required schema link correctly.
What I have tried so far is to add the link with different solutions including ' or "
$xml1 .= "\t<invoices xsi:noNamespaceSchemaLocation="http://schema.aptic.net/aptic-link-import-ledgeraccounts-v2.xsd">\n";
$xml1 .= "\t<invoices 'xsi:noNamespaceSchemaLocation="http://schema.aptic.net/aptic-link-import-ledgeraccounts-v2.xsd"'>\n";
Thank you
ADDON
The following error comes up when opening the created file with your solution:
$xml1 .= "\t<invoices xsi:noNamespaceSchemaLocation=\"http://schema.aptic.net/aptic-link-import-ledgeraccounts-v2.xsd\">\n";
XML Parsing Error: prefix not bound to a namespace
<invoices xsi:noNamespaceSchemaLocation="http://schema.aptic.net/aptic-link-import-ledgeraccounts-v2.xsd">
--------^
SOLUTION:
The following code was the solution for me:
$xml1 = "<?xml version='1.0' encoding='utf-8'?>\n";
$xml1 .= "\t<invoices xmlns='http://www.w3schools.com' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='http://schema.aptic.net/aptic-link-import-ledgeraccounts-v2.xsd'>\n";
$xml1 .= "\t\t<journal>\n";
Do not try to create xml's through string concatenation. You will have a hard time doing this. Use a designated php api for creating xml documents.
Here is a minimal example that will create a invoices.xml.
/* create a dom document with encoding utf8 */
$domtree = new DOMDocument('1.0', 'UTF-8');
/* create the root element of the xml tree */
$xmlRoot = $domtree->createElement("xml");
/* append it to the document created */
$xmlRoot = $domtree->appendChild($xmlRoot);
$invoices = $domtree->createElement("invoices");
$invoices->setAttribute('xsi:noNamespaceSchemaLocation', 'http://schema.aptic.net/aptic-link-import-ledgeraccounts-v2.xsd');
$invoices = $xmlRoot->appendChild($invoices);
$journal = $domtree->createElement("journal");
$journal = $invoices->appendChild($journal);
$domtree->save("invoices.xml");
Content will be:
<?xml version="1.0" encoding="UTF-8"?>
<xml><invoices xsi:noNamespaceSchemaLocation="http://schema.aptic.net/aptic-link-import-ledgeraccounts-v2.xsd"><journal/></invoices></xml>
Addon:
Based on your comment and post edit, I think you just need to quote the " in your string.
$xml1 .= "\t<invoices xsi:noNamespaceSchemaLocation=\"http://schema.aptic.net/aptic-link-import-ledgeraccounts-v2.xsd\">\n";
The attribute like xsi:noNamespaceSchemaLocation uses a namespace prefix. Because of the prefix a assume that it is the namespace http://www.w3.org/2001/XMLSchema-instance.
Namespaces need to be defined, but they will be defined as necessary if you use the namespace aware methods of the DOM API. The end with the suffix NS.
$document = new DOMDocument();
$document->appendChild(
$document->createElement('invoices')
);
$document->documentElement->setAttributeNS(
// namespace
'http://www.w3.org/2001/XMLSchema-instance',
// attribute name including namespace prefix
'xsi:noNamespaceSchemaLocation',
// attribute value
'http://schema.aptic.net/aptic-link-import-ledgeraccounts-v2.xsd'
);
$document->documentElement->appendChild(
$document->createElement('journal')
);
$document->formatOutput = TRUE;
Output:
<?xml version="1.0"?>
<invoices xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schema.aptic.net/aptic-link-import-ledgeraccounts-v2.xsd">
<journal/>
</invoices>
You can see that the generated output includes a xmlns attribute that defines the nampespace used by the xsi prefix.
The XML view in the browsers hides the namespace definitions, often. Check the source to validate that it here.
You can generate the same XML uses string functions. If you do this keep in mind to escape values if needed.
please help.
I have XML file which is not filled correctly (some values are missed)
Bad example of XML
<?xml version="1.0"?>
<csv_data>
<row>
<brand>Maxxis</brand>
<code>1126668/02</code>
<articul_brand></articul_brand>
<tiporazmer></tiporazmer>
<name>(26/9.00 R12) 6PR TL</name>
<model>M-966 Mudzilla</model>
<sklad_msk></sklad_msk>
<sklad_nahabino></sklad_nahabino>
<sklad_chelny>8</sklad_chelny>
<price_big_opt>6,786р.</price_big_opt>
<price_small_opt>7,100р.</price_small_opt>
<price_retail>8,170р.</price_retail>
</row>
</csv_data>
Good example of XML
<?xml version="1.0"?>
<csv_data>
<row>
<brand>Metzeler</brand>
<code>ЦО04257/21</code>
<articul_brand>2491500</articul_brand>
<tiporazmer>110/80R18</tiporazmer>
<name>(110/80 R18) 58W TL (M) Front</name>
<model>Roadtec Z8</model>
<sklad_msk></sklad_msk>
<sklad_nahabino>2</sklad_nahabino>
<sklad_chelny></sklad_chelny>
<price_big_opt>7,720р.</price_big_opt>
<price_small_opt>8,106р.</price_small_opt>
<price_retail>9,270р.</price_retail>
</row>
</csv_data>
The point is that values in <tiporazmer></tiporazmer> are missed in bad example but they can be found in <name>(26/9.00 R12) 6PR TL</name>
The question is how to update fields <tiporazmer></tiporazmer> using name filed? Copy that without brackets? How?
What I did, but i've got an error Fatal error: Call to a member function getElementsByTagName() on a non-object in
Code:
<?php
$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load('test2.xml');
$new_tiporazmer="26/9.00 R12";
//Get item Element
$element = $xml->getElementsByTagName('rows')->item(0);
//Load child elements
$tiporazmer = $element->getElementsByTagName('tiporazmer')->item(0);
//Replace old elements with new
$element->replaceChild($tiporazmer, $new_tiporazmer);
?>
The error is caused because you're calling getElementsByTagName('rows'), but the name of your XML node is row, so there shouldn't be an s in rows
Then in order to Replace old elements with new, you will need to use nodeValue instead of replaceChild unless you already have a constructed DOMDocument object ready.
But a better way to achieve what you want is to simply find the element by name directly, replace its text, and then save as a new file. I saved to the "/tmp" folder because permissions are usually writeable there on unix systems, but you can write anywhere your php interpreter is allowed.
<?php
$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load('test2.xml');
$new_tiporazmer="26/9.00 R12";
//Get item Element and Replace old elements with new
$xml->getElementsByTagName('tiporazmer')->item(0)->nodeValue = $new_tiporazmer;
//Save as new file
$xml->save("/tmp/good.xml");
This results in good.xml:
<?xml version="1.0"?>
<csv_data>
<row>
<brand>Maxxis</brand>
<code>1126668/02</code>
<articul_brand/>
<tiporazmer>26/9.00 R12</tiporazmer>
<name>(26/9.00 R12) 6PR TL</name>
<model>M-966 Mudzilla</model>
<sklad_msk/>
<sklad_nahabino/>
<sklad_chelny>8</sklad_chelny>
<price_big_opt>6,786р.</price_big_opt>
<price_small_opt>7,100р.</price_small_opt>
<price_retail>8,170р.</price_retail>
</row>
</csv_data>