0
votes

I am using xml data using a url & because the xml is too long I just want to check condition to get particular node values only :-

Here is my code :-

<?php
$doc = new DOMDocument('1.0', 'utf-8');
$doc->load("https://retailapi.apparel21.com/RetailAPI/products?countrycode=au");

$xpath = new DOMXpath($doc);
/*foreach ($xpath->query("/Products/Product[Code='00122']") as $node)
{
    echo $node->nodeValue;
    echo "Hi<br>";
}*/

echo $xpath->query("/Products/Product[Code='00122']")->item(0)->nodeValue;
?>

As you can see that I already used foreach loop & successfully executed the condition but.....the thing is inside it, it prints whole data of that all the nodes of it's parent node.

Confused? :)

Ok no worries, just execute this url: https://retailapi.apparel21.com/RetailAPI/products?countrycode=au; please click on Proceed anyway button then wait for some time.

There are many Product tags...now I want the data of the following nodes :- Id Code Name Description

whose code=00122 that's the first product's data.

I applied foreach then it printed all node's data of that product. I applied simple single statement but then also it printed all node's data :(

And one more thing is can't it be done by simplexml_load_file function?

One more thing :- You can see I am loading url, so the thing is it will read the whole xml first. Can't we query in this itself so that it will only take only related product tags so the loading time can be reduced.

Can anyone please help?

1
Could you share an excerpt of the relevant XML structure? - Ja͢ck
Hi Jack, Can you please tell me what excerpt means? If you want how xml is looking like......then it's this :- retailapi.apparel21.com/RetailAPI/products?countrycode=au I have already mention in question jack :) - Kaushal
A small but relevant part of the XML structure that you need to query. - Ja͢ck
Hi Jack, Here is a small part of the xml :- - Kaushal
@Kaushal: edit your question to include the small and relevant piece of XML. Your link doesn't load for me. Please read the help center before asking any further questions. - Madara's Ghost

1 Answers

0
votes

You're nearly there. Replace DOMXpath::query() with DOMXpath::evaluate(). It allows to use Xpath expressions that return scalars like strings. Now the second argument of evaluate() (or query()) is the context, so you can iterate all nodes from one expression and fetch the details using xpath expressions depending on a context node:

$doc = new DOMDocument('1.0', 'utf-8');
$doc->load("https://retailapi.apparel21.com/RetailAPI/products?countrycode=au");
$xpath = new DOMXpath($doc);

$result = [];
foreach ($xpath->evaluate("/Products/Product[Code='00122']") as $node) {
  $result[] = [
    'id' => $xpath->evaluate('string(Id)', $node),
    'code' => $xpath->evaluate('string(Code)', $node),
    'name' => $xpath->evaluate('string(Name)', $node),
  ];   
}

var_dump($result);

A call like $xpath->evaluate('Id', $node) would return a list with all Id element nodes that are children of $node. The Xpath function string() casts the first node in this list into a string and returns it. If the list is empty the result will be an empty string.