473,320 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

php to xml

anfetienne
424 256MB
Hi,

I have this php code that creates a xml file(#1)....is is possible to get it to generate a xml like the 1 below(#2)? The main part of my xml is the caption....can i add a new line to be generated for the xml for the amount of images/captions required?


#1
Expand|Select|Wrap|Line Numbers
  1. <?
  2. $doc = new_xmldoc('1.0');
  3. $root = $doc->add_root('members');
  4. $member = $root->new_child('member','');
  5.  
  6. $member->new_child('lastName','John');
  7. $member->new_child('firstName','Adams');
  8. $member->new_child('contribution','3400');
  9.  
  10. $fp = @fopen('members.xml','w');
  11. if(!$fp) {
  12.     die('Error cannot create XML file');
  13. }
  14. fwrite($fp,$doc->dumpmem());
  15. fclose($fp);
  16. ?>
  17.  


Expand|Select|Wrap|Line Numbers
  1. <gallery> 
  2.       <pic1 name="PHOTO 1" caption="This is the space where you can put in your own customised caption for each image."/> 
  3.       <pic2 name="PHOTO 2" caption="This is the space where you can put in your own customised caption for each image. "/> 
  4.       <pic3 name="PHOTO 3" caption="This is the space where you can put in your own customised caption for each image. "/> 
  5.       <pic4 name="PHOTO 4" caption="This is the space where you can put in your own customised caption for each image. "/> 
  6.       <pic5 name="PHOTO 5" caption="This is the space where you can put in your own customised caption for each image. "/> 
  7.       <pic6 name="PHOTO 6" caption="This is the space where you can put in your own customised caption for each image. "/> 
  8.       <pic7 name="PHOTO 7" caption="This is the space where you can put in your own customised caption for each image."/> 
  9.       <pic8 name="PHOTO 8" caption="This is the space where you can put in your own customised caption for each image. "/> 
  10.       <pic9 name="PHOTO 9" caption="This is the space where you can put in your own customised caption for each image. "/> 
  11.       <pic10 name="PHOTO 10" caption="This is the space where you can put in your own customised caption for each image. "/> 
  12.       <pic11 name="PHOTO 11" caption="This is the space where you can put in your own customised caption for each image. "/> 
  13.       <pic12 name="PHOTO 12" caption="This is the space where you can put in your own customised caption for each image. "/> 
  14.       <pic13 name="PHOTO 13" caption="This is the space where you can put in your own customised caption for each image."/> 
  15.       <pic14 name="PHOTO 14" caption="This is the space where you can put in your own customised caption for each image. "/> 
  16.       <pic15 name="PHOTO 15" caption="This is the space where you can put in your own customised caption for each image. "/> 
  17.       <pic16 name="PHOTO 16" caption="This is the space where you can put in your own customised caption for each image. "/> 
  18.       <pic17 name="PHOTO 17" caption="This is the space where you can put in your own customised caption for each image. "/> 
  19. </gallery>
  20.  
Mar 11 '09 #1
23 1918
Markus
6,050 Expert 4TB
Do the XML adding in a loop, using the amount of images required as the delimiter.
Mar 11 '09 #2
anfetienne
424 256MB
markus.....you help out so much but you are confsuing this noob...lol can you show me an example of what you just said and i can work from there lol

thanks....a great help you are
Mar 11 '09 #3
Dormilich
8,658 Expert Mod 8TB
roughly something like
Expand|Select|Wrap|Line Numbers
  1. foreach($image) {
  2.   $member->new_child($image_spec);
  3. }
note that this is not valid PHP code! but it should be enough to demonstarte the idea. (you can also use a for or while loop)
Mar 11 '09 #4
Markus
6,050 Expert 4TB
I could, but I'm not overly sure what your problem is.

Let me try and take a swing:
You want to generate n XML and in that file you want n amounts of elements (pic) - with attributes 'name' and 'caption'.
Is that correct?

Also, the elements don't have to be progressively named (pic1, pic2) do they? Because that really doesn't make sense to me. The best way to do that, but I'll let dormi confirm, is like so:
Expand|Select|Wrap|Line Numbers
  1. <gallery>
  2.   <pic>
  3.     <name>photo 1</name>
  4.     <caption>this is photo 1</caption>
  5.   </pic>
  6.   [...]
  7. </gallery>
  8.  
or
Expand|Select|Wrap|Line Numbers
  1. <gallery>
  2.   <pic name="photo 1" caption="this is photo 1" />
  3.   [...]
  4. </gallery>
  5.  
Would that markup be OK?
Mar 11 '09 #5
anfetienne
424 256MB
this doesn't input the two attributes that i need:-

name="PHOTO 1"

caption="This is the space where you can put in your own customised caption for each image."
Mar 11 '09 #6
Dormilich
8,658 Expert Mod 8TB
@Markus
either markup is OK (well, of course add the prologue, but this should be done automaticly while saving...). which one to use depends on the programm reading the XML files.
Mar 11 '09 #7
Markus
6,050 Expert 4TB
@anfetienne
Sorry? I don't understand.
Mar 11 '09 #8
Dormilich
8,658 Expert Mod 8TB
@anfetienne
that's only a matter of writing/using the correct method of the used object.
Mar 11 '09 #9
anfetienne
424 256MB
i don't understand what method you are talking about as this is my 1st attempt to generate xml with php.....whats why i asked for a example of something the generates the 1st xml i posted so i can work on it and figure out how exactly it's done.

lol gotta start and learn from somewhere
Mar 11 '09 #10
Dormilich
8,658 Expert Mod 8TB
@anfetienne
well, your code looked like an OOP way of creating XML, thus proposing a method (function that belongs to an object). the most useful classes in PHP for that are SimpleXML and DOMDocument, although you need to tweak (extend) it a bit to fit your needs.
Mar 11 '09 #11
anfetienne
424 256MB
hmmmm....thanks for the help....i'm just going to try figure this out by myself
Mar 11 '09 #12
anfetienne
424 256MB
ok so i have got this far...here is my coding.....it generates the xml but how do i change it so that it adds another line if there is more than 1 image caption

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $xmltext = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<gallery></gallery>";
  4. $xmlobj = simplexml_load_string($xmltext);
  5.  
  6. $i=1;
  7.  
  8. $nameCaps = $xmlobj->addChild("pic");
  9. $nameCaps->addAttribute("name", "PHOTO 1");
  10. $nameCaps->addAttribute("caption", "This is the space where you can put in your own customised caption for each image.");
  11.  
  12. print header("Content-type: text/plain") . $xmlobj->asXML();
  13.  
  14. ?>
  15.  
also the coding genrates this:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <gallery>
  3. <pic name="PHOTO 1" caption="This is the space where you can put in your own customised caption for each image."/>
  4. </gallery>
  5.  
but i want it to look like this....it won't allow me to use any vars between the brackets

Expand|Select|Wrap|Line Numbers
  1. <gallery>
  2.       <pic1 name="PHOTO 1" caption="This is the space where you can put in your own customised caption for each image."/> 
  3. </gallery>
Mar 11 '09 #13
Dormilich
8,658 Expert Mod 8TB
@anfetienne
put this in a loop
Expand|Select|Wrap|Line Numbers
  1. $nameCaps = $xmlobj->addChild("pic");
  2. $nameCaps->addAttribute("name", "PHOTO 1");
  3. $nameCaps->addAttribute("caption", "This is the space where you can put in your own customised caption for each image.");
@anfetienne
the first output is perfectly fine. why do you want to put vars between the brackets? and whitespace between the XML tags is only needed for the human eye, any programm reading XML will ignore it (except for CDATA sections).
Mar 11 '09 #14
anfetienne
424 256MB
my lines need to look like this.....the vars are only to put a digit infront of "pic" or do you think it will be better to have my vars set on a different page and bring it in via session or $_GET?

Expand|Select|Wrap|Line Numbers
  1.       <pic1 name="PHOTO 1" caption="This is the space where you can put in your own customised caption for each image."/> 
  2.       <pic2 name="PHOTO 2" caption="This is the space where you can put in your own customised caption for each image. "/> 
  3.       <pic3 name="PHOTO 3" caption="This is the space where you can put in your own customised caption for each image. "/> 
  4.       <pic4 name="PHOTO 4" caption="This is the space where you can put in your own customised caption for each image. "/> 
  5.       <pic5 name="PHOTO 5" caption="This is the space where you can put in your own customised caption for each image. "/> 
  6.       <pic6 name="PHOTO 6" caption="This is the space where you can put in your own customised caption for each image. "/> 
  7.       <pic7 name="PHOTO 7" caption="This is the space where you can put in your own customised caption for each image."/> 
  8.       <pic8 name="PHOTO 8" caption="This is the space where you can put in your own customised caption for each image. "/> 
  9.       <pic9 name="PHOTO 9" caption="This is the space where you can put in your own customised caption for each image. "/> 
  10.       <pic10 name="PHOTO 10" caption="This is the space where you can put in your own customised caption for each image. "/> 
  11.       <pic11 name="PHOTO 11" caption="This is the space where you can put in your own customised caption for each image. "/> 
  12.       <pic12 name="PHOTO 12" caption="This is the space where you can put in your own customised caption for each image. "/> 
  13.       <pic13 name="PHOTO 13" caption="This is the space where you can put in your own customised caption for each image."/> 
  14.       <pic14 name="PHOTO 14" caption="This is the space where you can put in your own customised caption for each image. "/> 
  15.       <pic15 name="PHOTO 15" caption="This is the space where you can put in your own customised caption for each image. "/> 
  16.       <pic16 name="PHOTO 16" caption="This is the space where you can put in your own customised caption for each image. "/> 
  17.       <pic17 name="PHOTO 17" caption="This is the space where you can put in your own customised caption for each image. "/> 
  18.  
Mar 11 '09 #15
Dormilich
8,658 Expert Mod 8TB
now I see the problem.... try
Expand|Select|Wrap|Line Numbers
  1. $nameCaps = $xmlobj->addChild("pic$i");
Mar 11 '09 #16
anfetienne
424 256MB
here's the result

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <gallery>
  3. <pic1 name="PHOTO 1" caption="This is the space where you can put in your own customised caption for each image."/>
  4. <pic1 name="PHOTO 2" caption="This is the space where you can put in your own customised caption for each image."/>
  5. <pic1 name="PHOTO 3" caption="This is the space where you can put in your own customised caption for each image."/>
  6. </gallery>
  7.  
Mar 11 '09 #17
anfetienne
424 256MB
this is my code

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $xmltext = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<gallery></gallery>";
  3. $xmlobj = simplexml_load_string($xmltext);
  4.  
  5. $i=1;
  6. $rowN= pic.$i++;
  7.  
  8.  
  9. $nameCapsA = $xmlobj->addChild("$rowN");
  10. $nameCapsA->addAttribute("name", "PHOTO 1");
  11. $nameCapsA->addAttribute("caption", "This is the space where you can put in your own customised caption for each image.");
  12.  
  13.  
  14. $nameCapsB = $xmlobj->addChild("$rowN");
  15. $nameCapsB->addAttribute("name", "PHOTO 2");
  16. $nameCapsB->addAttribute("caption", "This is the space where you can put in your own customised caption for each image.");
  17.  
  18.  
  19. $nameCapsC = $xmlobj->addChild("$rowN");
  20. $nameCapsC->addAttribute("name", "PHOTO 3");
  21. $nameCapsC->addAttribute("caption", "This is the space where you can put in your own customised caption for each image.");
  22.  
  23. print header("Content-type: text/plain") . $xmlobj->asXML();
  24.  
  25. ?>
  26.  
Mar 11 '09 #18
anfetienne
424 256MB
for the numbering im just going to pass the variables from one page to another with session().....the only thing that i need help on now is getting a new line to be created automatically for each image caption added.

here do i start? thanks in advance
Mar 12 '09 #19
anfetienne
424 256MB
help anyone??? still can't figure out this one bit
Mar 13 '09 #20
Markus
6,050 Expert 4TB
Like has been said previously: you need to put the xml stuff in a loop - logic says inside your image upload loop.
Mar 13 '09 #21
anfetienne
424 256MB
should i use a while loop? i don't have a clue where to start.....sorry to be a pain
Mar 13 '09 #22
Markus
6,050 Expert 4TB
@anfetienne
Well, the data you're inserting into the XML file is based on the images uploaded, correct? And you already are using a while() loop to upload the images. So, from that, you could easily integrate into your loop the xml object you use.

This is pseudo-code:

Expand|Select|Wrap|Line Numbers
  1. while ( $my_images_are_uploading )
  2. {
  3.     // You will have to use the image data here.
  4.     $child = $xml_object->addChild( [...] );
  5.     $child->addAttribute( [...] );
  6. }
  7.  
Mar 13 '09 #23
anfetienne
424 256MB
my final question lol

As i am using a form to populate the xml...world i need to have the var in "while ( $my_images_are_uploading )" or would i have to put something else there?

Thanks for all your help
Mar 13 '09 #24

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
1
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the...
4
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the...
1
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url ...
2
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value...
3
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.