xml processing using php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
baDibere
Forum Newbie
Posts: 4
Joined: Sun Aug 22, 2010 11:54 am

xml processing using php

Post by baDibere »

Hi,

I need help about XML processing.

Assume that there is a XML file that contains following code:

Code: Select all

<app>
   <description xml:lang="en">english content</description>
   <description xml:lang="fr">contenu en français</description>
</app>
Here I want to get data according to xml:lang attribute (or whatever else). But using SimleXML, I couldn't do.

For examle, I use

Code: Select all

<?php
$xml = new SimpleXMLElement($xmldata);
echo $xml->app->description[0]['xml:lang'];
?>
I expect that result will be "en". But result is empty.

Any ideas?
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: xml processing using php

Post by yacahuma »

Do you have the full xml? This has to do with namespaces
baDibere
Forum Newbie
Posts: 4
Joined: Sun Aug 22, 2010 11:54 am

Re: xml processing using php

Post by baDibere »

Code: Select all

<PISI>
<Package>
<Name>pida</Name>
<Summary xml:lang="en">
A program written to help you practice ear training
</Summary>
<Summary xml:lang="tr">Kulak eğitimi için yazılmış uygulama</Summary>
<Description xml:lang="en">
Solfege is a free ear training program written in python using gtk+ and PyGTK.
</Description>
<Description xml:lang="tr">
Solfege, kulak eğitimi için, Gtk+ ve PyGTK kullanılarak Python'da yazılmış ücretsiz bir uygulamadır.
</Description>
<IsA>app:gui</IsA>
<PartOf>programming.environment</PartOf>
<License>as-is</License>
<RuntimeDependencies>
<Dependency>pygtk</Dependency>
<Dependency>kiwi</Dependency>
<Dependency>vte</Dependency>
<Dependency>gnome-python</Dependency>
</RuntimeDependencies>
<History>
<Update release="2">
<Date>2008-01-06</Date>
<Version>0.5.1</Version>
<Comment>
Add patch for controlling editors' availability on first screen.
</Comment>
<Name>xxxxxxxxxxxxx</Name>
<Email>xxxxxxxxxxxx@gmail.com</Email>
</Update>
</History>
<Build>1</Build>
<Distribution>Pardus</Distribution>
<DistributionRelease>2009</DistributionRelease>
<Architecture>i686</Architecture>
<InstalledSize>1692705</InstalledSize>
<PackageSize>320968</PackageSize>
<PackageHash>0a3b52fc6c228382c3d44eb8d4fa452dc45044d4</PackageHash>
<PackageURI>pida-0.5.1-2-1.pisi</PackageURI>
<Source>
<Name>pida</Name>
<Homepage>http://pida.co.uk/</Homepage>
<Packager>
<Name>Jérôme Schneider</Name>
<Email>jerome.schneider@gmail.com</Email>
</Packager>
</Source>
</Package>
</PISI>
For example, i want to get tr summary here. How can i do?
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: xml processing using php

Post by yacahuma »

are you creating this xml or just consuming it? The first easy thing will be to change xml:lang for just lang
I think you have to specify a name space if you us xml:lang. The problem I have so far is that I did a little test,
I added to namespace to the xml, and I still cannot get the property. I am not sure what I am doing wrong.

But if you use lang="en", it will work for sure.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: xml processing using php

Post by requinix »

The namespace URI for xml is "http://www.w3.org/XML/1998/namespace", so

Code: Select all

$node->attributes("http://www.w3.org/XML/1998/namespace")->lang
Alternatively, you can use the prefix itself.

Code: Select all

$node->attributes("xml", true)->lang
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: xml processing using php

Post by yacahuma »

Man I goit it working, what a pain!!!

Code: Select all

$xml = simplexml_load_file('file.xml');
foreach ($xml as $entry){
  echo '<br>Content:' . (string) $entry;
   $attributes = $entry->attributes('xml',true);
   echo  '<br />LANG' . $attributes['lang'];
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: xml processing using php

Post by yacahuma »

I did not notice, that someone already posted the solution, o well.
baDibere
Forum Newbie
Posts: 4
Joined: Sun Aug 22, 2010 11:54 am

Re: xml processing using php

Post by baDibere »

Thank you. I'll try it asap. :)
baDibere
Forum Newbie
Posts: 4
Joined: Sun Aug 22, 2010 11:54 am

Re: xml processing using php

Post by baDibere »

yacahuma's code is working. Thank you so much. :drunk:

I think tasairi's code must end with ['lang'] instead of ->lang. Am I wrong?
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: xml processing using php

Post by yacahuma »

to tell you the truth, I am not sure why some example access the data like object properties, other access like an array(like in my code). maybe tasairis can share some light.
Post Reply