I'm looping over a array like so:
$objDOM->load("URL");
foreach ($objDOM->getElementsByTagName('CRPublication') as $node) {
$itemVacature = array (
'naam' => $node->getElementsByTagName('titleInformation')->item(0)->nodeValue
}
Now the loaded XML node has an dynamic id. Is it possible to get the value from the id where getElementsByTagName is titleInformation?
The xml node I need to get the id from looks like so:
<createdBy>
<CRUser id="122"></CRUser>
</createdBy>
<modifiedBy>
<CRUser id="122"></CRUser>
</modifiedBy>
<owner>
<CRUser id="122"></CRUser>
</owner>
As you can see I only get the id from this external company. I have to convert these id's to values that make sense.
Thanks in advance!
Example of loop on a loaded an XML object using DOM Document
having parent,child and grand-child structure.
$xml ='<?xml version="1.0" encoding="utf-8"?>
<root>
<Parent>
<child>child 1</child>
<child>child 2</child>
<child>child 3</child>
<subParent>
<Grandchild>Grandchild 1</Grandchild>
<Grandchild>Grandchild 2</Grandchild>
<Grandchild>Grandchild 3</Grandchild>
</subParent>
</Parent>
<Parent>
<child>child 4</child>
<child>child 5</child>
<child>child 6</child>
<subParent>
<createdBy>
<CRUser id="122"></CRUser>
</createdBy>
<modifiedBy>
<CRUser id="122"></CRUser>
</modifiedBy>
<owner>
<CRUser id="122"></CRUser>
</owner>
</subParent>
</Parent>
</root>';
$doc = new DOMDocument();
$doc->preserveWhiteSpace = false;
$doc->loadXML($xml);
$i=0;
while(is_object($obj = $doc->getElementsByTagName("Parent")->item($i))) {
foreach($obj->childNodes as $nodename) {
if($nodename->nodeName=='subParent') {
foreach($nodename->childNodes as $subNodes) {
if($subNodes->nodeName=='owner') {
if(!empty($subNodes->childNodes['CRUser']->getAttribute("id"))) {
echo $subNodes->childNodes['CRUser']->getAttribute("id")."
";
}
}
if($subNodes->nodeName=='createdBy') {
if(!empty($subNodes->childNodes['CRUser']->getAttribute("id"))) {
echo $subNodes->childNodes['CRUser']->getAttribute("id")."
";
}
}
if($subNodes->nodeName=='modifiedBy') {
if(!empty($subNodes->childNodes['CRUser']->getAttribute("id"))) {
echo $subNodes->childNodes['CRUser']->getAttribute("id")."
";
}
}
}
} else {
if(!empty($nodename->getAttribute("id"))) {
echo $nodename->getAttribute("id")."
";
}
}
}
$i++;
}
/* Output
122
122
122
*/
Check the output of the above example here
Use getAttribute
assuming the schema to be same modified code. Try this.
$objDOM->load("URL");
foreach ($objDOM->getElementsByTagName('CRPublication') as $node) {
$itemVacature = array (
'naam' => $node->getElementsByTagName('titleInformation')->item(0)->nodeValue,
'id' => $node->getElementsByTagName('titleInformation')->item(0)->getAttribute('id'),
);
}