Hello, everyone. I'm newbie in PHP. I have a little problem. I have an existing XML file, that has some data. First let me show the XML Structure(it's an example):
Ok, what I want is add new data to the XML file, add data to the First_Name and Last_Name Child, but using PHP. I want that the php add the first name and last name inside a person label and the every person label must be inside the people label.
I hope you understand. Any suggestion will be appreciated.
Well, I have been looking for a solution in the page of SimpleXML in php.net. But I have no find how to add new data to a XML file. Any suggestion will be appreciated.
Hey Znupi, Thanks. The solution was there in PHP.net in SimpleXML functions. The way that you can add data to a XML file. But for readers let me explain:
This is the XML example that I going to use called "xml01.xml":
Now, what I want is to add another person to the XML file. Now I going to explained by two ways, by SimpleXML functions and by DomDocuments functions in PHP.
This is the SimpleXML way to add data:
PHP Code:
$xml = simplexml_load_file("xml01.xml"); //This line will load the XML file.
$sxe = new SimpleXMLElement($xml->asXML()); //In this line it create a SimpleXMLElement object with the source of the XML file.
//The following lines will add a new child and others child inside the previous child created.
$person = $sxe->addChild("person");
$person->addChild("first_name", "Nairoby");
$person->addChild("last_name", "Del Rosario");
//This next line will overwrite the original XML file with new data added
$sxe->asXML("xml01.xml");
Now when you see the XML file it will have added the next data:
Anybody know how to add new lines between entries and entry fields using SimpleXML or DOM? I mean, instead of like this:
<person><first_name>Nairoby</first_name><last_name>Del Rosario</last_name></person></people>
to make it like this:
<person>
<first_name>Nairoby</first_name>
<last_name>Del Rosario</last_name>
</person>
</people>
without manually editing the xml file, of course.
<?php
$xml = simplexml_load_file("xml01.xml"); //This line will load the XML file.
$sxe = new SimpleXMLElement($xml->asXML()); //In this line it create a SimpleXMLElement object with the source of the XML file.
//The following lines will add a new child and others child inside the previous child created.
$person = $sxe->addChild("person");
$person->addChild("first_name", "Nairoby");
$person->addChild("last_name", "Del Rosario");
//This next line will overwrite the original XML file with new data added
$sxe->asXML("xml01.xml");
?>
Hello, I have the same php version like you, but running locally in my pc, and it runs great. By the message that you receive means that the simplexml_load_file function is disabled by the server manager or disable in the php configuration. Some providers of web hosting disable some functions and options for security. So you have to contact your administrator for a solution.
Now, the simplexml_load_file function works and it is in the php site. And have a good documention.
If you need something else or more information, just post it.
Thanks
@metalx: are you running PHP 5? (SimpleXML is not available in PHP4.)
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Hi, i'm a novice in php and i need create a form where the user can add an image to an existing xml file and after save this into a data base that the xml file gets edited and that the client can see the changes in the gallery. Please any help will be appreciate. i need to create a database, put the info into it throught a form of an existing image that wants to be added to this dinamic gallery.
<item image="flash/banner_images/eleva_tu_poder.jpg" link="http://www.fokoconsulting.net/grupos.php?id_c=37&id_gp=131&id_g=136">
<![CDATA[<b><font size='20'>PONTE EN ACCIÓN Y ELEVA TU PODER</font><BR>11 Noviembre, 2010<BR>Hotel Jaragua</b>]]>
</item>
How can you update the text of an XML element in PHP
Originally Posted by theboss_edgar
Hello, everyone. I'm newbie in PHP. I have a little problem. I have an existing XML file, that has some data. First let me show the XML Structure(it's an example):
Ok, what I want is add new data to the XML file, add data to the First_Name and Last_Name Child, but using PHP. I want that the php add the first name and last name inside a person label and the every person label must be inside the people label.
I hope you understand. Any suggestion will be appreciated.
Thanks.
Okay, "theboss_edgar". I understand your solution to this problem further below. I have a related question. Suppose, instead of adding a new name you want to change an existing name. Let's say that "Edgar Polanco" wants to change his name to "Edwin Poland". How would you do that?
Anybody know how to add new lines between entries and entry fields using SimpleXML or DOM? I mean, instead of like this:
<person><first_name>Nairoby</first_name><last_name>Del Rosario</last_name></person></people>
to make it like this:
<person>
<first_name>Nairoby</first_name>
<last_name>Del Rosario</last_name>
</person>
</people>
without manually editing the xml file, of course.
Im in the same problem which is that the new items added to the XML are a sinle long line without any spaces.
Hope anyone could know a way to add this indent spaces to the XML.
Greetings.
Bookmarks