1

I have serious issues parsing xml using simpleXML in PHP, since both root element and children have colons in their name. I've search for many solutions and found one that works. However, my problem is more specific since these colon elements are nested.

As you can see, the children of the root element can vary from 1 to N (Snippet of the original xml):

     <bpmndi:BPMNEdge bpmnElement="sid-96E075E5-D515-46E7-BED2-9A284F1F5153" id="sid-96E075E5-D515-46E7-BED2-9A284F1F5153_gui">
        <omgdi:waypoint x="985.0" y="220.0"/>
        <omgdi:waypoint x="1055.5" y="220.0"/>
        <omgdi:waypoint x="1055.5" y="160.0"/>
     </bpmndi:BPMNEdge>
     <bpmndi:BPMNEdge bpmnElement="sid-F36DC404-7095-4653-8176-FB99ADAB8DEC" id="sid-F36DC404-7095-4653-8176-FB99ADAB8DEC_gui">
        <omgdi:waypoint x="927.0" y="580.0"/>
        <omgdi:waypoint x="1005.0" y="580.0"/>
     </bpmndi:BPMNEdge>

What I've currently done (and works, the question is in the comment part of the code)

$xml->registerXPathNamespace('bpmndi', 'specific-url');
$xml->registerXPathNamespace('omgdi', 'specific-url');
$edges = $xml->xpath('//bpmndi:BPMNEdge');
$point = $xml->xpath('//omgdi:waypont');

$i = 0;
foreach ($edges as $edge) {

echo (string) $edge[0]['bpmnElement'];  
//how to get the children of this element? E.g. 1 or N waypoints and their x and y?
$i++;
}
hakre
  • 193,403
  • 52
  • 435
  • 836
None None
  • 537
  • 2
  • 8
  • 15

1 Answers1

2

You can easily iterate over the children() of the element (see http://php.net/simplexmlelement.children):

foreach($edge->children('omgdi', true) as $child) {
    echo sprintf("waypoint %s / %s\n", $child->attributes()->x, $child->attributes()->y);
}

It is necessary to provide your namespace when calling children(), otherwise the call will only return children of the default namespace.

hakre
  • 193,403
  • 52
  • 435
  • 836
wolfo
  • 36
  • 4