1

I am working on the Amazon API, and when I get a responce it looks like this.

<GetMatchingProductForIdResult Id="082686322638" IdType="UPC" status="Success"> <Products> <Product> <Identifiers> <MarketplaceASIN> <MarketplaceId>ATVPDKIKX0DER</MarketplaceId> <ASIN>B00TU53O8Q</ASIN> </MarketplaceASIN> </Identifiers> <AttributeSets> <ns2:ItemAttributes xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd" xml:lang="en-US"> <ns2:Binding>Toy</ns2:Binding> <ns2:Brand>Rubie's</ns2:Brand> <ns2:Color>BLACK</ns2:Color> <ns2:Department>unisex-child</ns2:Department> <ns2:Feature>Half Helmet mask accessory covers face; one size fits most</ns2:Feature> <ns2:Feature>Officially licensed Star Wars Episode VII costume accessory; as worn by Kylo Ren in Star Wars Episode VII: The Force Awakens</ns2:Feature> <ns2:Feature>Helmet has faux battle damage representing a pivotal moment in the movie</ns2:Feature> <ns2:Feature>Kylo Ren costumes and additional accessories available separately</ns2:Feature> <ns2:Feature>Rubie's offers costumes and accessories from Star Wars original and prequel trilogies as well as the Clone Wars series; great group and family costume ideas</ns2:Feature> <ns2:ItemDimensions> <ns2:Height Units="inches">10.00</ns2:Height> <ns2:Length Units="inches">10.00</ns2:Length> <ns2:Width Units="inches">10.00</ns2:Width> <ns2:Weight Units="pounds">0.13</ns2:Weight> </ns2:ItemDimensions> <ns2:IsAdultProduct>false</ns2:IsAdultProduct> <ns2:Label>Rubies - Domestic</ns2:Label> <ns2:ListPrice> <ns2:Amount>9.99</ns2:Amount> <ns2:CurrencyCode>USD</ns2:CurrencyCode> </ns2:ListPrice> <ns2:Manufacturer>Rubies - Domestic</ns2:Manufacturer> <ns2:ManufacturerMaximumAge Units="months">120.0</ns2:ManufacturerMaximumAge> <ns2:ManufacturerMinimumAge Units="months">60.0</ns2:ManufacturerMinimumAge> <ns2:Model>32263</ns2:Model> <ns2:NumberOfItems>1</ns2:NumberOfItems> <ns2:PackageDimensions> <ns2:Height Units="inches">4.00</ns2:Height> <ns2:Length Units="inches">9.20</ns2:Length> <ns2:Width Units="inches">8.20</ns2:Width> <ns2:Weight Units="pounds">0.15</ns2:Weight> </ns2:PackageDimensions> <ns2:PackageQuantity>1</ns2:PackageQuantity> <ns2:PartNumber>32263</ns2:PartNumber> <ns2:ProductGroup>Toy</ns2:ProductGroup> <ns2:ProductTypeName>TOYS_AND_GAMES</ns2:ProductTypeName> <ns2:Publisher>Rubies - Domestic</ns2:Publisher> <ns2:Size>One Size</ns2:Size> <ns2:SmallImage> <ns2:URL>http://ecx.images-amazon.com/images/I/41XuPVJMMkL._SL75_.jpg</ns2:URL> <ns2:Height Units="pixels">75</ns2:Height> <ns2:Width Units="pixels">66</ns2:Width> </ns2:SmallImage> <ns2:Studio>Rubies - Domestic</ns2:Studio> <ns2:Title>Star Wars: The Force Awakens Child's Kylo Ren Half Helmet</ns2:Title> <ns2:Warranty>No Warranty</ns2:Warranty> </ns2:ItemAttributes> </AttributeSets> <Relationships/> <SalesRankings> <SalesRank> <ProductCategoryId>toy_display_on_website</ProductCategoryId> <Rank>5183</Rank> </SalesRank> <SalesRank> <ProductCategoryId>2229575011</ProductCategoryId> <Rank>11</Rank> </SalesRank> </SalesRankings> </Product> </Products> </GetMatchingProductForIdResult> 

