Hello I need some help to find my XML elements with PHP and xpath.
This is a part of my xml:
"processen": {
"proces": [
{
"@attributes": {
"id": "B1221"
},
"velden": {
"kernomschrijving": "activiteit aanleggen alarminstallatie",
"model-kernomschrijving": "aanleggen alarminstallatie",
"naam": "Het beoordelen van een alarminstallatie",
"standaard-dossiernaam": {
"@attributes": {
"ref": "SCN0000029"
}
},
"[tag:taakveld]": "Bouwzaken & Procedures",
"proceseigenaar": "Bouwzaken",
"toelichting-proces": "bla die bla.",
"aanleiding": "Dit werkproces wordt intern getriggerd.",
"opmerking-proces": {
"@attributes": {
"ref": "SCN0000036"
}
},
"exportprofiel": {
"@attributes": {
"ref": "SCN0000037"
},
},...
For example I want to be able to find the id (fast) and access all the elements under the id B1221
I tried this in al kind of variants but none works:
$xml = simplexml_load_file( $filename );
$proces = $xml->xpath("//processen/proces/@attributes/id=B1221");
$proces = $xml->xpath("//processen/proces[@attributes/id=B1221]");
It always returns an empty array...
Thanks for your help.
My Answer:
You can try the following code to find the elements under the id "B1221":
php
$xml = simplexml_load_file($filename);
$proces = $xml->xpath("//proces[@id='B1221']");
foreach ($proces as $p) {
$id = (string) $p['id'];
$kernomschrijving = (string) $p->velden->kernomschrijving;
$modelKernomschrijving = (string) $p->velden->{'model-kernomschrijving'};
$naam = (string) $p->velden->naam;
$standaardDossiernaamRef = (string) $p->velden->{'standaard-dossiernaam'}['ref'];
$tagTaakveld = (string) $p->velden->{'[tag:taakveld]'};
$proceseigenaar = (string) $p->velden->proceseigenaar;
$toelichtingProces = (string) $p->velden->{'toelichting-proces'};
$aanleiding = (string) $p->velden->aanleiding;
$opmerkingProcesRef = (string) $p->velden->{'opmerking-proces'}['ref'];
$exportprofielRef = (string) $p->velden->exportprofiel['ref'];
// Do something with the found elements
}
This code will find the element with the id "B1221" and then extract the values of the specified elements under it. You can then use these values as needed in your application.
Rate this post
3 of 5 based on 3126 votesComments