PHP SimpleXML use on complex XML to get data [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I try to get some data from some XML, which are very complex for me, so i ask you to help.
The XML is
<?xml version="1.0" encoding="UTF-8"?> <OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd"> <responseDate>2015-11-19T09:15:23Z</responseDate> <request verb="GetRecord" identifier="oai:nava.hu:Document-2359008" metadataPrefix="navadoc">http://10.10.1.6/dev/navadoc/index.php</request> <GetRecord>
<record>
<header>
<identifier>oai:nava.hu:Document-2359008</identifier>
<datestamp>2015-11-19T08:51:52Z</datestamp>
</header>
<metadata>
<navadoc:document xmlns:navadoc="http://nava.hu/oai-pmh/navadoc/xsd/" xmlns:document="http://nava.hu/oai-pmh/navadoc/xsd/document/" xsi:schemaLocation="http://nava.hu/oai-pmh/navadoc/xsd/ http://nava.hu/oai-pmh/navadoc/xsd/navadoc.xsd">
<document:main>
<id>2359008</id>
<title>Summa</title>
<provider id="nava">NAVA</provider>
<collection id="101">Magyar Televízió 1</collection>
<type>document</type>
</document:main>
<document:meta>
<broadcast>
<date type="day">2015-11-06</date>
<date type="start_time">2015-11-06 14:17:19</date>
<date type="end_time">2015-11-06 14:39:07</date>
<date type="scheduled">2015-11-06 14:15:00</date>
</broadcast>
<duration>00:21:48</duration>
<genre type="NAVA">háttér- és közéleti műsor</genre>
<genre type="EBU">3.1.3 TÁRSADALMI/POLITIKAI MŰSOROK</genre>
<date type="production_year">2015</date>
<description type="guide">(nincs kh.) Közben: Tőzsdenyitás, Egy Perc Híradó Feliratozva a Teletext 111. oldalán. (26 perc) Summa, a Magyar Televízió új háttérműsora. Miért kellett magyar tulajdonba venni az étkezési jegy üzletet? Miért kell visszaszerezni a külföldi cégektől a közüzemi vállalatokat,és miért kell nekünk magyar járműgyártás? Miért kerültek egyáltalán korábban külföldi cégek kezébe ezek a komoly nyereséget termelő vállalatok? Mitől jobb gazda a magyar állam, mint a külföldi befektető? Kérdések, amelyekre a legilletékesebbek válaszolnak a Summában, szerdán, az Este című műsor után. Hírek és ami mögöttük van.</description>
<description type="provider" />
<description type="technote" />
<description type="subtitle" />
<description type="main" />
<description type="private">- projekt: full FELDOLGOZÁS VÁLTOZÁSA 2015-08-18 ADÁSNAPTÓL! 1-es projektfeladatban lesznek részletesebb adatokkal, daraboláskor csak sablon+MSZI ha van Törzsidő: M1, péntek, 14:15 feldolgozás:kulcsképek, tárgyszavak; (új stáblista kissé nehezen olvasható...) Spotokkal megszakítva, de egyelőre egybevágjuk</description>
<attributes>
<color>Színes</color>
<sound>Hangos</sound>
<screenformat>N/A</screenformat>
</attributes>
<agelimit>korhatár nélkül</agelimit>
<nava>
<document_level>1</document_level>
<processing_level>3</processing_level>
<processing_state>2</processing_state>
<access since="2015-11-09">FREE</access>
<validated>false</validated>
<collectable>true</collectable>
<hasvideo>true</hasvideo>
<hassubtitle>true</hassubtitle>
<preview>00:00:00</preview>
</nava>
<language iso="hun">magyar</language>
<title type="Főcím" id="751954">Summa</title>
<image image="allframe/2015/11/06/m1-87212/m1-87212-08221900.jpg" time="00:00:00" />
<image image="allframe/2015/11/06/m1-87212/m1-87212-08230000.jpg" time="00:00:41" />
<image image="allframe/2015/11/06/m1-87212/m1-87212-08240000.jpg" time="00:01:41" />
<image image="allframe/2015/11/06/m1-87212/m1-87212-08250000.jpg" time="00:02:41" />
<image image="allframe/2015/11/06/m1-87212/m1-87212-08260000.jpg" time="00:03:41" />
<image image="allframe/2015/11/06/m1-87212/m1-87212-08270000.jpg" time="00:04:41" />
<image image="allframe/2015/11/06/m1-87212/m1-87212-08280000.jpg" time="00:05:41" />
<image image="allframe/2015/11/06/m1-87212/m1-87212-08290000.jpg" time="00:06:41" />
<image image="allframe/2015/11/06/m1-87212/m1-87212-08300000.jpg" time="00:07:41" />
<image image="allframe/2015/11/06/m1-87212/m1-87212-08310000.jpg" time="00:08:41" />
<image image="allframe/2015/11/06/m1-87212/m1-87212-08320000.jpg" time="00:09:41" />
<image image="allframe/2015/11/06/m1-87212/m1-87212-08330000.jpg" time="00:10:41" />
<image image="allframe/2015/11/06/m1-87212/m1-87212-08340000.jpg" time="00:11:41" />
<image image="allframe/2015/11/06/m1-87212/m1-87212-08350000.jpg" time="00:12:41" />
<image image="allframe/2015/11/06/m1-87212/m1-87212-08360000.jpg" time="00:13:41" />
<image image="allframe/2015/11/06/m1-87212/m1-87212-08370000.jpg" time="00:14:41" />
<image image="allframe/2015/11/06/m1-87212/m1-87212-08380000.jpg" time="00:15:41" />
<image image="allframe/2015/11/06/m1-87212/m1-87212-08390000.jpg" time="00:16:41" />
<image image="allframe/2015/11/06/m1-87212/m1-87212-08400000.jpg" time="00:17:41" />
<image image="allframe/2015/11/06/m1-87212/m1-87212-08410000.jpg" time="00:18:41" />
<image image="allframe/2015/11/06/m1-87212/m1-87212-08420000.jpg" time="00:19:41" />
<image image="allframe/2015/11/06/m1-87212/m1-87212-08430000.jpg" time="00:20:41" />
<image image="allframe/2015/11/06/m1-87212/m1-87212-08440000.jpg" time="00:21:41" />
<image image="allframe/2015/11/06/m1-87212/m1-87212-08440700.jpg" time="00:21:48" />
<image type="LOGÓ" subtype="Műsorszámlogó" id="2964186" image="gallery/keyframe/2015/03/17/m1-76264/m1-76264-03302700.jpg">Summa</image>
<image type="SZEMÉLY / CSOPORT" subtype="Személy / Személyek" time="00:01:28" id="3180774" image="gallery/keyframe/2015/11/06/m1-87212/m1-87212-08234700.jpg">Rogán Antal, miniszter, Miniszterelnöki Kabinet Iroda</image>
<image type="SZEMÉLY / CSOPORT" subtype="Személy / Személyek" time="00:02:33" id="3180778" image="gallery/keyframe/2015/11/06/m1-87212/m1-87212-08245200.jpg">György László phd, közgazdász</image>
<image type="SZEMÉLY / CSOPORT" subtype="Személy / Személyek" time="00:03:03" id="3180782" image="gallery/keyframe/2015/11/06/m1-87212/m1-87212-08252200.jpg">Horváth Dániel, vezető kutató, Migrációkutató Intézet</image>
<image type="SZEMÉLY / CSOPORT" subtype="Személy / Személyek" time="00:03:45" id="3180788" image="gallery/keyframe/2015/11/06/m1-87212/m1-87212-08260400.jpg">Lattmann Tamás, nemzetközi jogász</image>
<image type="INFORMÁCIÓS TARTALOM" subtype="Téma / Felirat / Inzert" time="00:07:15" id="3180790" image="gallery/keyframe/2015/11/06/m1-87212/m1-87212-08293400.jpg">Amerikai-magyar kereskedelmi kapcsolatok, 1989 óta</image>
<image type="SZEMÉLY / CSOPORT" subtype="Személy / Személyek" time="00:07:28" id="3180796" image="gallery/keyframe/2015/11/06/m1-87212/m1-87212-08294700.jpg">Kerekes György, igazgató, Nemzetközi tanácsadás, Századvég Zrt.</image>
<image type="SZEMÉLY / CSOPORT" subtype="Személy / Személyek" time="00:08:53" id="3180848" image="gallery/keyframe/2015/11/06/m1-87212/m1-87212-08311200.jpg">Colleen Bell, az USA magyarországi nagykövete</image>
<image type="SZEMÉLY / CSOPORT" subtype="Személy / Személyek" time="00:12:13" id="3180856" image="gallery/keyframe/2015/11/06/m1-87212/m1-87212-08343200.jpg">Mészáros László, elnök, Országos Magyar Méhészeti Egyesület</image>
<image type="SZEMÉLY / CSOPORT" subtype="Személy / Személyek" time="00:12:59" id="3180858" image="gallery/keyframe/2015/11/06/m1-87212/m1-87212-08351800.jpg">Kutasi Tamás, méhészmester</image>
<image type="SZEMÉLY / CSOPORT" subtype="Személy / Személyek" time="00:14:05" id="3180862" image="gallery/keyframe/2015/11/06/m1-87212/m1-87212-08362400.jpg">Petneházy Antal, méhész</image>
<image type="SZEMÉLY / CSOPORT" subtype="Személy / Személyek" time="00:14:50" id="3180864" image="gallery/keyframe/2015/11/06/m1-87212/m1-87212-08370900.jpg">Jandácsik Attila, Pest Megyei Méhészeti Szaktanácsadó</image>
<relation type="tartalmazza">2358946</relation>
<media format="REAL" bitrate="128" resolution="192x144" clipBegin="08:22:19" clipEnd="08:44:07" media="2015/11/06/m1-20151106-055500.rm" />
<media format="REAL" bitrate="512" resolution="384x288" clipBegin="01:16:06" clipEnd="01:37:54" media="2015/11/06/m1-20151106-055500-07061300-12035300.rm" />
<media format="MPG" bitrate="8000" resolution="704x576" clipBegin="01:16:06" clipEnd="01:37:54" media="2015/11/06/m1-87212-07061300-12035300.mpg" />
<media format="MP4" bitrate="768" resolution="352x288" clipBegin="01:16:06" clipEnd="01:37:54" media="Broadcast/2015/11/06/m1-20151106-055500-07061300-12035300.mp4" />
<track type="subtitle" format="srt" language="hun" file="2015/11/06/m1-20151106-055500-07061300-12035300.srt" />
<person type="contributor" id="103356" role="hírigazgató" sex="Férfi" comment="szerkesztőségvezető, MTV">Németh Zsolt</person>
<person type="contributor" id="133458" role="produkciós vezető" sex="Férfi" comment="produkciós vezető">Tóth István</person>
<person type="contributor" id="148662" role="kreatív producer" sex="Férfi" comment="kreatív producer">Pető Zoltán</person>
<subject source="Földrajzi név" id="30">Európai Unió</subject>
<subject source="Földrajzi név" id="1403">Egyesült Államok</subject>
<subject source="Tezaurusz" id="4905">méhészet</subject>
<subject source="Tezaurusz" id="4906">méz</subject>
<subject source="Tezaurusz" id="4942">bevándorlás</subject>
<subject source="Tezaurusz" id="8945">migráció</subject>
<subject source="Tezaurusz" id="11161">menekült</subject>
<workflow date="2015-11-09" time="07:48:03" user="265" name="mate">Létrehozás</workflow>
<workflow date="2015-11-16" time="10:17:52" user="488" name="vera">Feladat kiosztása</workflow>
<workflow date="2015-11-19" time="08:51:52" user="488" name="vera">Feldolgozás vége</workflow>
</document:meta>
</navadoc:document>
</metadata>
</record> </GetRecord> </OAI-PMH>
I want to get all record from
where type="SZEMÉLY / CSOPORT"
I dont know if the php simplexml is enougth for this, or i have to use some other object.

I think that you can use simplexml_load_file and then use xpath.
For example, lets assume your xml is in 'temp.xml'.
$xml = simplexml_load_file("temp.xml");
$xml->registerXPathNamespace('e', 'http://www.openarchives.org/OAI/2.0/');
This will find your 10 images from the xml:
$images = $xml->xpath('//e:image[#type="SZEMÉLY / CSOPORT"]');
Now you can loop the $images, and each $image is of type SimpleXMLElement.
Then you can get the value and the values of the properties for example:
foreach ($images as $image) {
$content = $image->__toString();
$type = $image->attributes()->type;
$subtype = $image->attributes()->subtype;
$time = $image->attributes()->time;
$attributes = $image->attributes()->id;
$image = $image->attributes()->image;
}

Related

php xml xpath how to extract desired match

I have question on how to access SourceUrl for image with width=400
images/di/47/6b/77/454430384d6d324b413332544a695675313851-400x400-0-0.jpg?p=p2.7f19fe93a466ae45afab&a=1&c=1&l=7000610&r=1&pr=1&lks=43998&fks=35198
By default it is showing me image with width=100 and somehow my xpath syntax is not picking up 400
<?php
$string = <<<XML
<imageList>
<image available="true" height="100" width="100">
<sourceURL>images/di/47/6b/77/454430384d6d324b413332544a695675313851-100x100-0-0.jpg?p=p2.7f19fe93a466ae45afab&a=1&c=1&l=7000610&r=1&pr=1&lks=43998&fks=35198</sourceURL>
</image>
<image available="true" height="200" width="200"><sourceURL>images/di/47/6b/77/454430384d6d324b413332544a695675313851-200x200-0-0.jpg?p=p2.7f19fe93a466ae45afab&a=1&c=1&l=7000610&r=1&pr=1&lks=43998&fks=35198</sourceURL></image>
<image available="true" height="300" width="300"><sourceURL>images/di/47/6b/77/454430384d6d324b413332544a695675313851-300x300-0-0.jpg?p=p2.7f19fe93a466ae45afab&a=1&c=1&l=7000610&r=1&pr=1&lks=43998&fks=35198</sourceURL></image>
<image available="true" height="400" width="400"><sourceURL>images/di/47/6b/77/454430384d6d324b413332544a695675313851-400x400-0-0.jpg?p=p2.7f19fe93a466ae45afab&a=1&c=1&l=7000610&r=1&pr=1&lks=43998&fks=35198</sourceURL></image>
<image available="true" height="569" width="500"><sourceURL>images/di/47/6b/77/454430384d6d324b413332544a695675313851-500x569-0-0.jpg?p=p2.7f19fe93a466ae45afab&a=1&c=1&l=7000610&r=1&pr=1&lks=43998&fks=35198</sourceURL></image></imageList>
XML;
$xml = simplexml_load_string($string);
$result = $xml->xpath("//image[#height='400']/sourceURL");
?>
Your xpath is all right, but the XML is not valid, see http://www.xmlvalidation.com
The & within your links will be parsed as start of a character entity --> error.
solution: exclude the text within <sourceURL> from parsing with <![CDATA[...]]>:
$x = <<<XML
<imageList>
<image available="true" height="100" width="100">
<sourceURL>
<![CDATA[images/di/47/6b/77/454430384d6d324b413332544a695675313851-100x100-0-0.jpg?p=p2.7f19fe93a466ae45afab&a=1&c=1&l=7000610&r=1&pr=1&lks=43998&fks=35198]]>
</sourceURL>
</image>
...
</imageList>
XML;
$xml = simplexml_load_string($x);
$result = $xml->xpath("//image[#height='400']/sourceURL")[0];
echo $result;
output:
images/di/47/6b/77/454430384d6d324b413332544a695675313851-400x400-0-0.jpg?p=p2.7f19fe93a466ae45afab&a=1&c=1&l=7000610&r=1&pr=1&lks=43998&fks=35198
see it working: http://codepad.viper-7.com/31PLr3
more on CDATA: What does <![CDATA[]]> in XML mean?

Feching the values from xml on the condition check

I got a problem
<?php
$movie_name = 'Dabangg';
$url = sprintf(
'http://api.themoviedb.org/2.1/Movie.search/en/xml/mapi/%s',
rawurlencode(trim($movie_name))
);
$xml = simplexml_load_file($url);
if (!$xml)
{
throw new RuntimeException(sprintf("Failed to open XML from URL '%s'.", $url));
}
$movies = $xml->movies->movie;
foreach ($movies as $movie)
{
$movie_id = $movie->id;
if ($movie_id && is_int($movie_id))
{
break;
}
}
echo $movie_id;
The above code I am using to fecth the movieid . Now I realized that when I search for a particular movie it fetches two movies result
<OpenSearchDescription xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
<opensearch:Query searchTerms="Dabangg"/>
<opensearch:totalResults>2</opensearch:totalResults>
<movies>
<movie>
<score/>
<popularity>3</popularity>
<translated>true</translated>
<adult>false</adult>
<language>en</language>
<original_name>Dabangg</original_name>
<name>Dabangg</name>
<alternative_name/>
<type>movie</type>
<id>44425</id>
<imdb_id>tt1620719</imdb_id>
<url>http://www.themoviedb.org/movie/44425</url>
<votes>6</votes>
<rating>5.6</rating>
<certification>PG-13</certification>
<overview>
Set in Uttar Pradesh, Dabangg is the story of a corrupt police officer played by Salman Khan and highlights the flaws and loopholes in the system. The film primarily deals with the unlawful practices in the states of UP and Bihar.
</overview>
<released>2010-09-10</released>
<images>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w92/lfI4ihj4qa8Obyst9miTSNzHCqF.jpg" size="thumb" width="92" height="138" id="4ea685f034f8633bdc00b45f"/>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w154/lfI4ihj4qa8Obyst9miTSNzHCqF.jpg" size="w154" width="154" height="231" id="4ea685f034f8633bdc00b45f"/>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w185/lfI4ihj4qa8Obyst9miTSNzHCqF.jpg" size="cover" width="185" height="278" id="4ea685f034f8633bdc00b45f"/>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w342/lfI4ihj4qa8Obyst9miTSNzHCqF.jpg" size="w342" width="342" height="513" id="4ea685f034f8633bdc00b45f"/>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w500/lfI4ihj4qa8Obyst9miTSNzHCqF.jpg" size="mid" width="500" height="750" id="4ea685f034f8633bdc00b45f"/>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/original/lfI4ihj4qa8Obyst9miTSNzHCqF.jpg" size="original" width="1000" height="1500" id="4ea685f034f8633bdc00b45f"/>
<image type="backdrop" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w300/sNoWUZD4S3C4gqWVwvifuiwsJ4m.jpg" size="thumb" width="300" height="169" id="4ea6860234f8633bdc00b477"/>
<image type="backdrop" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w780/sNoWUZD4S3C4gqWVwvifuiwsJ4m.jpg" size="poster" width="780" height="439" id="4ea6860234f8633bdc00b477"/>
<image type="backdrop" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w1280/sNoWUZD4S3C4gqWVwvifuiwsJ4m.jpg" size="w1280" width="1280" height="720" id="4ea6860234f8633bdc00b477"/>
<image type="backdrop" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/original/sNoWUZD4S3C4gqWVwvifuiwsJ4m.jpg" size="original" width="1920" height="1080" id="4ea6860234f8633bdc00b477"/>
</images>
<version>800</version>
<last_modified_at>2013-04-09 03:03:56 UTC</last_modified_at>
</movie>
<movie>
<score/>
<popularity>3</popularity>
<translated>true</translated>
<adult>false</adult>
<language>en</language>
<original_name>Dabangg 2</original_name>
<name>Dabangg 2</name>
<alternative_name>沙曼罕之爆裂警官 2</alternative_name>
<type>movie</type>
<id>147405</id>
<imdb_id>tt2112131</imdb_id>
<url>http://www.themoviedb.org/movie/147405</url>
<votes>2</votes>
<rating>5.8</rating>
<certification>NR</certification>
<overview>
Dabangg 2 is an upcoming Bollywood action film directed and produced by Arbaaz Khan under the banner of Arbaaz Khan Productions.It is a sequel to the 2010 film Dabangg and is written by Dilip Shukla. Story is set in the city of Kanpur
</overview>
<released>2012-12-21</released>
<images>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w92/5VW8AEfj4r3bu9MtvwvcbWAN53O.jpg" size="thumb" width="92" height="130" id="514727ab760ee3215200fe27"/>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w154/5VW8AEfj4r3bu9MtvwvcbWAN53O.jpg" size="w154" width="154" height="217" id="514727ab760ee3215200fe27"/>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w185/5VW8AEfj4r3bu9MtvwvcbWAN53O.jpg" size="cover" width="185" height="261" id="514727ab760ee3215200fe27"/>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w342/5VW8AEfj4r3bu9MtvwvcbWAN53O.jpg" size="w342" width="342" height="483" id="514727ab760ee3215200fe27"/>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w500/5VW8AEfj4r3bu9MtvwvcbWAN53O.jpg" size="mid" width="500" height="706" id="514727ab760ee3215200fe27"/>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/original/5VW8AEfj4r3bu9MtvwvcbWAN53O.jpg" size="original" width="1156" height="1632" id="514727ab760ee3215200fe27"/>
<image type="backdrop" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w300/rWGMdyTydjXXh9YphuU5gQ7wJ8X.jpg" size="thumb" width="300" height="169" id="51472838760ee3216f010a1c"/>
<image type="backdrop" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w780/rWGMdyTydjXXh9YphuU5gQ7wJ8X.jpg" size="poster" width="780" height="439" id="51472838760ee3216f010a1c"/>
<image type="backdrop" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w1280/rWGMdyTydjXXh9YphuU5gQ7wJ8X.jpg" size="w1280" width="1280" height="720" id="51472838760ee3216f010a1c"/>
<image type="backdrop" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/original/rWGMdyTydjXXh9YphuU5gQ7wJ8X.jpg" size="original" width="1920" height="1080" id="51472838760ee3216f010a1c"/>
</images>
<version>2</version>
<last_modified_at>2013-04-28 21:28:49 UTC</last_modified_at>
</movie>
</movies>
</OpenSearchDescription>
There are two movie Ids in this result. The Id which I want is 147405 but I am only getting the Id 44425. How can I get the other movie ID.
If we see the movie name I searched for <name>Dabangg 2</name> bit it send the <name>Dabangg</name> also . How can I make a check of name and take that id from the xml.
If you want to search for the exact term like
$movieName = 'Dabangg 2';
You need to wrap it in double-quotes:
$apiKey = '30f44b6ef9472d414e50d2acaa058b60';
$url = sprintf(
'http://api.themoviedb.org/2.1/Movie.search/en/xml/%s/"%s"',
$apiKey,
rawurlencode(trim($movieName))
);
Which will allow you to obtain the ID you're looking for immediately:
$xml = simplexml_load_file($url);
$movie = $xml->movies->movie;
echo $movieName, ': ', $movie->id; # Dabangg 2: 147405
break ends execution of current while. then do not use it, because after first finding match, it ends the while loop

PHP, DOM - remove first several nodes

The xml file looks like this:
<newVotes>
<image details="14.10.11" path="/test/content/pictures3/" image_name="PIC_524700002.jpg" id="1"/>
<image details="14.10.11" path="/test/content/pictures3/" image_name="PIC_5317gg.jpg" id="2"/>
<image details="14.10.11" path="/test/content/pictures3/" image_name="PIC_5393.jpg" id="3"/>
<image details="14.10.11" path="/test/content/pictures3/" image_name="PIC_2299.jpg" id="4"/>
<image details="14.10.11" path="/test/content/pictures3/" image_name="PIC_4977.jpg" id="5"/>
<image details="14.10.11" path="/test/content/pictures3/" image_name="PIC_4977BW.jpg" id="6"/>
<image details="14.10.11" path="/test/content/pictures3/" image_name="PIC_4914.jpg" id="7"/>
<image details="14.10.11" path="/test/content/pictures3/" image_name="flowergirl.jpg" id="8"/>
<image details="14.10.11" path="/test/content/pictures3/" image_name="PIC_5393.jpg" id="9"/>
<image details="14.10.11" path="/test/content/pictures3/" image_name="PIC_2299.jpg" id="10"/>
</newVotes>
I don't know how many "image" nodes will contain my xml, and how many of them goes to remove (it's dynamic amount variable comes from flash module) My question is how to delete for example first 5 "image" nodes in xml, using PHP?
Especialy added "id" nodes because I thought I can delete them using Xpath somehow, but with no lucky...
Thanks for any help.
Artur.
Done... It was pretty simple:
$filesLimit = $_POST['_limit'];
$rateElement = $news_dom->getElementsByTagName('image');
$numberOfFiles = $rateElement->length;
$m = $numberOfFiles - $filesLimit + 1;
if($numberOfFiles>=$filesLimit){
for ($i = 0; $i < $m; $i++) {
$nodesToRemove = $rateElement->item(0)->parentNode->removeChild($rateElement->item(0));
}
}

