Advanced Search

Results 1 to 5 of 5

Thread: How to retrieve the data from PHP to XML?

  1. #1
    Join Date
    Mar 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How to retrieve the data from PHP to XML?

    Hello everyone,

    Im new to PHP, however I do have some experience with XML.

    I need to create XML file where the data will be retrieved from the PHP. What is the best way to do it?

    For example:
    <listOfSpecies>
    <species id="N1" name="ATP" />
    <species id="N2" name="ADP"/>
    <species id="N3" name="H"/>
    </listOfSpecies>
    (IDs and names has been retrieved from the php)

    Thanks. NewCarol

  2. #2
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    EDIT:

    I think I read your post too quickly. You want to CREATE the XML file? Or do already have the XML file and you need to get information out of it?

    If you want to create an XML file, let me know how you are getting your data. From a database? Post an example of the data you have.

    END EDIT.

    I am glad there have been some questions about XML on here lately. I am getting a lot of practice with simpleXML.

    species.xml

    Code:
    <?xml version="1.0" ?>
    <listOfSpecies>
    	<species id="N1" name="ATP" />
    	<species id="N2" name="ADP"/>
    	<species id="N3" name="H"/>
    </listOfSpecies>
    yourfile.php

    PHP Code:
    <?php

    $xml 
    simplexml_load_file('species.xml');

    foreach ( 
    $xml as $species ) {
        
        echo 
    'ID: ' $species->attributes()->id '<br />';
        echo 
    'Name: ' $species->attributes()->name '<br />';
        
    }

    ?>

  3. #3
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    This will create a XML file:

    PHP Code:
    <?php

    // $rows is just sample data
    // You'll have to gather up your data here:
    $rows = array();

    $rows[] = array( 'id' => 'N1''name' => 'ATP');
    $rows[] = array( 'id' => 'N2''name' => 'ADP');
    $rows[] = array( 'id' => 'N3''name' => 'H');

    $filefopen("list_of_species.xml""w");

    $xml '<?xml version="1.0" encoding="UTF-8" ?>' "\r\n";

    $xml .= "<listOfSpecies>\r\n";

    foreach (
    $rows as $row) {

        
    $xml .= '<species id="' $row['id'] . '" name="' $row['name'] . '" />' "\r\n";

    }

    $xml .= "</listOfSpecies>";

    fwrite($file$xml);

    fclose($file);

  4. #4
    Join Date
    Mar 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    JasonDFR,
    Thanks a lot for your reply.

    I do have the xml file. Im using php to access the database. The outcome has to be in xml format.

    The example that I put in my first post - is the actual outcome (which im trying to reproduce)
    (ps: i can send you example of the data by email)

    NewCarol

  5. #5
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    You are saying you have the XML file? That is what confused me. I think you want to create an XML file, right?

    If so, my second post should work then. It starts with nothing and builds an XML file.

    Look at my second post.

    Query your DB where I have the $rows array, get the $result, then in the code below, replace:

    PHP Code:
    // replace this with your db specific code

    foreach ($rows as $row) {

        
    $xml .= '<species id="' $row['id'] . '" name="' $row['name'] . '" />' "\r\n";


    depending on your database, with something like:

    PHP Code:
    while ( $row mysqli_fetch_array($result) ) {

         
    $xml .= '<species id="' $row['id'] . '" name="' $row['name'] . '" />' "\r\n"


    Now, if you want to add to an existing XML file, it gets a bit more complicated. It is probably best to create a new XML file everytime you need it. I guess there are some exceptions, and it is possible to manipulate an XML document, but since you have a database already, I would just make a new xml file each time.
    Last edited by JasonDFR; 03-04-2009 at 02:43 PM. Reason: Can't spell

Bookmarks

Posting Permissions

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