1

I have a simple xml structure:

<?xml version="1.0"?>
<fmxmlsnippet type="FMObjectList">
    <Step enable="True" id="141" name="Set Variable">
        <Value>
            <Calculation><![CDATA[Get(RecordID)]]></Calculation>
        </Value>
        <Repetition>
            <Calculation><![CDATA[1]]></Calculation>
        </Repetition>
        <Name>$currentRecord</Name>
    </Step>
</fmxmlsnippet>

What I'm trying to do is pull out the attribute values for the Step node as well as the CDATA values form the Calculation child node for the Value and Repetition nodes.

I can get the step attribute values without an issue:

$iterator = new SimpleXMLIterator($xml);

for($iterator->rewind() ; $iterator->valid() ; $iterator->next()){
     $stepId = $iterator->current()->attributes()->id->__toString();
     $stepName = $iterator->current()->attributes()->name->__toString();
     $stepEnable= $iterator->current()->attributes()->enable->__toString();
}

But I'm having a problem getting the CDATA. I've tried numerous methods of getting it without success.

I noticed at one point that when I die and var_dump the result of $iterator->current() in my for loop the CDATA doesn't even look like it is recognized:

object(SimpleXMLIterator)#3 (4) {
  ["@attributes"]=>
  array(3) {
    ["enable"]=>
    string(4) "True"
    ["id"]=>
    string(3) "141"
    ["name"]=>
    string(12) "Set Variable"
  }
  ["Value"]=>
  object(SimpleXMLIterator)#4 (1) {
    ["Calculation"]=>
    object(SimpleXMLIterator)#6 (0) {
    }
  }
  ["Repetition"]=>
  object(SimpleXMLIterator)#5 (1) {
    ["Calculation"]=>
    object(SimpleXMLIterator)#6 (0) {
    }
  }
  ["Name"]=>
  string(14) "$currentRecord"
}

It's like the content of the Calculation nodes is empty.

Is there a way of getting the CDATA?

Chris Schmitz
  • 20,160
  • 30
  • 81
  • 137

1 Answers1

0

No, that var_dump() is just misleading, it shows its empty but its it is there. Simply cast it then assign:

for($iterator->rewind() ; $iterator->valid() ; $iterator->next()){
     $stepId = $iterator->current()->attributes()->id->__toString();
     $stepName = $iterator->current()->attributes()->name->__toString();
     $stepEnable= $iterator->current()->attributes()->enable->__toString();

     $calculation = (string) $iterator->current()->Value->Calculation;
     $repitition = (string) $iterator->current()->Repetition->Calculation;

     echo $calculation . '<br/>' . $repitition;
}

Sample Output

Kevin
  • 41,694
  • 12
  • 53
  • 70
  • Fantastic! I wish I had seen your comment before going to bed last night, it would have saved me some tossing and turning :P I appreciate the help (I didn't know about casting) and also for the link to http://codepad.viper-7.com/ in general, that's a helpful tool! – Chris Schmitz Nov 13 '14 at 20:04