PHP XML XPath - Stop duplicate nodes - php

Im trying to parse an XML document using the XPath element in simple XML. However this script below (When searching for the entry "U2" in the last.fm API) returns:
Passengers Passengers Bono Passengers Bono U2 and Green Day Passengers
Bono U2 and Green Day R.E.M. Passengers Bono U2 and Green Day R.E.M.
INXS
As you can see there are repeating nodes. Is there a way that I can stop duplicate/repeating nodes from being shown?
(PHP Code)
$xmlmusic = new SimpleXMLElement($result);
$releases = $xmlmusic->xpath('artist/similar/artist');
foreach ($releases as $artist) {
$artistResult .= $artist->name . PHP_EOL;
echo $artistResult;}
(XML Document)
<?xml version="1.0" encoding="utf-8"?>
<lfm status="ok">
<artist>
<name>U2</name>
<mbid>704acdbb-1415-4782-b0b6-0596b8c55e46</mbid>
<url>http://www.last.fm/music/U2</url>
<image size="small">http://userserve-ak.last.fm/serve/34/107345.jpg</image>
<streamable>1</streamable>
<stats>
<listeners>2613654</listeners>
<playcount>96947986</playcount>
</stats>
<similar>
<artist>
<name>Passengers</name>
<url>http://www.last.fm/music/Passengers</url>
<image size="small">http://userserve-ak.last.fm/serve/34/4826014.jpg</image>
</artist>

I'm assuming that your XML snippet is only a small sample of the full results, so...
you need to modify your foreach loop to check if the artist currently being processed has been seen before. The easiest way is to use an array:
$seen = array();
foreach ($releases as $artist) {
if (!isset($seen[$artist->name])) {
$seen[$artist->name] = true;
}
}
Once the loop's done, you've got a nice array with each artist being a key in $seen. To replicate your simple string concatenation, you simply implode/echo:
echo implode(array_keys(', ', $seen));

Related

How to iterate through an XML element node with dynamic children

