Friday, 6 September 2013

Prase xml with colon - nested structure (php and simpleXML)

Prase xml with colon - nested structure (php and simpleXML)

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++;
}

No comments:

Post a Comment