/    Sign up×
Community /Pin to ProfileBookmark

How to create a dynamic XML file

Hi. I am trying to have PHP dynamically create an xml file based on the contents of a directory.

For example, i have two directories “Dir1” and “Dir2” In each of these directories are several pictures “Dir1-image1.jpg, Dir1-image2.jpg etc” and “Dir2-image1.jpg, Dir2-image2.jpg etc”.

The resulting XML should look like:

[code]
<images>
<imagebatch>
<dir1>dir1-image1.jpg</dir1>
<dir2>dir2-image1.jpg</dir2>
</imagebatch>
<imagebatch>
<dir1>dir1-image2.jpg</dir1>
<dir2>dir2-image2.jpg</dir2>
</imagebatch>
</images>[/code]

The images must be listed in alphabetical order (which i think is the default anyway), and the xml must be saved as a separate file called images-auto.xml.

I have found many tutorials on google on how to do each part (Read content of a directory, create xml files, save xml files etc), but i have spent far too long pulling them together with absolutley no success. Nothing even gets close.

Can anyone help?

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@cluettrJul 12.2007 — Here is some code to get you started... my guess is you are having trouble with the header type. My code does not have the mysql info for pulling the data from the database. I stripped it out so you could understand this. What you want to do is pull your data, write a loop for the xml statements the repeat while putting the data between the xml tags. The structure of you xml file will likely be different but this should render properlly in a webpage to at least give you something to go on.

[code=php]<?
header('Content-type: text/xml');

echo '<?xml version="1.0"?>';
echo '<Items>';
echo ' <Request>';
echo ' <IsValid></IsValid>';
echo ' <ItemSearchRequest>';
echo ' <Author>Robert D. Cluett, Enterprise Network Support Services</Author>';
echo ' <Location>Boston, Massachusetts</Location>';
echo ' <Phone>blah blah</Phone>';
echo ' </ItemSearchRequest>';
echo ' </Request>';
echo ' <TotalResults>'.$count.'</TotalResults>';
echo ' <TotalPages>'.'data here'.'</TotalPages>';

echo ' <Item>';
echo ' <BAID>'.$record['baid'].'</BAID>';
echo ' <DetailPageURL>none</DetailPageURL>';
echo ' <ItemAttributes>';
echo ' <newname>'.$user_name.'</newname>';
echo ' <phoneoffice>'.$record['phone_office_primary'].'</phoneoffice>';
echo ' <phonemobile>'.$record['phone_mobile'].'</phonemobile>';

echo ' <phonehome>'.$record['phone_home'].'</phonehome>';
echo ' </ItemAttributes>';
echo ' </Item>';
echo ' </Items>';
[/code]
×

Success!

Help @Typhoon101 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...