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 7501125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:26:07+00:00 2026-05-29T20:26:07+00:00

I really need some help on an issue: I’m converting an XML to CSV

  • 0

I really need some help on an issue: I’m converting an XML to CSV using a PHP script, but I only need to get certain fields in the CSV, not all of them. Is there any way to specify which fields I want to import?

This is the XML file structure:

<PRICES>
<PRICE>
<WIC>HDE0AAFGRBOX</WIC>
<DESCRIPTION>some text here</DESCRIPTION>
<STOCK>100</STOCK>
<MY_PRICE>219.00</MY_PRICE>
<IMAGE>some image here</image>
<EAN-CODE>some code here</EAN-CODE>
</PRICE>
</PRICES>

This is the script I use:

<?
$filexml='stock.xml';
if (file_exists($filexml)) {
$xml = simplexml_load_file($filexml);
$f = fopen('stock.csv', 'w')
foreach($xml->PRICES->PRICE as $price) {
fputcsv($f, get_object_vars($price),',','"');
}
fclose($f);
}
?>

I only need to get WIC, STOCK and MY_PRICE.

Any help will be greatly appreciated.

Thanks in advance!

  • 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-05-29T20:26:09+00:00Added an answer on May 29, 2026 at 8:26 pm

    You only need to create an array with those properties and send it to fputcsv() instead of get_object_vars():

    $filexml='stock.xml';
    if (file_exists($filexml)) {
      $xml = simplexml_load_file($filexml);
      $f = fopen('stock.csv', 'w');
      foreach($xml->PRICES->PRICE as $price) {
        // Array of just the components you need...
        $values = array("WIC" => $price->WIC, "STOCK" => $price->STOCK, "MY_PRICE" => $price->MY_PRICE);
        fputcsv($f, $values,',','"');
      }
      fclose($f);
    }
    

    Note: It may be necessary to cast these values as strings. I can’t remember for certain without testing:

    $values = array(
       "WIC" => (string)$price->WIC, 
       "STOCK" => (string)$price->STOCK, 
       "MY_PRICE" => (string)$price->MY_PRICE
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I really need some help on an issue: I'm converting an XML to CSV
I am really struggling with the issue below and need some help...I have found
I really need some help with an issue I am having with NSURLConnection in
I really need some help with Plone. I am experiencing some issue upgrading from
I really need some help in Regular Expressions, i'm working on a function like
I really need some help with this as I have been trying to fix
I need some help in figuring a compiler error which is really driving me
I've some questions .. and I really need your help. I have an application.
I really need some quick tips here. I've got this VBScript script which sends
I have decided that I really need to get some flowcharts for reverse engineering

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.