Access an attribute of a String xml

0

Amazon Market Place Id returns an xml with the list of X products, but I do not know how to access let's say to the attribute < ns2:Title> , the answer I get is the following:

SimpleXMLElement Object ( [ListMatchingProductsResult] => SimpleXMLElement Object ( [Products] => SimpleXMLElement Object ( [Product] => Array ( [0] => SimpleXMLElement Object ( [Identifiers] => SimpleXMLElement Object ( [MarketplaceASIN] => SimpleXMLElement Object ( [MarketplaceId] => XXXXXXXXX [ASIN] => B01N9BACDM ) ) [AttributeSets] => SimpleXMLElement Object ( ) [Relationships] => SimpleXMLElement Object ( ) [SalesRankings] => SimpleXMLElement Object ( [SalesRank] => Array ( [0] => SimpleXMLElement Object ( [ProductCategoryId] => watch_display_on_website [Rank] => 2370 ) [1] => SimpleXMLElement Object ( [ProductCategoryId] => 7012519011 [Rank] => 971 ) ) ) ) [1] => SimpleXMLElement Object ( [Identifiers] => SimpleXMLElement Object ( [MarketplaceASIN] => SimpleXMLElement Object ( [MarketplaceId] => XXXXXXXXX [ASIN] => B00ZUEMJLY ) ) [AttributeSets] => SimpleXMLElement Object ( ) [Relationships] => SimpleXMLElement Object ( ) [SalesRankings] => SimpleXMLElement Object ( [SalesRank] => Array ( [0] => SimpleXMLElement Object ( [ProductCategoryId] => watch_display_on_website [Rank] => 3392 ) [1] => SimpleXMLElement Object ( [ProductCategoryId] => 7012519011 [Rank] => 1396 ) ) ) ) [2] => SimpleXMLElement Object ( [Identifiers] => SimpleXMLElement Object ( [MarketplaceASIN] => SimpleXMLElement Object ( [MarketplaceId] => XXXXXXXXX [ASIN] => B01N7WMCMJ ) ) [AttributeSets] => SimpleXMLElement Object ( ) [Relationships] => SimpleXMLElement Object ( ) [SalesRankings] => SimpleXMLElement Object ( [SalesRank] => Array ( [0] => SimpleXMLElement Object ( [ProductCategoryId] => watch_display_on_website [Rank] => 5262 ) [1] => SimpleXMLElement Object ( [ProductCategoryId] => 7012519011 [Rank] => 2189 ) ) ) ) [3] => SimpleXMLElement Object ( [Identifiers] => SimpleXMLElement Object ( [MarketplaceASIN] => SimpleXMLElement Object ( [MarketplaceId] => XXXXXXXXX [ASIN] => B00854DIXE ) ) [AttributeSets] => SimpleXMLElement Object ( ) [Relationships] => SimpleXMLElement Object ( ) [SalesRankings] => SimpleXMLElement Object ( [SalesRank] => Array ( [0] => SimpleXMLElement Object ( [ProductCategoryId] => watch_display_on_website [Rank] => 5324 ) [1] => SimpleXMLElement Object ( [ProductCategoryId] => 7012519011 [Rank] => 2217 ) ) ) ) [4] => SimpleXMLElement Object ( [Identifiers] => SimpleXMLElement Object ( [MarketplaceASIN] => SimpleXMLElement Object ( [MarketplaceId] => XXXXXXXXX [ASIN] => B01NBE9WTU ) ) [AttributeSets] => SimpleXMLElement Object ( ) [Relationships] => SimpleXMLElement Object ( ) [SalesRankings] => SimpleXMLElement Object ( [SalesRank] => Array ( [0] => SimpleXMLElement Object ( [ProductCategoryId] => watch_display_on_website [Rank] => 2703 ) [1] => SimpleXMLElement Object ( [ProductCategoryId] => 7012519011 [Rank] => 1103 ) ) ) ) [5] => SimpleXMLElement Object ( [Identifiers] => SimpleXMLElement Object ( [MarketplaceASIN] => SimpleXMLElement Object ( [MarketplaceId] => XXXXXXXXX [ASIN] => B01H59KVCK ) ) [AttributeSets] => SimpleXMLElement Object ( ) [Relationships] => SimpleXMLElement Object ( ) [SalesRankings] => SimpleXMLElement Object ( [SalesRank] => Array ( [0] => SimpleXMLElement Object ( [ProductCategoryId] => watch_display_on_website [Rank] => 7933 ) [1] => SimpleXMLElement Object ( [ProductCategoryId] => 7012519011 [Rank] => 3399 ) ) ) ) [6] => SimpleXMLElement Object ( [Identifiers] => SimpleXMLElement Object ( [MarketplaceASIN] => SimpleXMLElement Object ( [MarketplaceId] => XXXXXXXXX [ASIN] => B01I956QMI ) ) [AttributeSets] => SimpleXMLElement Object ( ) [Relationships] => SimpleXMLElement Object ( ) [SalesRankings] => SimpleXMLElement Object ( [SalesRank] => Array ( [0] => SimpleXMLElement Object ( [ProductCategoryId] => watch_display_on_website [Rank] => 3195 ) [1] => SimpleXMLElement Object ( [ProductCategoryId] => 7012519011 [Rank] => 1311 ) ) ) ) [7] => SimpleXMLElement Object ( [Identifiers] => SimpleXMLElement Object ( [MarketplaceASIN] => SimpleXMLElement Object ( [MarketplaceId] => XXXXXXXXX [ASIN] => B005OT7MBM ) ) [AttributeSets] => SimpleXMLElement Object ( ) [Relationships] => SimpleXMLElement Object ( ) [SalesRankings] => SimpleXMLElement Object ( [SalesRank] => Array ( [0] => SimpleXMLElement Object ( [ProductCategoryId] => watch_display_on_website [Rank] => 2366 ) [1] => SimpleXMLElement Object ( [ProductCategoryId] => 7012519011 [Rank] => 967 ) ) ) ) [8] => SimpleXMLElement Object ( [Identifiers] => SimpleXMLElement Object ( [MarketplaceASIN] => SimpleXMLElement Object ( [MarketplaceId] => XXXXXXXXX [ASIN] => B00596EO4S ) ) [AttributeSets] => SimpleXMLElement Object ( ) [Relationships] => SimpleXMLElement Object ( ) [SalesRankings] => SimpleXMLElement Object ( [SalesRank] => Array ( [0] => SimpleXMLElement Object ( [ProductCategoryId] => watch_display_on_website [Rank] => 2929 ) [1] => SimpleXMLElement Object ( [ProductCategoryId] => 7012523011 [Rank] => 724 ) ) ) ) [9] => SimpleXMLElement Object ( [Identifiers] => SimpleXMLElement Object ( [MarketplaceASIN] => SimpleXMLElement Object ( [MarketplaceId] => XXXXXXXXX [ASIN] => B01N7BWYJF ) ) [AttributeSets] => SimpleXMLElement Object ( ) [Relationships] => SimpleXMLElement Object ( ) [SalesRankings] => SimpleXMLElement Object ( [SalesRank] => Array ( [0] => SimpleXMLElement Object ( [ProductCategoryId] => watch_display_on_website [Rank] => 3843 ) [1] => SimpleXMLElement Object ( [ProductCategoryId] => 7012519011 [Rank] => 1575 ) ) ) ) ) ) ) [ResponseMetadata] => SimpleXMLElement Object ( [RequestId] => 765deb6b-7ae6-4aaf-bfea-4f489a855cf0 ) )