How do i go about getting the Sales Rank, Amount, Product Dims, Id, and ASIN? I have been looking around for some time now, and haven't found anything about the colon.

My code that i was trying to use is:

for($i = 0; $i < (count($groupedArrays) - 0); $i++){ $xmlFilePath = 'xml/' . $file_name . $i . '.xml'; $xml = simplexml_load_file($xmlFilePath); foreach($xml->children() as $items) { if ($items->GetMatchingProductForIdResult->Error) { continue; } $upcTag = $items['Id'] . ','; if($items->GetMatchingProductForIdResult->Products->Product->AttributeSets->Relationship == FALSE){ echo $items->GetMatchingProductForIdResult->Products->Product->AttributeSets->SalesRankings->SalesRank[0] . ','; } else { echo $items->GetMatchingProductForIdResult->Products->Product->AttributeSets->Relationship->VariationParent->Identifiers->MarketplaceASIN->ASIN . ','; } $subject = "<ns2:Amount></ns2:Amount>"; preg_match_all('/<[^:]*:([^>]*)>(.*)<\/[^>]*>/', $subject, $matches); $i = 1; $ii = 0; foreach ($matches as $key => $value) { $newArr[$matches[$i][$ii]] = $matches[$ii][$ii]; $ii++; $i++; } $result = array_filter($newArr); foreach($result as $key => $value){ echo $key." = ".$value. "<br/>"; }; } 

There is a for loop before this one that gets the response and saves it as a .xml in my xml folder. i cannot figure out what i am doing wrong and help would be greatly appreciated! I have been working on this part for a little more then 8-9 hours. Thanks in advance!

1 Answer 1

1

If you want to get the properties from your xml, you can use simplexml_load_file.

To loop the <ns2:ItemAttributes.. you can use the SimpleXMLElement children method with the corresponding namespace 'ns2'

Maybe this example which gets a few of the properties can help you:

$xml = simplexml_load_file($xmlFilePath); $ns = $xml->getNamespaces(true); $asin = ''; $salesRankings = array(); $amount = ''; foreach ($xml->Products->Product as $product) { $asin = (string)$product->Identifiers->MarketplaceASIN->ASIN; foreach ($product->SalesRankings->SalesRank as $salesRank) { $salesRankings[] = array( "ProductCategoryId" => (string)$salesRank->ProductCategoryId, "Rank" => (string)$salesRank->Rank ); } foreach ($product->AttributeSets->children($ns['ns2']) as $itemAttributes) { $amount = (string)$itemAttributes->ListPrice->Amount; } } 
Sign up to request clarification or add additional context in comments.

5 Comments

The code did not work. I am not sure what i am doing wrong. When i run this code it gives me a bunch of warnings. I am not sure what is going on. Could you possibly just write it off of my current code?
@Robert The code is based on the provided xml in the question. You can see the result in this demo (note that in the demo I've used simplexml_load_string instead of simplexml_load_file. What are the warnings that you get? Is the structure of the xml the same when you get the warnings?)
it was multiple warnings, but only one error, it said "fatal error call to undefined function getNamespace() on line 121". Line 121 is the line that $ns is being defined. When i print something, it doesn't print out anything. I was putting the print inside of the for loop. Since this code overwrites, how would i be able to save everything down to a csv file? I also just realized that you did use simplexm_load_string. So all i would have to do is change that to simplexml_load_file?
I used simplexml_load_string for the demo to load the string in $source. If I look at your code, you want to load xml file(s). Maybe simplexml_load_file returns false when loading the xml. What you could do is verify if $xmlFilePath contains the expected value when the error occurs. When you run the code, is the xml structure in the xml file(s) the same as the xml from your example?
it returns the everything as expected.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.