0

I have been working on a project and have run into a brick wall. I researched around the web and it seems that no one is having the same problem as me and it is upsetting. I am unable to get the full xml in return from simplexml_load_string(). When I use simplexml at all it gives me a problem. The : is just where most think the problem is. If you look at the sales rankings tag in the bottom example and then look at the hierarchy you will see that there is not one namespace. Here is the original xml:

<GetMatchingProductForIdResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
<GetMatchingProductForIdResult Id="883028968343" IdType="UPC" status="Success">
    <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>ATVPDKIKX0DER</MarketplaceId>
                    <ASIN>B007C435CW</ASIN></MarketplaceASIN>
            </Identifiers>
            <AttributeSets>
                <ns2:ItemAttributes xml:lang="en-US">
                <ns2:Binding>Apparel</ns2:Binding>
                <ns2:Brand>Secret Wishes</ns2:Brand>
                <ns2:Color>Black</ns2:Color>
                <ns2:Department>womens</ns2:Department>
                <ns2:Feature>Black and gold musketeer or pirate costume</ns2:Feature>
                <ns2:Feature>Features strapless corset with lace up back and matching mini skirt</ns2:Feature>
                <ns2:Feature>Hat and choker included</ns2:Feature>
                <ns2:Feature>Boots and sword available separately</ns2:Feature>
                <ns2:Feature>Sexy wishes for playful adults is a division of rubies costume company</ns2:Feature>
                <ns2:Genre>Historical</ns2:Genre>
                <ns2:IsAdultProduct>false</ns2:IsAdultProduct> 
                <ns2:Label>Secret Wishes</ns2:Label>
                <ns2:Manufacturer>Secret Wishes</ns2:Manufacturer><ns2:Model>889683XS</ns2:Model>
                <ns2:PackageDimensions>
                    <ns2:Height Units="inches">1.10</ns2:Height>
                    <ns2:Length Units="inches">14.50</ns2:Length>
                    <ns2:Width Units="inches">11.40</ns2:Width>
                    <ns2:Weight Units="pounds">0.90</ns2:Weight>
                </ns2:PackageDimensions>
                <ns2:ProductGroup>Apparel</ns2:ProductGroup> 
                <ns2:ProductTypeName>ADULT_COSTUME</ns2:ProductTypeName>       
                <ns2:Publisher>Secret Wishes</ns2:Publisher>
                <ns2:Size>X-Small</ns2:Size>
                <ns2:SmallImage>
                    <ns2:URL>http://ecx.images-amazon.com/images/I/41YPo629q4L._SL75_.jpg</ns2:URL>
                    <ns2:Height Units="pixels">75</ns2:Height>
                    <ns2:Width Units="pixels">37</ns2:Width>
                </ns2:SmallImage>
            </AttributeSets>
            <SalesRankings>
                <SalesRank>
                    <ProductCategoryId>office_product_display_on_website</ProductCategoryId>
                    <Rank>259</Rank>
                </SalesRank>
            </SalesRankings>

             ECT...

The response I get when I do print_r(simplexml_load_string(curl_exec($ch); is this:

SimpleXMLElement Object
(
[GetMatchingProductForIdResult] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [Id] => 883028968343
                [IdType] => UPC
                [status] => Success
            )

        [Products] => SimpleXMLElement Object
            (
                [Product] => SimpleXMLElement Object
                    (
                        [Identifiers] => SimpleXMLElement Object
                            (
                                [MarketplaceASIN] => SimpleXMLElement Object
                                    (
                                        [MarketplaceId] => ATVPDKIKX0DER
                                        [ASIN] => B007C435CW
                                    )

                            )

                        [AttributeSets] => SimpleXMLElement Object
                            (
                            )

                        [Relationships] => SimpleXMLElement Object
                            (
                                [VariationParent] => SimpleXMLElement Object
                                    (
                                        [Identifiers] => SimpleXMLElement Object
                                            (
                                                [MarketplaceASIN] => SimpleXMLElement Object
                                                    (
                                                        [MarketplaceId] => ATVPDKIKX0DER
                                                        [ASIN] => B00B7DAJAA
                                                    )

                                            )

                                    )

                            )

                        [SalesRankings] => SimpleXMLElement Object
                            (
                            )

                    )

            )

    )

I do not understand what the possible problem is. My main goal for this data is to push it to JSON. If someone could help me out, I would greatly appreciate it. Thanks!

Teddy Codes
  • 489
  • 4
  • 14
  • Possible duplicate of [Simple XML - Dealing With Colons In Nodes](http://stackoverflow.com/questions/1186107/simple-xml-dealing-with-colons-in-nodes) – ThW Aug 07 '16 at 07:05
  • I am not looking for you to give me how to deal with colon nodes. Not Even Sales rankings Is Showing up. – Teddy Codes Aug 07 '16 at 11:29
  • 1
    The colons separate the namespace prefix from the local node name. Your node have namespaces. You need to learn how to read XML with namespaces. Just replacing the character is broken in several ways. First a namespace prefix is not the actual namespace. It can change and is optional. Second the character can occur regularly in other parts of the file. – ThW Aug 08 '16 at 07:30
  • I just wanted it to be json instead of xml. – Teddy Codes Aug 08 '16 at 10:33
  • Converting XML directly to JSON is a broken idea in the first place - they have different basic assumptions about the shape of data they can represent. It's *definitely* not what SimpleXML was designed for. You're much better off thinking about *what data you want to get out*, and then learning how to get it with SimpleXML. – IMSoP Aug 08 '16 at 11:33
  • I've already done it. The data that I'm getting is fine. I don't understand the problem. If the code isn't broken why fix it? – Teddy Codes Aug 08 '16 at 11:34
  • Because it IS broken. You did not solve the problem, you created a fragile workaround. – ThW Aug 08 '16 at 11:41
  • I have no clue how to use simplexml. – Teddy Codes Aug 08 '16 at 11:41
  • 1
    @Robert Luckily, [the manual has some simple examples](http://php.net/manual/en/simplexml.examples-basic.php) and for anything with a namespace prefix, look at [the `children()` method](http://php.net/manual/en/simplexmlelement.children.php). Or, use an XML parser/API that you do understand. Converting to JSON just gives you more things to go wrong, and less chance of understanding them. – IMSoP Aug 08 '16 at 12:15
  • Thanks! I will look into it! – Teddy Codes Aug 08 '16 at 12:20

1 Answers1

-1

Its looks like simplexml is not working correctly because of ':' symbol in tag names. You can replace all ':' by:

$xml = str_replace ( ':', '_', $xml );

Or look for another solutions described here.

Community
  • 1
  • 1
Ivnhal
  • 1,099
  • 1
  • 12
  • 20