simplexml - can it do what I need?

simplexml - can it do what I need?

am 06.02.2010 17:46:16 von TerryA

My first post and I'm just a few days into learning PHP so that I can extra=
ct
data from an XML feed for updating a MySQL driven website. Simplexml grabs
most of my data without a problem but I can't get at the data in elements
such as:=20
ez de
jardin]]>

pièces">
chambres">
(m²)">
I have around a hundred of these elements but only need data from two of
them. I need to extract the CDATA by specifying the idtype and lang
attribute. For example, in the first of these elements, I need to be able t=
o
get out the string "Rez de jardin" by specifying idtype=3D"11" and lang=3D"=
fr".
Can I use Simplexml to do this and, if so, how? If not, what should I use?
Many thanks for any help.
Terry
--=20
View this message in context: http://old.nabble.com/simplexml---can-it-do-w=
hat-I-need--tp27481222p27481222.html
Sent from the PHP - General mailing list archive at Nabble.com.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: simplexml - can it do what I need?

am 06.02.2010 18:24:20 von Shawn McKenzie

TerryA wrote:
> My first post and I'm just a few days into learning PHP so that I can extract
> data from an XML feed for updating a MySQL driven website. Simplexml grabs
> most of my data without a problem but I can't get at the data in elements
> such as:
> > jardin]]>
>
>
>
> I have around a hundred of these elements but only need data from two of
> them. I need to extract the CDATA by specifying the idtype and lang
> attribute. For example, in the first of these elements, I need to be able to
> get out the string "Rez de jardin" by specifying idtype="11" and lang="fr".
> Can I use Simplexml to do this and, if so, how? If not, what should I use?
> Many thanks for any help.
> Terry

Look at the options:

$xml = simplexml_load_file('file.xml', 'SimpleXMLElement', LIBXML_NOCDATA);

--
Thanks!
-Shawn
http://www.spidean.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: simplexml - can it do what I need?

am 07.02.2010 09:14:37 von TerryA

Hi Shawn
Thanks for answering my query. I have looked at the suggestions:

$xml = simplexml_load_file('file.xml', 'SimpleXMLElement', LIBXML_NOCDATA);

I am/was able to load the file OK and to access the data by iteration.
However, I can't find a way to extract data by attributes. I need something
like $string=.
Obviously, that won't work but that's the result I need. How do I get the
data out of one of these elements by specifying its idtype and lang? I've
google for hours on this and for another hour on SimpleXMLElement.

Terry
--
View this message in context: http://old.nabble.com/simplexml---can-it-do-what-I-need--tp2 7481222p27486649.html
Sent from the PHP - General mailing list archive at Nabble.com.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: simplexml - can it do what I need?

am 07.02.2010 10:50:29 von Carlos Medina

TerryA schrieb:
> Hi Shawn
> Thanks for answering my query. I have looked at the suggestions:
>
> $xml = simplexml_load_file('file.xml', 'SimpleXMLElement', LIBXML_NOCDATA);
>
> I am/was able to load the file OK and to access the data by iteration.
> However, I can't find a way to extract data by attributes. I need something
> like $string=.
> Obviously, that won't work but that's the result I need. How do I get the
> data out of one of these elements by specifying its idtype and lang? I've
> google for hours on this and for another hour on SimpleXMLElement.
>
> Terry
Hi Terry,
look at the PHP.NET documentation. There indicates the use of
simpleXMLElement structures. If you want to extract elements from this
object, please read there how this work. By the way, it would be
interesting to see, how your XML is made. May be is usefull to use
another class like DOM.

regards

carlos


http://de2.php.net/manual/fr/book.simplexml.php
http://de2.php.net/manual/fr/refs.xml.php

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: simplexml - can it do what I need?

am 08.02.2010 08:24:16 von TerryA

Hi Carlos

I looked at the documentation for hours but don't see any solution. I'm
uploading a sample file here. The full file contains around 100 properties
but the uploaded file has just one. I need to extract data from this file to
write to a MySQL database. I have no problem in doing this until it comes to
the -> tags. I only need 3 of these elements. I can
extract the data by iteration but that does not help me to get the three
specific tags that I need because there are some tags that appear
for some properties and not for others so the order is different. Here is
the file:
http://old.nabble.com/file/p27496211/annnonces.xml annnonces.xml
Thanks
Terry



