1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

want to create XML file using PHP

Discussion in 'PHP & Perl' started by naveensingh, Jan 27, 2011.

  1. naveensingh

    naveensingh Power Member

    Joined:
    Feb 15, 2010
    Messages:
    622
    Likes Received:
    198
    Occupation:
    Wordpress & Magento Expert
    Location:
    United States
    Hi,
    is there any way to create a xml file using PHP , i have tired by DOM but not able to make that. is there any sample code.

    thanks
     
  2. BugFixed

    BugFixed Junior Member

    Joined:
    Sep 24, 2010
    Messages:
    130
    Likes Received:
    39
    Try googling with keyword "PHP XML" there are plenty of samples.
     
  3. voyevoda

    voyevoda Regular Member Premium Member

    Joined:
    Mar 21, 2010
    Messages:
    217
    Likes Received:
    97
    Location:
    Eastern Front
    Code:
    <?php
    
    header("Content-Type: application/xml; charset=utf-8"); 
    
    echo <<<EOS
    <?xml version="1.0" encoding="UTF-8"?>
    <doc>
      <!-- data here -->
    </doc>
    EOS;
    
    ?>
    There you go.
     
  4. gillyweed

    gillyweed Newbie

    Joined:
    Mar 19, 2010
    Messages:
    4
    Likes Received:
    1
    Code:
    <?php
    $filename = "newxmlfile";
    $filetype = ".xml";
    $filepath = "files/";
    $file = $filepath.$filename.$filetype;
    $input = "type your xml here";
    
    $filehandle = fopen($file, 'w') or die("Failed to open" .$file. ".");
    fwrite($filehandle, $input) or die("Failed to write to " .$file. ".");
    fclose($filehandle);
    echo "Success. " .$file. " was created.";
    ?>
    the variables are as follows:

    $filename = your desired file name (not the extention)
    $filepath = the directory where you want it saved to (relative to your php doc)
    $filetype = the type of file (in this case .xml)
    $file = ignore this
    $input = put your xml in here (you will need to add a xml header though)

    If this was any help then please press thanks :)
     
  5. HungryMind

    HungryMind Newbie

    Joined:
    Jul 10, 2010
    Messages:
    9
    Likes Received:
    0
    I've code but i can't send here due to some restrictions :s
     
  6. cyberroot

    cyberroot BANNED BANNED

    Joined:
    Jul 24, 2010
    Messages:
    345
    Likes Received:
    72
    there are thousand of way to create xml file man fopen , print , etc whats ur exact need ?