Midified the xml file using DomDocument object? - php

Midified the xml file using DomDocument object?
Have a file.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Document xmlns="urn:iso:std">
<content>
<GrpHdr>
<MsgId>xxxxxx </MsgId>
<CreDtTm>2010-07-05T16:48:00</CreDtTm>
<BtchBookg>false</BtchBookg>
<NbOfTxs>2</NbOfTxs>
<CtrlSum>56653</CtrlSum>
<Grpg>Old</Grpg>
<InitgPty>
<Nm> Name </Nm>
</InitgPty>
</GrpHdr>
</content>
<Document>
How can modified the xml data and save back to file.xml (for example )
<NbOfTxs>1</NbOfTxs>
<CtrlSum>1223</CtrlSum>
<Grpg>NEW</Grpg>

Use save() function of the DomDocument Object, for example:
$dom->save("newfile.xml")
Look here for further details

Related

How to add a child via PHP in xml string with soap tag

Im trying to add a Child in articoli node but when i execute the code i receive this error, what I'm doing wrong?
SimpleXMLElement::addChild(): Cannot add child. Parent is not a
permanent member of the XML tree
<?php
$note='<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<AddPrenotazione>
<sessionId>6355aa2c-21f5-4436-8fef-24f0211bbd86</sessionId>
<guid>4353999998999999</guid>
<articoli>
</articoli>
</AddPrenotazione>
</soap12:Body>
</soap12:Envelope>';
$xml = simplexml_load_string($note);
$xml->children('soap12', true)->Body->articoli->addChild("ArticoloPrenotazione","");
echo $xml->asXML();
?>
Your structure of the XML when setting the value needs a couple of extra parts...
$xml->children('soap12', true)->Body->children()
->AddPrenotazione->articoli->addChild("ArticoloPrenotazione","");
You missed out the AddPrenotazione level, but as this is in a different namespaces to the Body element you also need to use children() to fetch the elements from the default namespace.

How to add an xslt stylesheet in php using SimpleXmlElement to an existing xml document?

I have this xml document in a file called text.xml
The xml document currently looks like this:
<?xml version="1.0" encoding="UTF-8"?>
......
<rss version="2.0"...>
Right after the xml version declaration, I want to add the line:
<?xml-stylesheet type="text/xml" href="style.xslt"?>
In more detail this is what I want to do:
I first want to convert the string contents of the file to an xml document.
$mystr = file_get_contents(path to my text.xml file);
$myxml = new SimpleXmlElement($mystr);
I then want to add an attribute to my XML document to link an XSLT stylesheet.
$myxml->addAttribute(_____);
using the SimpleXmlElement addAttribute method, but the addAttribute method allows for the name, value, and namespace--How would I even use the namespace? Since I have three parameters it makes the most sense for the href to be in the namespace parameter spot.
So I have tried to fill in the blank with 'xml-stylesheet','text/xml','style.xslt' which doesn't work because I am getting the error that "Attribute requires prefix for namespace". How do I get this to work?
Link to php docs: http://php.net/manual/en/simplexmlelement.addattribute.php
Edit: This is different from the other question because I am trying to link an xslt stylesheet to an rss document. Using the "duplicate questions'" solution causes the xslt stylesheet to be linked after the rss version declaration which is not good enough.
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"...>
<?xml-stylesheet type="text/xml" href="test.xslt"?>
....
Instead I want:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xml" href="test.xslt"?>
<rss version="2.0"...>
....

Using unset() to delete node in XML; PHP/simplexml_load_file

