XML to Text File

Discussion in 'PHP' started by IGiveMoney, Oct 23, 2008.

  1. #1
    So I'm working on something to try to get xml to parse,
    and then after it is parsed and is in the proper format
    I would like - to have it write to a text file for later
    processing.

    Now I have the proper formatting I want (it displays on the
    page properly) and now it's time to write it to a text file.

    But my theory got screwed up somewhere.

    I thought I would be able to parse out the data while
    writing to the file. But that really isn't working properly.
    Instead it's only writing a 1 to the file.

    So I've removed the write to a text file code and this
    is what I have:

    
    <?php
    // The XML file that is to be parsed for osc
    $file = "sample.xml";
    
    # Get the contents of each tag
    function contents($parser, $data){
    	
    	echo $data;
    }
    
    // This helps setup some formatting for the top
    
    function startTag($parser, $data){
    $temp_data1 = str_replace('INVENTORY', '', $data);
    $temp_data2 = str_replace('DATA', '', $temp_data1);
    
    		echo $temp_data2 . ': ';
    	}
    
    // And the end tag needs to be set for formatting
    function endTag($parser, $data){
    	echo "<br />";
    }
    
    // These lines create the parser and then parse the data
    
    $xml_parser = xml_parser_create();
    
    // Sets the functions for start and end tags
    xml_set_element_handler($xml_parser, "startTag", "endTag");
    
    // Sets the function for the contents/data
    xml_set_character_data_handler($xml_parser, "contents");
    
    // Open the xml feed for reading
    $fp = fopen($file, "r");
    
    // Read the file and store a variable
    $data = fread($fp, 80000);
    
    // Parse the document, if it faults out tell them where/why.
    
    
    
    if(!(xml_parse($xml_parser, $data, feof($fp)))){
    	die("Error on line " . xml_get_current_line_number($xml_parser));
    }
    
    
    
    // Free the memory used to create the parser
    xml_parser_free($xml_parser);
    
    // Close xml file as we are done.
    fclose($fp);
    ?>
    
    
    PHP:
    The key - somewhere in there the ability to write to a text
    file the data that is parsed by the xml and script.

    Any help is appreciated.

    Thought - should the writing portion be split up into
    my functions?
     
    IGiveMoney, Oct 23, 2008 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    header('Content-type: text/plain');
    header('Content-Disposition: attachment; filename="newname.text"');
    readfile('yourxml.xml');

    try this one..
     
    bartolay13, Oct 23, 2008 IP
  3. IGiveMoney

    IGiveMoney Peon

    Messages:
    116
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thats not what i really need tho.

    That would just output to a text file that can be downloaded.

    I want it stored on the server - thus the part of writing to a text file
    being incorporated to what is there.


    Thanks tho! :)
     
    IGiveMoney, Oct 23, 2008 IP
  4. salahsoftware

    salahsoftware Peon

    Messages:
    63
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    you may search a class for this on phpclasses.org
     
    salahsoftware, Oct 24, 2008 IP