如何使用simplexml修复数据

时间:2016-08-07 03:23:01

标签: php xml simplexml

我一直致力于一个项目,并遇到了一堵砖墙。我在网上进行了研究,似乎没有人遇到和我一样的问题,这让人心烦意乱。我无法从simplexml_load_string()获得完整的xml。当我使用simplexml时它会给我一个问题。 :正是大多数人认为问题所在的地方。如果查看底部示例中的销售排名标记,然后查看层次结构,您将看到没有一个名称空间。这是原始的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...

print_r(simplexml_load_string(curl_exec($ch);时得到的回答是:

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
                            (
                            )

                    )

            )

    )

我不明白可能的问题是什么。我对这些数据的主要目标是将其推向JSON。如果有人可以帮助我,我会非常感激。谢谢!

1 个答案:

答案 0 :(得分:-1)

由于标记名称中的“:”符号,它看起来像simplexml无法正常工作。您可以将所有':'替换为:

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

或寻找另一种描述here的解决方案。