dougudu3564 2016-08-07 03:23
浏览 48
已采纳

如何使用simplexml修复数据

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!

  • 写回答

1条回答 默认 最新

  • duanfaxin7014 2016-08-07 06:56
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?