I'm trying to use unset() to delete a node in an XML using PHP and can't figure out what is going on here. It doesn't seem to work correctly and I've seen a lot of other questions of similar nature on here but they don't seem to address this issue directly. Here's what my XML looks like:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<user>
<name>Test Name 1</name>
<email>test#test.com</email>
<spouse/>
</user>
<user>
<name>Test Name 2</name>
<email>anotherone#test.com</email>
<spouse>Test Name 3</spouse>
</user>
</data>
My loop that I'm using is like this:
url = 'data/users.xml';
$xml = simplexml_load_file($url);
foreach($xml->user as $theUser){
if($theUser->email[0]=="test#test.com"){
echo "test";
unset($theUser);
}
}
When the e-mail matches "test#test.com" I want to be able to delete that whole user node. It seems that this should work but I can't figure out why it wouldn't? Any help would be greatly appreciated. Thank you!
SimpleXML is fine, no need to switch to DOM, unset() is working fine, if you do it right:
unset($theUser[0]);
see it working: https://eval.in/228773
However there will be a problem with your foreach() if you delete a node mid-loop.
I suggest to use xpath() instead of a loop, IMO elegant and the code is much simpler.
$users = $xml->xpath("/data/user[email='test#test.com']");
will create an array of all <user> with that email-address.
unset($users[0][0]);
will delete the first user in that array.
foreach ($users as $user) unset($user[0]);
will delete the whole array.
see this in action: https://eval.in/228779
SimpeXML is not really meant for changing to the XML structure. Just a simple way of reading the XML.
If you want to manipulate the XML structure you should use the dom functions and more specifically the dom_import_simplexml. This function allows you to import a SimpleXML element and turn it into a DomElement that can be used for manipulation and that includes deletion.
Here is a code sample that solves your problem and demonstrates the usage of dom_import_simplexml.
<?php
$xmlData = '<?xml version="1.0" encoding="UTF-8"?>
<data>
<user>
<name>Test Name 1</name>
<email>test#test.com</email>
<spouse/>
</user>
<user>
<name>Test Name 2</name>
<email>anotherone#test.com</email>
<spouse>Test Name 3</spouse>
</user>
</data>';
$xml = simplexml_load_string($xmlData);
foreach($xml->user as $theUser){
if($theUser->email == 'test#test.com'){
$dom = dom_import_simplexml($theUser);
$dom->parentNode->removeChild($dom);
}
}
echo $xml->asXml();
When reading this code you might be thinking why this works since we dont save the new structure anywhere after we have executed the removeChild function. This works because the DOM functions does not create copies of the underlying objects but instead manipulates them directly.
Result
<?xml version="1.0" encoding="UTF-8"?>
<data>
<user>
<name>Test Name 2</name>
<email>anotherone#test.com</email>
<spouse>Test Name 3</spouse>
</user>
</data>

PHP SimpleXML - Node as string

Im trying to get a XML node (with children) as string but had no success.
Here my xml
<?xml version="1.0" encoding="UTF-8"?>
<params>
<main_settings>
<id>23</account_id>
<test/>
<logo_url/>
<app appid="test_12">
<d:tag xmlns:c="http://mypage.com/test2" id="mypage_id_256" type="mypage_type_test">
<path-info value="show"/>
</d:tag>
</app>
</main_settings>
</params>
This is not working.
$var->params->main_settings->app;
Is there a way to get with simpleXML a nodes ("app") childs as sting?
You can call the asXml method on the child:
$var->params->main_settings->app->asXml();
It's also possible via typecasting:
(string)$var->params->main_settings->app

Insert node in xml using php

I want to insert text node and create element in xml using php
for example
XML
<?xml version="1.0"?>
<employees>
<employee>
<name>Albert</name>
<age>34</age>
<salary>$10000</salary>
</employee>
<employee>
<name>Claud</name>
<age>20</age>
<salary>$2000</salary>
</employee>
</employees>
I want insert Data for one more employees using php.
Regards
NewBie
<?php
$xml = simplexml_load_file('clients.xml');
$employee = $xml->addChild('employee');
$employee->addChild('name', 'Claud');
$employee->addChild('age', '20');
$employee->addChild('salary', 'This is all about the people who make it work.');
file_put_contents('clients.xml', $xml->asXML());
See the DOMDocument class documentation. There are examples for XML parsing and modifying.

Categories