--
View this message in context: http://old.nabble.com/simplexml---can-it-do-what-I-need--tp2 7481222p27496211.html
Sent from the PHP - General mailing list archive at Nabble.com.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: simplexml - can it do what I need?

am 08.02.2010 09:12:06 von Ashley Sheridan

--=-HQIiRSloTP50sgcoHMtT
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Sun, 2010-02-07 at 23:24 -0800, TerryA wrote:

> Hi Carlos
>
> I looked at the documentation for hours but don't see any solution. I'm
> uploading a sample file here. The full file contains around 100 properties
> but the uploaded file has just one. I need to extract data from this file to
> write to a MySQL database. I have no problem in doing this until it comes to
> the -> tags. I only need 3 of these elements. I can
> extract the data by iteration but that does not help me to get the three
> specific tags that I need because there are some tags that appear
> for some properties and not for others so the order is different. Here is
> the file:
> http://old.nabble.com/file/p27496211/annnonces.xml annnonces.xml
> Thanks
> Terry
>
>
>
> --
> View this message in context: http://old.nabble.com/simplexml---can-it-do-what-I-need--tp2 7481222p27496211.html
> Sent from the PHP - General mailing list archive at Nabble.com.
>
>


As you iterate the tags, check their attributes with
getAttribute().

Thanks,
Ash
http://www.ashleysheridan.co.uk



--=-HQIiRSloTP50sgcoHMtT--

Re: simplexml - can it do what I need?

am 08.02.2010 10:04:05 von TerryA

Hi Guys

Thanks for all your input. This forum is great! I can get the attributes OK
but I can't code the checking routing correctly. This is what I have:

Many thanks again for your input. I tried this but it only gives me the data
for each element. I need to be able to access the attributes of the data in
order to select only those elements that I need. This is the code that I
have at present:

$file= 'annnonces.xml';
// load file
$xml = simplexml_load_file($file) or die ("Unable to load XML file!");
foreach ($xml->xpath('//lot') as $lot) {
echo $lot->numero_lot, 'type ', $lot->type, $lot->identification, ' />';
echo 'Prix maxi: ', $lot->tarif_max, ' Prix mini: ', $lot->tarif_min,
'
';
foreach( $lot->elements->element as $key => $data ){
$result=((string) $data->attributes());
if ($result='17') {
echo $data;
}
}
}
?>

But this just gives the data for every element. Any ideas on how to get just
the data for the element when the attribute is 17?

Terry
--
View this message in context: http://old.nabble.com/simplexml---can-it-do-what-I-need--tp2 7481222p27496987.html
Sent from the PHP - General mailing list archive at Nabble.com.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: simplexml - can it do what I need?

am 08.02.2010 15:36:48 von TerryA

Got it! I corrected my syntax and this code now works as far as I can tell:

$file=3D 'annnonces.xml';
// load file
$xml =3D simplexml_load_file($file) or die ("Unable to load XML file!");
foreach ($xml->xpath('//lot') as $lot) {
echo $lot->numero_lot, 'type ', $lot->type, $lot->identification, ' />';
echo 'Prix maxi: ', $lot->tarif_max, ' Prix mini: ', $lot->tarif_min,
'
';
=09foreach( $lot->elements->element as $key =3D> $data ){
=09$result=3D((string) $data->attributes());
=09if ($result == "1") {
echo "Nombre pièces: ", $data, '
';
=09}
=09if ($result == "3") {
echo "Surface: ", $data, "m2", '
';
=09}
=09if ($result == "7") {
echo "Nombre étoiles: ", $data, '
';
break;
=09}
=09}
}
?>

Output is:
201type APPARTEMENT STUDIOTEE2 106
Prix maxi: 475 Prix mini: 475
Nombre pièces: 1
Surface: 27m2
Nombre étoiles: 3

Hooray and thanks to all of you guys.

Terry
--=20
View this message in context: http://old.nabble.com/simplexml---can-it-do-w=
hat-I-need--tp27481222p27500759.html
Sent from the PHP - General mailing list archive at Nabble.com.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php