0

I have a really complicated XML file(in my opinion). It contains multiple namespaces(prefixes) and I am trying to convert it to an object.

So at first we were using this function

simplexml_load_file("file.xml");

file.xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<animal:race>
    <animal:dog>
        <specification:color>Black</specification>
    </animal:dog>
    <animal:cat>
        <animal:name>Flappie</animal:name>
    </animal:cat>
</animal:race>

At first the function returned an empty object. Then we found out that you have to specify prefixes like this:

   simplexml_load_file("file.xml",null,null,"animal");

This returned an object but without all the other prefixes like: specification. So the object became:(outputted as an array

object(animal:race)#1 (2) { 
    ["dog"]=> object(SimpleXMLElement)#2 (0) { 
    }
    ["cat"]=> object(SimpleXMLElement)#2 (1) { 
        [animal:name] => string "flappie"
    }
}

So my conclusion is that we need our other namespaces to be "included" to.. cause specifications arent shown.

Roy Stijsiger
  • 199
  • 2
  • 12
  • Have you tried registering the different namespaces? See http://stackoverflow.com/questions/740730/parse-an-xml-with-simplexml-which-has-multiple-namespaces and using XPath to iterate your object. – R. Chappell Oct 05 '16 at 14:09
  • @R.Chappell We've seen that but we cant figure out how to make it work. – Roy Stijsiger Oct 05 '16 at 14:11
  • @R.Chappell Do we just need registerxnamespace rules? cause after we've done that it still didnt work. and how does that foreach loop work.. – Roy Stijsiger Oct 05 '16 at 14:11
  • The registerXPathNamespace allows you to find nodes using XPath, which is like jQuery selectors for XML but a bit more complicated. What are you trying to do with the file, do you just want to build up an array using the XML described? – R. Chappell Oct 05 '16 at 14:13
  • @R.Chappell So I am using soapclient in php and the method needs an object which is created from the xml message.. but the xml has prefixes/namespaces which complicates using the simplexml_load_file function cuase ti doesnt include those with prefixes. – Roy Stijsiger Oct 05 '16 at 14:21
  • Perhaps you should consider using PHP Dom instead as it has far better support for documents with multiple namespaces. http://php.net/manual/en/domdocument.load.php would be a good start – R. Chappell Oct 05 '16 at 14:31
  • @R.Chappell Yeah but then the content is not an object.. right? – Roy Stijsiger Oct 05 '16 at 14:33
  • No, it is an object. It's a DomDocument which should be supported by SoapClient. – R. Chappell Oct 05 '16 at 14:35
  • @R.Chappell $xml = new DOMDocument(1.0, 'UTF-8'); $xml->load('filename.xml'); dump($xml); die; the values are here: ["textContent"]=> string(203)"Lv01 C3 king StUF Yelp King Romain 20161003142300 ZAK 1 false 570000001 570000001" – Roy Stijsiger Oct 05 '16 at 14:38

0 Answers0