Read multiple tag attribute in an XML

Asked

Viewed 116 times

0

I want to read the XML and get the tag data <product zupid="fe36ddb561f0cd98f693722931fa2b83"> but I’m not getting it.

Part of XML:

<?xml version="1.0" encoding="UTF-8"?>
-<products xsi:schemaLocation="http://zanox.com/productdata/exportservice/v1 http://productdata.zanox.com/exportservice/schema/export_new_retail.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
-<product zupid="fe36ddb561f0cd98f693722931fa2b83">
<name>Raquete Winmax Thrones 300</name>
<number>X68-0092-010</number>
<deepLink>http://ad.zanox.com/ppc/?45860320C867715000&ULP=[[pdp/X68-0092-010?campaign=me_xml-afiliados&utm_source=me-s_zanx_&utm_medium=xml&utm_campaign=me-s_zanx-xml-_-jogos_me-s_zanx-xmlwinmax-_-raquete-winmax-thrones-300-_-::N3T:AF-AW-00-00-00-TD:N3T::_{{::N3T:AF-AW-00-00-00-TD:N3T::}}&utm_term=]]</deepLink>
<price>99.99</price>
<oldPrice>190.46</oldPrice>
<currencyCode>BRL</currencyCode>
<description>Raquete Badminton Thrones vermelha. Produzida em carbono grafite, cabeça 100% de alumínio + eixo de aço, é resistente e de alta qualidade. Alcança a flexibilidade de : 7.5 - 8.0 e 26-28lbs. Cabo ...</description>
<longDescription>Raquete Badminton Thrones vermelha. Produzida em carbono grafite, cabeça 100% de alumínio + eixo de aço, é resistente e de alta qualidade. Alcança a flexibilidade de : 7.5 - 8.0 e 26-28lbs. Cabo ...</longDescription>
<merchantCategoryPath>Jogos</merchantCategoryPath>
<largeImage>https://static.netshoes.com.br/produtos/raquete-winmax-thrones-300/10/X68-0092-010/X68-0092-010_detalhe1.jpg</largeImage>
<lastModified>2019-07-02T09:11:00</lastModified>
<stockAmount>null</stockAmount>
<program>12078</program>
<ean>6947663402090</ean>
<manufacturer>Winmax</manufacturer>
<gender>Unissex</gender>
</product>
...

I’m using:

$url = "...";

$feed = simplexml_load_file($url);

foreach($feed->children() as $item){
   $IdProduto = trim($item->product('zupid');
   echo "$IdProduto<br>";
}

2 answers

0

If you want to make a loop by all tags product, just do:

$feed = simplexml_load_file($url);
foreach($feed->children() as $item) {
    if($item->getName() == 'product') // somente se for product, eu imprimo o zupid
        echo $item['zupid']. "<br>";
}

Another option is to use DOMDocument along with DOMXPath to fetch the tags product and get the zupid of these:

$dom = new DOMDocument;
$dom->load($url);
$xpath = new DOMXPath($dom);
// procura as tags "product" dentro de "products"
foreach ($xpath->query("/products/product") as $p) {
    echo $p->getAttribute('zupid'). "<br>"; // pega o zupid
}

0

Suppose you have XML, an implementation can be:

<?php   
class simple_xml_extended extends SimpleXMLElement
{
    public    function    Attribute($name)
    {
        foreach($this->Attributes() as $key=>$val)
        {
            if($key == $name)
                return (string)$val;
        }
    }

}

$string = <<<XML
<?xml version='1.0'?>
<document>
 <product zupid="fe36ddb561f0cd98f693722931fa2b83"></product>
</document>
XML;

$xml = simplexml_load_string($string,"simple_xml_extended");

echo $xml->product->Attribute('zupid');

?>
  • Hello Bruno. And how would you look to read all the product id of an XML file, which is a lot of records, as would be the foreach with PHP? Thank you for your reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.