1

I have a list of links to XML files displayed in a div on my site:

File1.xml
File2.xml
File3.xml
etc...

And I have a separate div to display the parsed XML in a formatting of my liking. The parsing is done with PHP. I want to load the file contents into the PHP file for parsing by clicking the link to the individual file. Is there a way to do this?

1

3 Answers 3

1

you can use $_GET to load each file individually.

mysite.com/index.php?file=file1.xml

then use some sort of parser where the file you parse will be that one that is in the get variable.

Sign up to request clarification or add additional context in comments.

Comments

1

The thing to keep in mind is that PHP runs on the server, where as the HTML and display runs on the client (browser), so anytime you want to pass information back and forth (in this case, the file name to parse, and the parsed and formatted document) you need to either go to a separate page, or use AJAX to do the request asynchronously.

I'll leave it up to you to parse and format the XML however you see fit, but this should get you started:

<?php
$file = $_GET['file'];
if (!empty($file)) {
    $xml = load_xml($file);
    $parsed = parse_xml($xml);
    $formatted = format_xml($parsed);
} else {
    $formatted = "";
}
?>
<html>
    <head><title>Sample XML Picker</title></head>
    <body>
    <ul class="list_of_documents">
        <li><a href="<?php echo $_SERVER['PHP_SELF']?>?file=File1.xml">File1.xml</a></li>
        ...
    </ul>
    <div class="results">
        <?php echo $formatted ?>
    </div>
</html>

This links to itself, but with a query string indicating which file to load. At the very beginning, we check to see if that file variable is set, and if it is, go ahead and load, parse, and format the xml. Note that those functions are just stubs for you to fill in with whatever method you choose.

Comments

1

Thank you to those who took the time to answer. This is the method I finally used to create the links and display the parsed XML in an iframe:

<?php
$dirname = "Directory";
$dir = opendir($dirname);
while (false != ($file = readdir($dir))) {
    if (($file != ".") and ($file != "..")) {
        $filename = current(explode(".", $file));
        echo("<a href='Iframe.php?file=$dirname/$file' target='iframe'>$filename</a> <br />");
    }
}
?>
</div>

<div>
<iframe name="iframe" width="400" height="640" style="background: white;"
frameborder="1">

The PHP file loaded in the iframe (Iframe.PHP)

<?php

$story = $_GET[file];
$xml = simplexml_load_file($story);
echo $xml->getName() . "<br />";

foreach($xml->children() as $child) {
    echo $child->getName() . ": " . $child . "<br />";
}
?> 

</body>
</html>
</iframe>
<div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.