engelsol Posted June 26, 2007 Share Posted June 26, 2007 How to parse this xml and generate php code to insert to mysql. In this example xml there are 3 articles, one with 2 photos and the others with just one. A Php5 solution requiered. <?xml version="1.0"?> <articles> <article artId="646889" artName="102506-CRI-59-1" SendToWeb="Si" Credito="Presidente/Nombre Apellido" WebSeccionName="SUPLEMENTOS" SubWebSeccionName="" SectionFileNameTag="ghjghg" SectionDesc="" ExtCopyRight="EFE" ArtTemaId="" ArtForum="No" Comment="Comentario" Valid="Si" ExtPriority="No" sectiondesc="Provincias" pagenumber="1"> <head> <kicker>lead here</kicker> <headline>Headline goes here</headline> </head> <body> <intro>abstract text</intro> <para>body content</para> <para>body content</para> <para>body content</para> </body> <photo refRef="img" refId="323573" SendToWeb="Si" PhotoComment="" refMIME="application/postscript" refURL="file:///H:/Critica/Fotos/012105-EVA-02-3-01.eps" refWidth="336693m" refHeight="578160m" refPixWidth="335" refPixHeight="576" PageType="Article" ImgCredito="Carlos Sanchez"></photo> <photo refRef="img" refId="571721" SendToWeb="Si" PhotoComment="" refMIME="application/postscript" refURL="file:///H:/Panama_America/Fotos/SIGALE-PISTA-22-7-01.EPS" refWidth="216810m" refHeight="326490m" refPixWidth="216" refPixHeight="325" PageType="Article" ImgCredito="Alberto Mota"></photo> </article> <article artId="646890" artName="102507-CRI-59-1" SendToWeb="Si" Credito="Presidente/Nombre Apellido" WebSeccionName="SUPLEMENTOS" SubWebSeccionName="" SectionFileNameTag="ghjghg" SectionDesc="" ExtCopyRight="EFE" ArtTemaId="" ArtForum="No" Comment="Comentario" Valid="Si" ExtPriority="No" sectiondesc="Provincias" pagenumber="1"> <head> <kicker>lead here</kicker> <headline>Headline goes here</headline> </head> <body> <intro>abstract text</intro> <para>body content</para> <para>body content</para> <para>body content</para> </body> <photo refRef="img" refId="571725" SendToWeb="Si" PhotoComment="" refMIME="application/postscript" refURL="file:///H:/Panama_America/Fotos/SIGALE-PISTA-22-7-01.EPS" refWidth="216810m" refHeight="326490m" refPixWidth="216" refPixHeight="325" PageType="Article" ImgCredito="Alberto Mota"></photo> </article> <article artId="646891" artName="102508-CRI-59-1" SendToWeb="Si" Credito="Presidente/Nombre Apellido" WebSeccionName="SUPLEMENTOS" SubWebSeccionName="" SectionFileNameTag="ghjghg" SectionDesc="" ExtCopyRight="EFE" ArtTemaId="" ArtForum="No" Comment="Comentario" Valid="Si" ExtPriority="No" sectiondesc="Provincias" pagenumber="1"> <head> <kicker>lead here</kicker> <headline>Headline goes here</headline> </head> <body> <intro>abstract text</intro> <para>body content</para> <para>body content</para> <para>body content</para> </body> <photo refRef="img" refId="323579" SendToWeb="Si" PhotoComment="" refMIME="application/postscript" refURL="file:///H:/Critica/Fotos/012105-EVA-02-3-01.eps" refWidth="336693m" refHeight="578160m" refPixWidth="335" refPixHeight="576" PageType="Article" ImgCredito="Carlos Sanchez"></photo> </article> </articles> Engel Link to comment https://forums.phpfreaks.com/topic/57207-solved-parsing-xml-and-using-php-inserting-in-mysql/ Share on other sites More sharing options...
Barand Posted June 26, 2007 Share Posted June 26, 2007 something like this <?php $xml = simplexml_load_file('myfile.xml'); foreach ($xml->article as $art) { echo "<h1>{$art->head->headline}</h1>"; echo "<h3>{$art['artName']}</h3>"; foreach ($art->body->para as $txt) { echo "<p>$txt</p>"; } foreach ($art->photo as $pic) { echo "<p>Photo Ref: {$pic['refId']}<br>"; echo "Photo Credit: {$pic['ImgCredito']}</p>"; } } ?> Link to comment https://forums.phpfreaks.com/topic/57207-solved-parsing-xml-and-using-php-inserting-in-mysql/#findComment-282882 Share on other sites More sharing options...
engelsol Posted June 26, 2007 Author Share Posted June 26, 2007 But if I want to build some kind of arrays so that I can then can manipulate the data make some kind of cleanup and generate MYSQL Inserts scripts.? Link to comment https://forums.phpfreaks.com/topic/57207-solved-parsing-xml-and-using-php-inserting-in-mysql/#findComment-282949 Share on other sites More sharing options...
Barand Posted June 26, 2007 Share Posted June 26, 2007 Then instead of echoing the elements as I have, store them in array. Link to comment https://forums.phpfreaks.com/topic/57207-solved-parsing-xml-and-using-php-inserting-in-mysql/#findComment-282962 Share on other sites More sharing options...
engelsol Posted June 26, 2007 Author Share Posted June 26, 2007 please give me an exmple code Link to comment https://forums.phpfreaks.com/topic/57207-solved-parsing-xml-and-using-php-inserting-in-mysql/#findComment-283016 Share on other sites More sharing options...
Barand Posted June 26, 2007 Share Posted June 26, 2007 In the code above, $xml $xml->article $art->body->para $art->photo are all arrays Link to comment https://forums.phpfreaks.com/topic/57207-solved-parsing-xml-and-using-php-inserting-in-mysql/#findComment-283077 Share on other sites More sharing options...
engelsol Posted June 27, 2007 Author Share Posted June 27, 2007 ok You put me on the right way.. Now I am only missing two things... 1- For the above code... and example the $art->body->para array have multiple content. So how now I get all different $art->body->para into one variable. 2- When importing text with accents they come with errors... I guest this is an encoding thing... if the original text was in utf-8 or in iso-8859-1 how do I can get them correctly Thanks in advance... Link to comment https://forums.phpfreaks.com/topic/57207-solved-parsing-xml-and-using-php-inserting-in-mysql/#findComment-284056 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.