1

I've got xml that looks like this:

<?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>

When I decode this like this:

$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);

I receive everything within an array except the <media> things. Obviously this is because media has a namespace:

<ns2:mediaType>

How do I make sure I get the ns2:mediaType data aswell? Because right now I get an empty array?

-- EDIT --

When I add registerXPathNamespace it's not working. Please see above the edit.

Jenssen
  • 1,801
  • 4
  • 38
  • 72
  • Possible duplicate of [Parse XML with Namespace using SimpleXML](https://stackoverflow.com/questions/595946/parse-xml-with-namespace-using-simplexml) – CBroe Jan 08 '18 at 08:38
  • Please see my edit! – Jenssen Jan 08 '18 at 08:53
  • Possible duplicate of [Resolve namespaces with SimpleXML regardless of structure or namespace](https://stackoverflow.com/questions/26400993/resolve-namespaces-with-simplexml-regardless-of-structure-or-namespace) – iainn Jan 08 '18 at 11:20
  • 2
    As a very dirty - but quick method, before you load the XML (the call to simplexml_load_string ), replace ` – Nigel Ren Jan 08 '18 at 15:34
  • @NigelRen Are there any downsides to doing what you suggested? That is to say, are there any real possibilities that removing the namespaces would make the resulting array unreadable / broken? – FiddlingAway Dec 14 '22 at 13:05

0 Answers0