Jump to content

[SOLVED] Parsing xml and using php inserting in mysql


engelsol

Recommended Posts

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

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>"; 
    }
}
?>

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...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.