Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8686815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:57:36+00:00 2026-06-12T22:57:36+00:00

I have a simple little script that grabs XML and converts it to CSV.

  • 0

I have a simple little script that grabs XML and converts it to CSV. However it doesn’t write the headers. Would anyone know how to write the headers?

$file='example.xml';
if (file_exists($file)) {
    $xml = simplexml_load_file($file);
    $f = fopen('newfile.csv', 'w');
    foreach ($xml->Information as $information) {
        fputcsv($f, get_object_vars($information),',','"');
    }
    fclose($f);
}

This would put all the content of the child elements of <Information> in nice columns for Excel. But it won’t write the element names as CSV headings.

Any ideas how I could edit the script to also include the headers?

Thanks

Mike

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:57:39+00:00Added an answer on June 12, 2026 at 10:57 pm

    You can use the SimpleXMLElement::getName() function to get the element name from the first set of data and use that to write the CSV header.

    $file='example.xml';
    if (file_exists($file)) {
        $xml = simplexml_load_file($file);
        $f = fopen('newfile.csv', 'w');
    
        // array to hold the field names
        $headers = array(); 
        // loop through the first set of fields to get names
        foreach ($xml->Information->children() as $field) { 
            // put the field name into array
            $headers[] = $field->getName(); 
        }
        // print headers to CSV
        fputcsv($f, $headers, ',', '"');
    
        foreach ($xml->Information as $information) {
            fputcsv($f, get_object_vars($information), ',', '"');
        }
        fclose($f);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have found this little script in PHP that send a simple request to
I have a little problem with a simple vbScript. The script has to run
I have a simple little code fragment that is frustrating me: HashSet<long> groupUIDs =
I have an little script which logs users that login to my Pidgin/MSN account
I was trying to create a quick little script that would insert data into
I have a simple search script that takes user input and searches across directories
I have a very simple js script that works for hovers, but I want
I have a little script that prints a certain amount of rows in a
I have a simple shell script that builds my Roxygen documents, builds the package,
I which to make a simple little bash script that toggle some files (specifically

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.