Page 1 of 5 123 ... LastLast
Results 1 to 10 of 43

Thread: How to read xml file using PHP

  1. #1
    Join Date
    Jun 2016
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to read xml file using PHP

    Hello,
    my question may be seems simple but what i really meant is that i have unzipped a pptx file and i m trying to read the xml using php ... here is my code .
    i just got a "done (unzipped succeeded)" output and nothing else

    PHP Code:
    <?php
    error_reporting
    (E_ALL);
    ini_set('display_errors'1);

        

    // the string variable that will hold the file content
    $file_content "";

    // open file
    $file_path './Reports/report.pptx';
    $target_folder_path './Reports/';

    $zip = new ZipArchive;
    $res $zip->open($file_path);

    if (
    $res === TRUE) {
        
    $zip->extractTo($target_folder_path);
        
    //$zip->close();
        
    echo 'done <br>';
        
        
    // loop through all slide#.xml files
        
    $slide 1;

        while ( (
    $index $zip -> locateName("./Reports/ppt/slides/" $slide ".xml")) !== false ){

            
    $data $zip -> getFromIndex($index);
            
    $xml DOMDocument::loadXML($dataLIBXML_NOENT LIBXML_XINCLUDE LIBXML_NOERROR LIBXML_NOWARNING);
            
    $file_content .= strip_tags($xml -> saveXML());

            
    $slide++;
        }
        
    $zip->close();
        
    }   else {
            echo 
    'failed';
        }

    echo 
    $file_content;
    //var_dump($file_content);
    ?>

    Thanks in advance
    Karl

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    First thing I would try is to make sure these files are actually there in the output folder, like:

    ./Reports/ppt/slides/1.xml

    Or maybe that's the problem. From the comment (// loop through all slide#.xml files) you are looking for like:

    ./Reports/ppt/slides/slide1.xml

    But the code is only looking for ./Reports/ppt/slides/1.xml

    If that's not the problem, make sure those files are there.

    If all that checks out, I see that you seem to be trying to extract each xml file again in order to process it (from within the loop):

    $data = $zip -> getFromIndex($index);

    Instead, try using the actual already extracted file (whatever you use, $data must now be a valid xml string):

    $data = file_get_contents("./Reports/ppt/slides/" . $slide . ".xml");

    If still not working do some checking to see that the loop is actually proceeding, like put something in there that will echo the filename each time the loop executes:

    echo "./Reports/ppt/slides/" . $slide . ".xml";

    If it's not executing AND the files are there AND they are named correctly/as expected, then this needs work:

    while ( ($index = $zip -> locateName("./Reports/ppt/slides/" . $slide . ".xml")) !== false )

    Try looping on the actual folder, like maybe using glob to get the actual files.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    OK, I got this working in a mock up locally. This line looks to be the trouble:

    Code:
     while ( ($index = $zip -> locateName("./Reports/ppt/slides/" . $slide . ".xml")) !== false ){
    The path ("./Reports/ppt/slides/") and filename ($slide . ".xml") are crucial. They must be relative and accurate to the structure of the .zip file - NOT to that of the output folder. In my mock up I had a zip that had 1.xml and 2.xml in the .zip file. The output folder was 'output' but that's immaterial unless we want to go after the actual extracted files. Using the methodology already in your code we can simply go after the $zip object. But we must do so using it's structure. ./ means nothing, and causes a problem. If we want to start from the root of the zip file, use nothing to indicate that. Next comes the path (if any) and the filename within the zip archive. To be absolutely certain of what that is, look at the archive using a utility or other method that shows its internal path structure.

    That all said, working with glob and the actual output, as I mentioned before would also work.

    If you want more help, attach or give me a location I can download your zip archive from.
    Last edited by jscheuer1; 06-03-2016 at 08:43 PM.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Jun 2016
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi John,

    thank for your response.
    there was a typo on this line
    $data = file_get_contents("./Reports/ppt/slides/" . $slide . ".xml");

    instead for $data = file_get_contents("./Reports/ppt/slides/slide" . $slide . ".xml");

    however it still doesnt do anything => blank page

    ich made some changes and i got donefailed ???
    PHP Code:
    <?php
    error_reporting
    (E_ALL);
    ini_set('display_errors'1);

    // the string variable that will hold the file content
    $file_content "";

    // open file
    $file_path 'Reports/report.pptx';
    $zip = new ZipArchive;
    $res $zip->open($file_path);

    if (
    $res === TRUE) {
    $zip->extractTo('Reports');

    echo 
    'done';

    // loop through all slide#.xml files
    $slide 1;

    while ( (
    $index $zip -> locateName("Reports/ppt/slides/slide" $slide ".xml")) !== false ){
        
        
    //echo "./Reports/ppt/slides/slide" . $slide . ".xml";
        
    $data $zip -> getFromIndex($index);
        
    $xml DOMDocument::loadXML($dataLIBXML_NOENT LIBXML_XINCLUDE LIBXML_NOERROR LIBXML_NOWARNING);
        
    $file_content .= strip_tags($xml -> saveXML());
        
    $slide++;
    }
    $zip->close();

    echo 
    'failed';

    }
    echo 
    $file_content;

    ?>
    Karl

  5. #5
    Join Date
    Jun 2016
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi,
    you can any pptx file of your choice
    Thks in advance
    Karl

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    OK, There could also still be other problems, but you took away too much ./

    By way of explanation, you don't really have to extract anything, but if you do, the path must be correct, and the path to the archive must be correct in any case. It is only the path inside the archive used by locateName that must reflect only the actual structure of the archive and not use the ./ or any local folders. So this should work assuming the pptx file is in the Reports folder off of the current working folder:

    PHP Code:
    <?php 
    error_reporting
    (E_ALL); 
    ini_set('display_errors'1); 

    // the string variable that will hold the file content 
    $file_content ""

    // open file 
    $file_path './Reports/report.pptx'// must be a valid pptx file with path relative to this script's working folder
    $zip = new ZipArchive
    $res $zip->open($file_path); 

    if (
    $res === TRUE) { 
    $zip->extractTo('./Reports/'); // not required, but if used, must be a valid local path, only needed if you want to have the files extracted

    echo 'done<br>'// this means we successfully opened the zip for processing

    // loop through all slide#.xml files 
    $slide 1

    while ( (
    $index $zip -> locateName("ppt/slides/slide" $slide ".xml")) !== false ){ // must represent the internal structure of the archive - no local paths allowed
         
        
    $data $zip -> getFromIndex($index); 
        
    $xml DOMDocument::loadXML($dataLIBXML_NOENT LIBXML_XINCLUDE LIBXML_NOERROR LIBXML_NOWARNING); 
        
    $file_content .= strip_tags($xml -> saveXML()); 
        
    $slide++; 

    $zip->close(); 


    } else {
    echo 
    'failed'// this means we failed to open any archive
    }
    echo 
    $file_content

    ?>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Jun 2016
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hey John,

    Code:
    <?php  
    error_reporting(E_ALL);  
    ini_set('display_errors', 1);  
    
    // the string variable that will hold the file content  
    $file_content = "";  
    
    // open file  
    $file_path = './Reports/report.pptx'; // must be a valid pptx file with path relative to this script's working folder 
    $zip = new ZipArchive;  
    $res = $zip->open($file_path);  
    
    if ($res === TRUE) {  
    $zip->extractTo('./Reports/'); // not required, but if used, must be a valid local path, only needed if you want to have the files extracted 
    
    echo 'done<br>'; // this means we successfully opened the zip for processing 
    
    // loop through all slide#.xml files  
    $slide = 1;  
    
    while ( ($index = $zip -> locateName("ppt/slides/slide" . $slide . ".xml")) !== false ){ // must represent the internal structure of the archive - no local paths allowed 
          
        $data = $zip -> getFromIndex($index); 
    	$xml= new DOMDocument();
        $xml->loadXML($data, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);  
        $file_content .= strip_tags($xml -> saveXML());  
        $slide++;  
    }  
    $zip->close();  
    
    
    } else { 
    echo 'failed'; // this means we failed to open any archive 
    } 
    echo $file_content;  
    
    ?>
    now it works fine but can you help me to display those information properly?

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by mignoncharly View Post
    now it works fine but can you help me to display those information properly?
    "properly" is a relative term. I don't even know what data is in your file(s), let alone how you want it displayed.

    That said, the xml tags within the xml files we have just extracted and read probably provide a clue as to what each piece of data represents. It's entirely possible to get the tag names and use them as table headers. But that might not be what you want. Alternatively, one can introduce line breaks at strategic points. It's possible that might accomplish what you're after. There well may be other possibilities.

    In any case, if I could see a typical report.pptx file of the kind you are working with, that would help me get an idea of how best to present the data. But, even so, you are the one who needs to describe the optimal output format.

    For instance, if you could take the output from our current working version and show or describe how you would like to see that being displayed - that might be all I need to know.

    As you can see though, it's pretty vague to just say, "display those information properly".

    Again, can you supply a typical file and/or describe what "properly" means to you in a more specific manner?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Jun 2016
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi John,

    the document is confidential but its about a report on company's progression with images. So iwanted to know if there is apossibility to extract all these info through xml and parse it in a pdf file using i.e fpdf library.

    Karl

  10. #10
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    This blog post might help you extract and manipulate xml data into an HTML page - you can then be your own judge and creator of how to display something "properly" http://www.dynamicdrive.com/forums/e...with-SimpleXML

    From there, you might be able to use an HTML to PDF converter https://www.sitepoint.com/convert-ht...f-with-dompdf/
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

Similar Threads

  1. read xml file
    By lay in forum PHP
    Replies: 4
    Last Post: 03-12-2009, 12:13 PM
  2. how to read pdf file using php?
    By khoulong in forum PHP
    Replies: 1
    Last Post: 03-12-2009, 03:33 AM
  3. read pdf file
    By lay in forum PHP
    Replies: 3
    Last Post: 03-12-2009, 02:39 AM
  4. how to read xml file from safari?
    By onoprise in forum JavaScript
    Replies: 1
    Last Post: 12-16-2008, 10:55 PM
  5. asp read file
    By midhul in forum ASP
    Replies: 1
    Last Post: 02-23-2008, 06:37 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •