0

Can i use php loops in xml file to list out a list of number incremented file names such as file 1.jpg file2.jpg file3.jpg etc to save me having to manually typing it out. If so, how do I do this and what doctype etc would the xml file need to have to make it display the loop?

1
  • What is your desired output? What have you tried so far? Commented Sep 25, 2012 at 12:55

2 Answers 2

2

Your question is a little vague, I can't work out if you're trying to read an XML file or write to it.

Assuming you want to read an XML file, and assuming your XML looks a little like this:

<?xml version="1.0" encoding="utf-8" ?>
<rootnode>
  <images>
    <image>file_1.jpg</image>
    <image>file_2.jpg</image>
    <image>file_3.jpg</image>
  </images>
</rootnode>

you could use SimpleXML to either load the XML file or to load the XML as a string and then return an object to loop through. http://php.net/manual/en/ref.simplexml.php

Your PHP might look something like this:

if (file_exists('test.xml')) {
    $xml = simplexml_load_file('test.xml');

    foreach($xml->images AS $image) {
        echo $image;
    }
} else {
    exit('Failed to open test.xml.');
}
Sign up to request clarification or add additional context in comments.

1 Comment

Just want to know if there is a simple way of producing a list of file names in an xml file such as file1.jpg file2.jpg file3.jpg using say a php loop instead of me having to list out from file1.jpg upto file80.jpg thanks
1

What I would do is point to a .php file that outputs XML. All you need to do that is send a header("Content-Type: text/xml"); and make sure you add the at the top of your output.

Here's an example:

    <?php

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

    print "<?xml version=\"1.0\"?>";

    print "<rootNode>";

    for(your loop stuff here) {
        print "<node>" . $yourDataHere . "</node>";
    }

    print "</rootNode>";

    ?>

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.