On the internet I have seen ways to access, such as $ Object-> attribute, but the response I get does not look like those XML, not even the attributes are described in that answer.

When I print in xml using (( htmlentities )) it's like this:

<?xml version="1.0"?> <ListMatchingProductsResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01"><ListMatchingProductsResult><Products xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd"><Product><Identifiers><MarketplaceASIN><MarketplaceId>XXXXXXXXXXXX</MarketplaceId><ASIN>B01N9BACDM</ASIN></MarketplaceASIN></Identifiers><AttributeSets><ns2:ItemAttributes xml:lang="en-CA"><ns2:BandMaterialType>Silicone</ns2:BandMaterialType><ns2:Binding>Watch</ns2:Binding><ns2:Brand>Tommy Hilfiger</ns2:Brand><ns2:ClaspType>Buckle</ns2:ClaspType><ns2:Department>mens</ns2:Department><ns2:ItemDimensions><ns2:Height Units="inches">5.91</ns2:Height><ns2:Length Units="inches">5.91</ns2:Length><ns2:Width Units="inches">5.91</ns2:Width><ns2:Weight Units="pounds">0.18</ns2:Weight></ns2:ItemDimensions><ns2:Label>Tommy Hilfiger</ns2:Label><ns2:ListPrice><ns2:Amount>235.20</ns2:Amount><ns2:CurrencyCode>CAD</ns2:CurrencyCode></ns2:ListPrice><ns2:Manufacturer>Tommy Hilfiger</ns2:Manufacturer><ns2:MaterialType>stainless-steel</ns2:MaterialType><ns2:Model>1791350</ns2:Model><ns2:PackageDimensions><ns2:Height Units="inches">3.20</ns2:Height><ns2:Length Units="inches">3.50</ns2:Length><ns2:Width Units="inches">3.30</ns2:Width><ns2:Weight Units="pounds">0.50</ns2:Weight></ns2:PackageDimensions><ns2:PackageQuantity>1</ns2:PackageQuantity><ns2:PartNumber>1791350</ns2:PartNumber><ns2:ProductGroup>Watch</ns2:ProductGroup><ns2:ProductTypeName>WATCH</ns2:ProductTypeName><ns2:Publisher>Tommy Hilfiger</ns2:Publisher><ns2:SmallImage><ns2:URL>http://ecx.images-amazon.com/images/I/51uLGBQ9ZmL._SL75_.jpg</ns2:URL><ns2:Height Units="pixels">75</ns2:Height><ns2:Width Units="pixels">53</ns2:Width></ns2:SmallImage><ns2:Studio>Tommy Hilfiger</ns2:Studio><ns2:Title>Tommy Hilfiger Men's 'COOL SPORT' Quartz Stainless Steel and Silicone Casual Watch, Color:Blue (Model: 1791350)</ns2:Title></ns2:ItemAttributes></AttributeSets><Relationships/><SalesRankings><SalesRank><ProductCategoryId>watch_display_on_website</ProductCategoryId><Rank>2370</Rank></SalesRank><SalesRank><ProductCategoryId>7012519011</ProductCategoryId><Rank>971</Rank></SalesRank></SalesRankings></Product>