Storing the content of an xml child node based on a parent node's attribute in php

I'm trying to display the biggest image url returned from an xml result. So far the largest returned is 400 high so I hardcoded 400 in. If possible I would like to select just the largest in case in the future I get results that don't have a 400 height image in them.
I've tried
$x = file_get_contents($url);
$xml = simplexml_load_string($x);
$imageURL=$xml->categories->category->items->product->images->image[#height='400']->sourceURL;
Which gives me "syntax error, unexpected '=', expecting ']'".
And I also tried:
$imageURL= $xml->xpath("/categories/category/items/producct/images/image[#height='400']/sourceURL");
But got a bad link.
Here is the XML:
<images>
<image available="true" height="100" width="100">
<sourceURL>
Someurl.com
</sourceURL>
</image>
<image available="true" height="200" width="200">
<sourceURL>
Someurl.com
</sourceURL>
</image>
<image available="true" height="300" width="300">
<sourceURL>
Someurl.com
</sourceURL>
</image>
<image available="true" height="400" width="400">
<sourceURL>
Someurl.com
</sourceURL>
</image>
<image available="true" height="399" width="400">
<sourceURL>
Someurl.com
</sourceURL>
</image>
</images>
Any ideas?
->image[#height='400'] is a direct PHP array reference. This'd be interpreted as supressing errors (#) on a defined() constant (height), and trying to set its value via an assignment ='400'.
For your xpath version, remember that an xpath query returns a DOMNodeList, not an actual DOMElement. To get the URLs you need from the query results, you have to ierate over the node list:
$nodes = $xpath->query(...) {
foreach($nodes as $node) {
$url = $node->nodeValue;
}
Below code might help...
$xmlSQLProcedures = new DOMXPath($xmlSQLProcedures);
$strProcedureName = $xmlSQLProcedures->query("//SQLProcedure[#ID='$sSQLProcedureID']")->item(0)->nodeValue;
$nodeParameters = $xmlSQLProcedures->query("//SQLProcedure[#ID='$sSQLProcedureID']/Parameters/Parameter");
$ParamCount = $nodeParameters->length-1;
for ($i=0;$i<=$ParamCount;$i++) {
echo $nodeParameters->item($i)->getAttribute("Name").'<br>';
}
<?xml version="1.0" encoding="UTF-8"?>
<SQLProcedures>
<!-- ********** FOR KEYWORD IN LOCAL LANGUAGE ************* -->
<SQLProcedure ID="001070001">
<Name>P_ManipulateKeywordsInLL</Name>
<Parameters>
<Parameter Name="LanguageId"/>
<Parameter Name="KeywordId"/>
<Parameter Name="KeywordInLL"/>
<Parameter Name="ActionFor"/>
<Parameter Name="KeywordInLLId"/>
<Parameter Name="Keyword"/>
<Parameter Name="KeywordList"/>
<Parameter Name="SessionId"/>
<Parameter Name="WarehouseId"/>
</Parameters>
</SQLProcedure>
</SQLProcedures>

Get image from XML and display with PHP?

I'm trying to use an API with the following XML:
<movies>
<movie>
<images>
<image type="poster" url="http://cf1.imgobject.com/posters/b7a/4bc91de5017a3c57fe00bb7a/i-am-legend-original.jpg" size="original" width="675" height="1000" id="4bc91de5017a3c57fe00bb7a"/>
<image type="poster" url="http://cf1.imgobject.com/posters/b7a/4bc91de5017a3c57fe00bb7a/i-am-legend-mid.jpg" size="mid" width="500" height="741" id="4bc91de5017a3c57fe00bb7a"/>
<image type="poster" url="http://cf1.imgobject.com/posters/b7a/4bc91de5017a3c57fe00bb7a/i-am-legend-cover.jpg" size="cover" width="185" height="274" id="4bc91de5017a3c57fe00bb7a"/>
</images>
</movie>
</movies>
Can someone give me an example of the PHP code I should use to get the image url where size="cover"?
Thanks.
SimpleXML can do this quite, for lack of a better word, simply:
$xml = new SimpleXMLElement($str);
$xpath = $xml->xpath("/movies/movie/images/image[#size = 'cover']");
echo $xpath[0]['url'];
Load the xml with a XML Parser, DOMDocument, SimpleXML etc.
http://se.php.net/manual/en/refs.xml.php
Then you can use XPath to select the image.
http://www.w3schools.com/xpath/xpath_syntax.asp
XPath to grab movie with attribute size = cover
/movies/movie/images/image[#size=cover]
Looks like a good tutorial: http://ditio.net/2008/12/01/php-xpath-tutorial-advanced-xml-part-1/
<?php
$string = <<<XML
<?xml version='1.0'?>
<movies>
<movie>
<images>
<image type="poster" url="http://cf1.imgobject.com/posters/b7a/4bc91de5017a3c57fe00bb7a/i-am-legend-original.jpg" size="original" width="675" height="1000" id="4bc91de5017a3c57fe00bb7a"/>
<image type="poster" url="http://cf1.imgobject.com/posters/b7a/4bc91de5017a3c57fe00bb7a/i-am-legend-mid.jpg" size="mid" width="500" height="741" id="4bc91de5017a3c57fe00bb7a"/>
<image type="poster" url="http://cf1.imgobject.com/posters/b7a/4bc91de5017a3c57fe00bb7a/i-am-legend-cover.jpg" size="cover" width="185" height="274" id="4bc91de5017a3c57fe00bb7a"/>
</images>
</movie>
</movies>
XML;
$xml = simplexml_load_string($string);
foreach($xml->movie->images->image as $image) {
if(strcmp($image['size'],"cover") == 0)
echo $image['url'];
}
?>
$xml = simplexml_load_string($string2);
foreach($xml->movie->images->image as $image) {
if(strcmp($image['size'],"cover"))
// echo $image['url'];
?>
" width="200px" height="100px">

Categories