www.webdeveloper.com
Results 1 to 14 of 14

Thread: Editing XML using PHP

  1. #1
    Join Date
    Jun 2007
    Posts
    20

    Editing XML using PHP

    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):
    HTML Code:
    <?xml version="1.0" encoding="iso-8859-1"?>
    <people>
    <person>
    <First_Name>Edgar</First_Name>
    <Last_Name>Polanco</Last_Name>
    </person>
    <person>
    <First_Name>Nairoby</First_Name>
    <Last_Name>Del Rosario</Last_Name>
    </person>
    </people>
    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.

  2. #2
    Join Date
    Sep 2006
    Location
    Bucharest, RO
    Posts
    940
    If you're using PHP 5+ you'd probably want to look into SimpleXML functions

  3. #3
    Join Date
    Jun 2007
    Posts
    20
    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.

    Thanks

  4. #4
    Join Date
    Sep 2006
    Location
    Bucharest, RO
    Posts
    940

  5. #5
    Join Date
    Jun 2007
    Posts
    20
    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":
    HTML Code:
    <?xml version="1.0" encoding="iso-8859-1"?>
    <people>
    <person>
    <first_name>Edgar</first_name>
    <last_name>Polanco</last_name>
    </person>
    </people>
    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:
    HTML Code:
    <?xml version="1.0" encoding="iso-8859-1"?>
    <people>
    <person>
    <first_name>Edgar</first_name>
    <last_name>Polanco</last_name>
    </person>
    <person><first_name>Nairoby</first_name><last_name>Del Rosario</last_name></person></people>
    I'll post how to do the same by the DOM Functions in PHP later.
    Thanks

  6. #6
    Join Date
    Jan 2008
    Posts
    1
    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.

  7. #7
    Join Date
    Sep 2008
    Posts
    1

    Exclamation Problem

    Hello
    I did as the example shows step by step..
    however each time I try it an error occurs:

    Fatal error: Call to undefined function: simplexml_load_file() in /home/metalx/public_html/test/test2.php on line 2

    here is the built page :
    http://www.themetalx.com/test/test2.php


    I have PHP version 5.2.5

    and here are the files i wrote:

    The php file:
    <?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");
    ?>
    the xml file

    <?xml version="1.0" encoding="iso-8859-1"?>
    <people>
    <person>
    <first_name>Edgar</first_name>
    <last_name>Polanco</last_name>
    </person>
    </people>
    I need a solution..Anyone

  8. #8
    Join Date
    Jun 2007
    Posts
    20

    It's in your server or php configuration

    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

  9. #9
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,633
    @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

    eBookworm.us

  10. #10
    Join Date
    Jun 2010
    Posts
    1
    its all in the php.ini file, the configuration file, you just have to enable some features which are commented out

  11. #11
    Join Date
    Jun 2010
    Posts
    1

    Unhappy Adding info to an existin XML file with PHP

    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.

    This is mi data.xml file:

    <?xml version="1.0" encoding="utf-8" ?>

    <Banner
    bannerWidth=""
    bannerHeight=""
    bannerBackgroundColor=""
    textSize=""
    textColor=""
    textAreaWidth="190"
    textLineSpacing="0"
    textLetterSpacing="-0.5"
    textMarginLeft="686"
    textMarginBottom="0"
    textBackgroundBlur="false"
    textBackgroundTransparency="10"
    transitionType="5"
    transitionDelayTimeFixed="2"
    transitionDelayTimePerWord=".5"
    transitionSpeed="8"
    transitionBlur="yes"
    showTimerClock="yes"
    showNextButton="yes"
    showBackButton="yes"
    showNumberButtons="yes"
    showNumberButtonsAlways="yes"
    showNumberButtonsHorizontal="yes"
    showNumberButtonsAscending="yes"
    showPlayPauseOnTimer="yes"
    alignButtonsLeft="no"
    alignTextTop="no"
    autoPlay="yes"
    imageResizeToFit="yes"
    imageRandomizeOrder="no"

    >
    <item image="flash/banner_images/john_tschohl.jpg" link="http://www.fokoconsulting.net/grupos.php?id_c=37&id_gp=62&id_g=126">
    <![CDATA[<b><font size='20'>LEALES POR SIEMPRE</font><BR>1 Septiembre, 2010<BR>Hotel Jaragua</b>]]>
    </item>



    <item image="flash/banner_images/rapidez.jpg" link="http://www.fokoconsulting.net/grupos.php?id_c=37&id_gp=131&id_g=129">
    <![CDATA[<b><font size='20'>RAPIDEZ</font><BR>3 Agosto, 2010<BR>Hotel Jaragua</b>]]>
    </item>


    <item image="flash/banner_images/sentimientos.jpg" link="http://www.fokoconsulting.net/grupos.php?id_c=37&id_gp=62&id_g=130">
    <![CDATA[<b><font size='20'>SENTIMIENTOS</font><BR>23 Junio, 2010<BR>Hotel Jaragua</b>]]>
    </item>


    <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>

    </Banner>

  12. #12
    Join Date
    Dec 2011
    Posts
    1

    How can you update the text of an XML element in PHP

    Quote Originally Posted by theboss_edgar View Post
    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):
    HTML Code:
    <?xml version="1.0" encoding="iso-8859-1"?>
    <people>
    <person>
    <First_Name>Edgar</First_Name>
    <Last_Name>Polanco</Last_Name>
    </person>
    <person>
    <First_Name>Nairoby</First_Name>
    <Last_Name>Del Rosario</Last_Name>
    </person>
    </people>
    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?

  13. #13
    Join Date
    Jul 2012
    Posts
    1

    Online xml editor in php

    if anyone is interested here is an xmleditor that allows you to manage xml files online via a CMS:

    http://x-presto.com/demo/axe

    Virtually transforms any simple .xml file in a database administration interface

  14. #14
    Join Date
    Sep 2008
    Posts
    6

    Same question here

    Quote Originally Posted by gagauza View Post
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles