0

When parsing an XML array like:

<?xml version="1.0" encoding="utf-8"?>
<Products>
  <Product>
    <Code>ABC-1001</Code>
    <Brand>ZCOM</Brand>
  </Product>
</Products>

I get an output of:

Array
(
  [0] => Array
  (
    [Code] => AP1024-DDRII640
    [Brand] => ZCOM
  )
}

But when the XML is like:

<?xml version="1.0" encoding="utf-8"?>
<Products Code="ABC-1001">
  <Product>
    <Code><![CDATA[ABC-1001]]></Code>
    <Brand><![CDATA[ZCOM]]></Brand>
  </Product>
</Products>

It returns:

array
  0 => 
    array (size=12)
      '@attributes' => 
        array (size=1)
          'Code' => string 'ABC-1001' (length=8)
      'Code' => 
        array (size=0)
          empty
      'Brand' => 
        array (size=0)
          empty

This is how the XML is parsed from a URL:

$updateUrl = file_get_contents('http://www.someplace/xmlfeed/xml.cfm?asd=12345&uhg=9999');
$updateXml=<<<XML
$updateUrl
XML;
$updateXmlObject=json_decode(json_encode((array) simplexml_load_string($updateXml)), 1);
$updatePHPArray=$updateXmlObject['Product'];

And:

var_dump($updatePHPArray);exit;

Gives the output as above.

Now, why am I getting empty values in the second instance and how could I remedy this without access to the XML source?

Craig van Tonder
  • 7,497
  • 18
  • 64
  • 109
  • what exactly is the point of getting a string, stuffing that string into ANOTHER string using a heredoc, then stuffing that second string into xml, forcing it to an array. json_encoding, json_decoding? That is just total cargo-cult programming. – Marc B Dec 19 '14 at 18:37
  • @MarcB I am a noob... This was the best that I could come up with. Open to any better solutions hey :) – Craig van Tonder Dec 19 '14 at 18:38
  • @MarcB I think that i encoded and decoded it to normalize the data if that makes sense? I recall having some characters or something that were giving me a hard time. – Craig van Tonder Dec 19 '14 at 18:41
  • 2
    SimpleXML with cdata -> http://stackoverflow.com/questions/2970602/php-how-to-handle-cdata-with-simplexmlelement – Brain Foo Long Dec 19 '14 at 18:45
  • @BrainFooLong Thanks, did not spot this one. – Craig van Tonder Dec 19 '14 at 18:47
  • @MarcB I'd still love to see how you would tackle this task, just saying :) – Craig van Tonder Dec 19 '14 at 19:33
  • 2
    well, since there's some normalizing going on, then that's ok. but just for raw conversion: `$xml = simplexml_load_file('http:/...');` would do. – Marc B Dec 19 '14 at 20:59
  • 1
    By using that `json_decode(json_encode())` hack, you've simultaneously thrown away *all the features of SimpleXML*, and introduced a whole load of unnecessary problems for yourself. The result of `simplexml_load_string`/`simplexml_load_file` is an object with lots of useful magic powers, don't just throw it away at your first opportunity. – IMSoP Dec 20 '14 at 21:39
  • @IndigoIdentity: Why do you convert the product to an array in the first place? – hakre Dec 21 '14 at 19:16
  • @hakre To process the data in various ways. – Craig van Tonder Dec 21 '14 at 23:04
  • @IndigoIdentity: I have good news for you then: You don't neeed to convert to array. So this solves your problem already. Just access the data from the **SimpleXMLelement** object and you're already done. The problem you describe you have also immediately disappears. – hakre Dec 22 '14 at 09:56

1 Answers1

1

The problem seems to be that the cast you're doing to array can return results different than the actual structure of the XML object.

Something like the following code should give you an array with the correct info:

$array = array_map('strval', (array) $xml->Product);

Take care you cast those parts to string of which you'll get the data from (in the example done via strval()). In the opposite, json_encode() is not working well with SimpleXMLElement.

hakre
  • 193,403
  • 52
  • 435
  • 836
bob0the0mighty
  • 782
  • 11
  • 28
  • Thanks, reading this and after the comment by @BrainFooLong , simply passing LIBXML_NOCDATA as the third argument within simplexml_load_string() will solve this. I saw this in the docs while reading the comments as u had suggested :) – Craig van Tonder Dec 19 '14 at 19:32
  • 2
    @IndigoIdentity I can't stress enough that *LIBXML_NOCDATA is not necessary*. Just don't use hacks like `json_decode(json_encode($blah))`, and don't trust `var_dump`. Instead, read [the examples of how to use SimpleXML properly](http://php.net/manual/en/simplexml.examples-basic.php). – IMSoP Dec 20 '14 at 21:35