The code:

$ch = curl_init($link);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch , CURLOPT_RETURNTRANSFER , true);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

var_dump($response);
$xmlObject =new SimpleXMLElement($response);
print_r($xmlObject);

I need to access the attributes of the product

for example if I try to print

var_dump($xmlObject->Product);
---->
object(SimpleXMLElement)[2]

and I do not understand, because in theory there are 10 products

    
asked by Andress Blend 30.10.2017 в 17:12
source

1 answer

1

You could try FriendsOfPHP / Goutte , I just did a test and it works correctly to extract the data (Products) from the XML that have shared, only that I had to close some XML tags at the end.

file.xml

<?xml version="1.0"?>
<ListMatchingProductsResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
    <ListMatchingProductsResult>
        <Products
                xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01"
                xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
            <Product>
                <Identifiers>
                    <MarketplaceASIN>
                        <MarketplaceId>XXXXXXXXXXXX</MarketplaceId>
                        <ASIN>B01N9BACDM</ASIN>
                    </MarketplaceASIN>
                </Identifiers>
                <AttributeSets>
                    <ns2:ItemAttributes xml:lang="en-CA">
                        <ns2:BandMaterialType>Silicone</ns2:BandMaterialType>
                        <ns2:Binding>Watch</ns2:Binding>
                        <ns2:Brand>Tommy Hilfiger</ns2:Brand>
                        <ns2:ClaspType>Buckle</ns2:ClaspType>
                        <ns2:Department>mens</ns2:Department>
                        <ns2:ItemDimensions>
                            <ns2:Height Units="inches">5.91</ns2:Height>
                            <ns2:Length Units="inches">5.91</ns2:Length>
                            <ns2:Width Units="inches">5.91</ns2:Width>
                            <ns2:Weight Units="pounds">0.18</ns2:Weight>
                        </ns2:ItemDimensions>
                        <ns2:Label>Tommy Hilfiger</ns2:Label>
                        <ns2:ListPrice>
                            <ns2:Amount>235.20</ns2:Amount>
                            <ns2:CurrencyCode>CAD</ns2:CurrencyCode>
                        </ns2:ListPrice>
                        <ns2:Manufacturer>Tommy Hilfiger</ns2:Manufacturer>
                        <ns2:MaterialType>stainless-steel</ns2:MaterialType>
                        <ns2:Model>1791350</ns2:Model>
                        <ns2:PackageDimensions>
                            <ns2:Height Units="inches">3.20</ns2:Height>
                            <ns2:Length Units="inches">3.50</ns2:Length>
                            <ns2:Width Units="inches">3.30</ns2:Width>
                            <ns2:Weight Units="pounds">0.50</ns2:Weight>
                        </ns2:PackageDimensions>
                        <ns2:PackageQuantity>1</ns2:PackageQuantity>
                        <ns2:PartNumber>1791350</ns2:PartNumber>
                        <ns2:ProductGroup>Watch</ns2:ProductGroup>
                        <ns2:ProductTypeName>WATCH</ns2:ProductTypeName>
                        <ns2:Publisher>Tommy Hilfiger</ns2:Publisher>
                        <ns2:SmallImage>
                            <ns2:URL>http://ecx.images-amazon.com/images/I/51uLGBQ9ZmL._SL75_.jpg</ns2:URL>
                            <ns2:Height Units="pixels">75</ns2:Height>
                            <ns2:Width Units="pixels">53</ns2:Width>
                        </ns2:SmallImage>
                        <ns2:Studio>Tommy Hilfiger</ns2:Studio>
                        <ns2:Title>Tommy Hilfiger Men's 'COOL SPORT' Quartz Stainless Steel and Silicone Casual Watch, Color:Blue (Model: 1791350)</ns2:Title>
                    </ns2:ItemAttributes>
                </AttributeSets>
                <Relationships/>
                <SalesRankings>
                    <SalesRank>
                        <ProductCategoryId>watch_display_on_website</ProductCategoryId>
                        <Rank>2370</Rank>
                    </SalesRank>
                    <SalesRank>
                        <ProductCategoryId>7012519011</ProductCategoryId>
                        <Rank>971</Rank>
                    </SalesRank>
                </SalesRankings>
            </Product>
        </Products>
    </ListMatchingProductsResult>
</ListMatchingProductsResponse>

index.php

<?php

include('vendor/autoload.php');

use Goutte\Client;

$client = new Client();
$crawler = $client->request('GET', 'http://localhost:8000/file.xml');

$products = $crawler->filter('default|Products default|Product')->each(function ($node) {

    /** @var $node \Symfony\Component\DomCrawler\Crawler */
    $marketplaceId = $node->filter('default|Identifiers default|MarketplaceASIN default|MarketplaceId')->text();
    $attributeBrand = $node->filter('default|AttributeSets ns2|ItemAttributes ns2|Brand')->text();

    return [
        'MarketplaceId' => $marketplaceId,
        'attributeBrand' => $attributeBrand
    ];

});

echo '<pre>';
print_r($products);
echo '</pre>';
    
answered by 30.10.2017 в 19:14