Php xml到具有命名空间的数组

时间:2018-01-08 08:35:40

标签: php arrays xml

我的xml看起来像这样:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<products xmlns="http://domain.dev/schema" xmlns:ns2="http://api.domain.dev/schemas">
    <header>
        <owner>TEST</owner>
    </header>
    <brand>
        <product brandCode="code">
            <sizeGridCode>M</sizeGridCode>
            <productCategoryAttributes>
                <productCategoryAttribute key="longDescription">TEST</productCategoryAttribute>
            </productCategoryAttributes>
            <LINES>
                <productLine>
                    <productLineId>ASDF</productLineId>
                    <brandColourName>GreASDFy</brandColourName>
                    <colourCodes>
                        <colourCode>ASDF</colourCode>
                    </colourCodes>
                    <composition>
                        <compositionComponent>
                            <material/>
                        </compositionComponent>
                    </composition>
                    <articles>
                        <article>
                            <articleActivationDate>2017-11-20T16:14:15.159Z</articleActivationDate>
                            <active>true</active>
                            <brandArticleId>asdfasdf</brandArticleId>
                            <ean>asdfasdf</ean>
                            <id>asdfasdf</id>
                            <articleMedia>
                                <media>
                                    <ns2:mediaType>image</ns2:mediaType>
                                    <ns2:mediaURL>default_image</ns2:mediaURL>
                                    <ns2:sortOrder>1</ns2:sortOrder>
                                </media>
                            </articleMedia>
                            <categoryAttributes>
                                <categoryAttribute key="size">XS</categoryAttribute>
                            </categoryAttributes>
                        </article>
                    </articles>
                </productLine>
            </LINES>
        </product>
    </brand>
</products>

当我像这样解码时:

$xml = simplexml_load_string(
    Storage::get($path),
    "SimpleXMLElement", LIBXML_NOCDATA
)->registerXPathNamespace("ns2", "http://api.domain.dev/schemas");

$json = json_encode($xml);
return json_decode($json,TRUE);

除了<media>之外,我收到数组中的所有内容。显然这个 是因为媒体有一个命名空间:

<ns2:mediaType>

如何确保获取ns2:mediaType数据?因为现在我得到一个空数组?

- 编辑 -

当我添加registerXPathNamespace时,它无效。请参见编辑上方。

0 个答案:

没有答案