I currently have the following XML structure:
<root>
<maininfo>
<node>
<tournament_id>3100423</tournament_id>
<games>
<a_0>
<id>23523636</id>
<type>
<choice_4>
<choice_id>345</choice_id>
<choice_4>
<choice_9>
<choice_id>345</choice_id>
<choice_9>
... etc
</type>
</a_0>
<a_1></a_1>
<a_2></a_2>
...etc
</games>
</info>
</node>
</root>
I can easily get the id of the first node element "a_0" by just doing:
maininfo[0]->a_3130432[0]->games[0]->a_1[0]->id;
My issue is:
How do I automatically iterate (with a foreach) through all a_0, a_1, a_2 and get the values of each of these node elements and all of their children like "345" in <choice_id>345</choice_id>?
The ending numbers of a_0, a_1 + the children of choice_4, choice_9, are dynamically created and there are no logic in the _[number] counting up with +1 for each next element.
As it has been outlined previously on Stackoverflow (for example in Read XML dynamic PHP) and as well generally in the PHP manual (for example in Basic SimpleXML usage), you can iterate over all child elements by using foreach.
For example to go over all a_* elements, it's just
foreach ($xml->maininfo->node->games[0] as $name => $a) {
echo $name, "\n";
}
Output:
a_0
a_1
a_2
You then want to iterate over these their ->type children again. This is possible in pure PHP by putting one foreach into a another:
foreach ($xml->maininfo->node->games[0] as $name => $a) {
echo $name, "\n";
if (!$a->type[0]) {
continue;
}
foreach ($a->type[0] as $name => $choice) {
echo ' +- ', $name, "\n";
}
}
This now outputs:
a_0
+- choice_4
+- choice_9
a_1
a_2
This starts to get a bit complicated. As you can imagine since XML is famous for it's tree structures, you're not the first one running into this problem. Therefore a query-language to get elements from an XML document has been invented: Xpath.
With Xpath you can access XML data as if it was a file-system. As I know that each a_* element is a child of games and each choice_* element a child of type, it's pretty straight forward:
/*/maininfo/node/games/*/type/*
^ ^ ^
| | choice_*
root |
a_*
In PHP Simplexml this looks like:
$choices = $xml->xpath('/*/maininfo/node/games/*/type/*');
foreach ($choices as $choice) {
echo $choice->getName(), ': ', $choice->choice_id, "\n";
}
Output:
choice_4: 345
choice_9: 345
As this example shows, the data is now retrieved with a single foreach.
If you as well need access to the <a_*> elements, you need to have multiple foreach's or your own iteration but that is even a more advanced topic which I'd say would extend over the limits of your question.
I hope this is helpful so far. See as well SimpleXMLElement::children() which also gives all children (like ->games[0] in the first example). All example codes are as well available as a working, interactive online-demo.
If I understand it well, you can do something like:
for($i = 0; $i < $max; ++$i){
$a = $parentNode->{'a_'.$i};
}
You can do this very easily using SimpleXML :
<?php
$xmlStr = "<?xml version='1.0' standalone='yes'?>
<root>
<maininfo>
<node>
<tournament_id>3100423</tournament_id>
<games>
<a_0>
<id>23523636</id>
<type>
<choice_4>
<choice_id>345</choice_id>
</choice_4>
<choice_9>
<choice_id>345</choice_id>
</choice_9>
</type>
</a_0>
<a_1></a_1>
<a_2></a_2>
</games>
</node>
</maininfo>
</root>";
$xmlRoot = new SimpleXMLElement($xmlStr);
$i = 0;
foreach($xmlRoot->maininfo[0]->node[0]->games[0] as $a_x)
{
echo $i++ . " - " . htmlentities($a_x->asXML()) . "<br/>";
}
?>
I have modified some parts of your XML string to make it syntactically correct. You can view the results at http://phpfiddle.org/main/code/56q-san

nested foreach loop in PHP 5

So here is the deal:
I have an array that contains manufacturers' names for smartphones and an XML file that holds specifications for each specific phone.
What I am trying to do is to group phones based on manufacturer. I am using nested FOREACH loop to do this. And here is my code:
foreach ($makes as $manufacturer){
echo '<div>'.'<h2>'.$manufacturer.'</h2>'.'<hr>';
foreach ($xml as $item){
if($item->make == $manufacturer){
$src = $item->thumb;
echo '<div>'."<img src='$src' width='100'/>".'<h1>'.
$item->make.' '.$item->model.'</h1>'.'</div>';
}
}
echo '</div>';
}
The problem with this approach is that for some reason my page displays ONLY THE FIRST phone for every manufacturer, instead of going through every model.
What am I doing wrong here?
Here is XML sample:
<?xml version="1.0" encoding="utf-8"?>
<items>
<item>
<make></make>
<model></model>
<thumb></thumb>
</item>
</items>
Also, if I remove outer loop, code works perfectly and displays every phone from the XML
You have a typo:
echo '<div>'."<img src='$src' width='100'/>".'<h1>'.
$item->make.' '.$item->model.'</h1>'.
should be
echo '<div>'."<img src='$src' width='100'/>".'<h1>'.
$item->make.' '.$item->model.'</h1>';
Note the ";" at the end of the echo.

php only appending 3 out of 5 child nodes

This code is only appending 3 of the 5 name nodes. Why is that?
Here is the original XML:
It has 5 name nodes.
<?xml version='1.0'?>
<products>
<product>
<itemId>531670</itemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<categoryPath>
<category><name>Buy</name></category>
<category><name>Car, Marine & GPS</name></category>
<category><name>Car Installation Parts</name></category>
<category><name>Deck Installation Parts</name></category>
<category><name>Antennas & Adapters</name></category>
</categoryPath>
</product>
</products>
Then is run this PHP code. which is suppossed to appened ALL name nodes into the product node.
<?php
// load up your XML
$xml = new DOMDocument;
$xml->load('book.xml');
// Find all elements you want to replace. Since your data is really simple,
// you can do this without much ado. Otherwise you could read up on XPath.
// See http://www.php.net/manual/en/class.domxpath.php
//$elements = $xml->getElementsByTagName('category');
// WARNING: $elements is a "live" list -- it's going to reflect the structure
// of the document even as we are modifying it! For this reason, it's
// important to write the loop in a way that makes it work correctly in the
// presence of such "live updates".
foreach ($xml->getElementsByTagName('product') as $product ) {
foreach($product->getElementsByTagName('name') as $name ) {
$product->appendChild($name );
}
$product->removeChild($xml->getElementsByTagName('categoryPath')->item(0));
}
// final result:
$result = $xml->saveXML();
echo $result;
?>
The end result is this and it only appends 3 of the name nodes:
<?xml version="1.0"?>
<products>
<product>
<itemId>531670</itemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<name>Buy</name>
<name>Antennas & Adapters</name>
<name>Car Installation Parts</name>
</product>
</products>
Why is it only appending 3 of the name nodes?
You can temporarily add the name elements to an array before appending them, owing to the fact that you're modifying the DOM in real time. The node list generated by getElementsByTagName() may change as you are moving nodes around (and indeed that appears to be what's happening).
<?php
// load up your XML
$xml = new DOMDocument;
$xml->load('book.xml');
// Array to store them
$append = array();
foreach ($xml->getElementsByTagName('product') as $product ) {
foreach($product->getElementsByTagName('name') as $name ) {
// Stick $name onto the array
$append[] = $name;
}
// Now append all of them to product
foreach ($append as $a) {
$product->appendChild($a);
}
$product->removeChild($xml->getElementsByTagName('categoryPath')->item(0));
}
// final result:
$result = $xml->saveXML();
echo $result;
?>
Output, with all values appended:
<?xml version="1.0"?>
<products>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<name>Buy</name><name>Car, Marine & GPS</name><name>Car Installation Parts</name><name>Deck Installation Parts</name><name>Antennas & Adapters</name></product>
</products>
You're modifying the DOM tree as you're pulling results from it. Any modifications to the tree that cover the results of a previous query operation (your getElementsByTagName) invalidate those results, so you're getting undefined results. This is especially true of operations that add/remove nodes.
You're moving nodes as you're iterating through them so 2 are being skipped. I'm not a php guy so I can't give you the code to do this, but what you need to do is build a collection of the name nodes and iterate through that collection in reverse.
A less complicated way to do it is to manipulate the nodes with insertBefore
foreach($xml->getElementsByTagName('name') as $node){
$gp = $node->parentNode->parentNode;
$ggp = $gp->parentNode;
// move the node above gp without removing gp or parent
$ggp->insertBefore($node,$gp);
}
// remove the empty categoryPath node
$ggp->removeChild($gp);

How to parse a specific xml file using PHP simpleXML

I know there is already a lot of topics about simpleXML and PHP, but I need help with an specific xml code.
<vitrine>
<canal>Hotwords</canal>
<product id="0">
<descricao>MP3 Apple iPod Class...</descricao>
<loja>ApetreXo.com</loja>
<preco>à vista R$765,22</preco>
<urlImagem>http://imagem.domain.com.br/thumbs/ensopado/18/80x80_107156_1.jpg</urlImagem>
<urlProduto>http://domain.com.br/tr/rd?o=BiY4C2UnHQ0LOWgyGjc3NRFp-</urlProduto>
</product>
<product id="1">
<descricao>TV Sony Bravia 3D LE...</descricao>
<loja>Fast Shop.com.b...</loja>
<preco>10 x R$299,90</preco>
<urlImagem>http://imagem.domain.com.br/thumbs/ensopado/2852/80x80_319373_1.jpg</urlImagem>
<urlProduto>http://domain.com.br/tr/rd?o=JDEn-</urlProduto>
</product>
</vitrine>
I need a foreach to get the data from each "product" like this:
<?
$feedUrl = 'url to xml file';
$rawFeed = file_get_contents($feedUrl);
$xml = simplexml_load_string($rawFeed);
foreach ($item ...????? ?)
{
}
How can I do this foreach to get the data. I tried all that I know without success.
Thanks.
First, you need <?xml version="1.0" encoding="UTF-8"?> as the first line in your XML, otherwise it's not valid. Then, and this will help you debug all sorts of things over your coding career, try this line:
echo "<pre>".print_r($xml,true)."</pre>";
That will give you the exact layout of the object you get back from the simplexml_load_string() call. From there, since you have a visual of the object layout, you should be able to figure out how to parse it. Incidentally, in your case, I think you'd need to do something like:
foreach($xml->vitrine as $element) {
// your code goes here
}
It seems PHP gets rid of evrything after the whitespace, because product id was changed to just product. Anyway here's the code.
<?php
$v = <<<ABC
<vitrine>
<canal>Hotwords</canal>
<product id="0">
<descricao>MP3 Apple iPod Class...</descricao>
<loja>ApetreXo.com</loja>
<preco>à vista R$765,22</preco>
<urlImagem>http://imagem.domain.com.br/thumbs/ensopado/18/80x80_107156_1.jpg</urlImagem>
<urlProduto>http://domain.com.br/tr/rd?o=BiY4C2UnHQ0LOWgyGjc3NRFp-</urlProduto>
</product>
<product id="1">
<descricao>TV Sony Bravia 3D LE...</descricao>
<loja>Fast Shop.com.b...</loja>
<preco>10 x R$299,90</preco>
<urlImagem>http://imagem.domain.com.br/thumbs/ensopado/2852/80x80_319373_1.jpg</urlImagem>
<urlProduto>http://domain.com.br/tr/rd?o=JDEn-</urlProduto>
</product>
</vitrine>
ABC;
$xml = simplexml_load_string($v);
//print_r($xml);
foreach ($xml->product as $c){
echo $c->loja; //echoing out value of 'loja'
}
Outputs
ApetreXo.com
Fast Shop.com.b...

Using SimpleXML to loop through muliple entries

Hi Im trying to parse an xml feed using simplexml in php.
The xml feed is laid out as follows:
<Member>
<MemberType>Full</MemberType>
<JoinDate>2010-06-12</JoinDate>
<DataType>A</DataType>
<Data>
<FirstName>Ted</FirstName>
<LasttName>Smith</LasttName>
<Data1>56</Data1>
<Data2>100</Data2>
<Data3>120</Data3>
</Data>
</Member>
<Member>
<MemberType>Full</MemberType>
<JoinDate>2010-06-12</JoinDate>
<DataType>B</DataType>
<Data>
<FirstName>Ted</FirstName>
<LasttName>Smith</LasttName>
<Data1>57</Data1>
<Data2>110</Data2>
<Data3>130</Data3>
</Data>
</Member>
<Member>
<MemberType>Full</MemberType>
<JoinDate>2010-06-12</JoinDate>
<DataType>C</DataType>
<Data>
<FirstName>Ted</FirstName>
<LasttName>Smith</LasttName>
<Data4>58</Data4>
<Data5>115</Data5>
<Data6>230</Data6>
</Data>
</Member>
where the member element loops over and over again in the xml doc, but the data inside it changes. What im trying to do is enter all the data for certain members into an sql database. So ideally i want to enter this all in one line in the db. The xml feed contains different member types, like 'full' or 'associate'.
At the moment i am trying to loop through all the full members, and get all the data for this particular member. The data for each member is broken up into three parts each with a separate member tag, so above Ted Bloggs has data in three member tags, where Datatype is A, B and C
$PLF = simplexml_load_file('../XML/Members.xml');
foreach ($PLF->Root->xpath('//Member') as $member) {
if ($member->MemberType == 'Full') {
echo $member->MemberType.'<br/>';
echo $member->JoinDate.'<br />';
echo $member->DataType.'<br/>';
echo $member->Data->FirstName.'<br/>';
echo $member->Data->LastName.'<br/>';
echo $member->Data->Data1.'<br/>';
echo $member->Data->Data2.'<br/>';
echo '<br />';
}}
the code i have at the moment can only pull data from the first type (type A) in each loop, and i really want to combine all types A, B, and C into the same loop. So i can get all the data for each member like Ted Smith into one line in the DB.
I use simple xml to read some remote files and loop thru them as well. This is my code:
$sUrl = "some url";
$sContent = file_get_contents($sUrl);
$oXml = new SimpleXMLElement($sContent);
$aReturn = array();
foreach ($oXml->children() as $oStation)
{
$iRackId = (int)$oStation->rack_id;
$dLong = (double)str_replace(",", ".", $oStation->longitute);
$dLati = (double)str_replace(",", ".", $oStation->latitude);
$sDescription = (string)$oStation->description;
$aRes = array();
$aRes['rack_id'] = $iRackId;
$aRes['longitute'] = $dLong;
$aRes['latitude'] = $dLati;
$aRes['description'] = utf8_decode($sDescription);
if ($dLong > 0 && $dLati > 0)
$aReturn[$iRackId] = $aRes;
}
What I do is I put the result of the XML file into an array. Later in the code I save that data to the database as well.
Hope this helped you. I don't use xpath... had nothing but problems with it and didn't had the time to sort them out. This seems to work for me though.
Br,
Paul Peelen

Categories