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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:09:00+00:00 2026-05-27T09:09:00+00:00

I have few files in .txt format (these files are either saved in a

  • 0

I have few files in .txt format (these files are either saved in a folder or opened from the desktop after execution).

I would like to build an application in PHP that can retrieve these files and save them in XML format and then display then dynamically with XSLT.

Are there any methods that will allow me to do this?

This is the code I have up to now:

$fp = fopen('optim.txt', 'r');

$xml = new XMLWriter;
$xml->openURI('php://output');
$xml->setIndent(true); // makes output cleaner

$xml->startElement('qualité');
while ($line = fgetcsv($fp)) {

   $xml->startElement('qualité');
   $xml->writeElement('critere', $line[0]);
   $xml->writeElement('poids', $line[1]);
   $xml->writeElement('Vm', $line[2]);
   $xml->writeElement('SV', $line[3]);
   $xml->writeElement('critere', $line[4]);
   $xml->writeElement('poids', $line[5]);
   $xml->writeElement('Vm', $line[6]);
   $xml->writeElement('SV', $line[7]);
   $xml->writeElement('resultat', $line[8]);
   $xml->endElement();
}
$xml->endElement();

However there is no result, can anybody help me find where I am going wrong?

  • 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-27T09:09:01+00:00Added an answer on May 27, 2026 at 9:09 am

    The problem is with your line:-

    $xml->openURI('php://output');
    

    This sends the output directly to the client browser, which I think is what you want to do. However, you need to set the correct headers to reflect this.

    Add the following line to your code

    header("Content-Type: text/xml");
    

    and you will see the output directly in your browser. It doesn’t really matter where that line goes, but it must be before any content is sent to the browser, it may be best to put it just before $fp = fopen('optim.txt', 'r');

    You should also change the name of the outermost element to whatever the plural of ‘qualité’ is so that you don’t have parent and child elements of the same name.

    Also, after $xml->setIndent(true); add this line $xml->startDocument(); otherwise your xml will not be valid.

    After a bit more digging it seems that it is essential to have a default time zone set (I don’t know why), so add this to the start of your script:-

    date_default_timezone_set("GMT"); //or whatever your default timezone is.
    

    Which means your code should look like this:-

    date_default_timezone_set("GMT"); //or whatever your default timezone is.
    header("Content-Type: text/xml"); //Added as part of answer
    $fp = fopen('optim.txt', 'r');
    
    $xml = new XMLWriter;
    $xml->openURI('php://output');
    $xml->startDocument(); // added as part of answer
    $xml->setIndent(true); // makes output cleaner
    
    $xml->startElement('qualités'); //changed as part of answer
    while ($line = fgetcsv($fp)) {
    
       $xml->startElement('qualité');
       $xml->writeElement('critere', $line[0]);
       $xml->writeElement('poids', $line[1]);
       $xml->writeElement('Vm', $line[2]);
       $xml->writeElement('SV', $line[3]);
       $xml->writeElement('critere', $line[4]);
       $xml->writeElement('poids', $line[5]);
       $xml->writeElement('Vm', $line[6]);
       $xml->writeElement('SV', $line[7]);
       $xml->writeElement('resultat', $line[8]);
       $xml->endElement();
    }
    $xml->endElement();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few .txt files my app accesses and gets data from. The
I have few jar files which I am not getting from any repositories.I have
I have a few log files like these: /var/log/pureftpd.log /var/log/pureftpd.log-20100328 /var/log/pureftpd.log-20100322 Is it possible
I have a php page that creates page data from two .txt files (one
What I need to do is that I have few files (txt) about 2GB
I have a unit test package in which I keep a few txt files.
I have a few text files, each for its own purposes. (Like: download.txt, questions.txt,
For reasons undisclosed, suppose I have many identical README.txt files in a few dozen
I have the first few lines of a text file fx.txt with the following
In almost all of my projects I have few files that are project wide

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.