How to access Xml child node value with by giveing child node name in condition In PHP using XMLREADER?

23 Views Asked by At

I need to retrieve the value of the node from my XML document, but only when the disposition attribute is equal to 'choicegroup'. Otherwise, I want to retrieve the value of the node.

"I have a large amount of XML data and I'm trying to decide whether to use XMLReader or SimpleXML. I've heard that SimpleXML can cause memory issues with very large XML documents, so I'm wondering if it's a good idea to use XMLReader. Are there any other suggestions for handling large XML documents?"

<infoObjects size="193">
<infoObject type="Publication" statusProcess="3" statusVisible="true" createdOn="2015-03-26T13:31:03.483+01:00" id="117482684" updatedOn="2021-03-19T11:08:48.783+01:00">
<attribute disposition="choicegroup" language="0" name="peerReviewed">
<data>20955</data>
<additionalInfo>Yes</additionalInfo>
</attribute>
<infoObject>

<infoObject type="Publication" statusProcess="3" statusVisible="true" createdOn="2014-04-04T09:22:56.766+02:00" id="113557224" updatedOn="2020-01-15T08:21:52.474+01:00">

<attribute disposition="choicegroup" language="0" name="complete author relations">
<data>18389</data>
<additionalInfo>Yes</additionalInfo>
</attribute>

</infoObject>
</infoObjects>

$infoObjects=array();
while ($xml->read()) {

            if ($xml->nodeType == XMLReader::ELEMENT && $xml->name == 'infoObject') {

                $infoObject=array();
                $infoObject['id']=$xml->getAttribute('id');
                $infoObject['type']=$xml->getAttribute('type'); 
                $infoObject['statusVisible']=$xml->getAttribute('statusVisible'); 
                $infoObject['createdOn']=$xml->getAttribute('updatedOn');
                $infoObject['updatedOn']=$xml->getAttribute('updatedOn');

                // echo $xml->getAttribute('id');
                
            }
           
           

            if ($xml->nodeType == XMLReader::ELEMENT && $xml->name == 'attribute') {
                // do something with the attribute element
                $nameAttribute = $xml->getAttribute('name');
                $languageAttribute=$xml->getAttribute('language');
                $dispositionAttribue=$xml->getAttribute('disposition');

                if ($languageAttribute==='1') {
                    
                    $nameAttribute=$nameAttribute."_en";

                }
                else{
                    $nameAttribute=$nameAttribute;
                }

                if ($dispositionAttribue==='choicegroup') {

                    // I want <additionalInfo>Yes</additionalInfo> value in the $attribute_value variable but its not working
                    $xml->read();
                    $attribute_value = $xml->readInnerXML();
                }
                else{
                    $xml->read();
                    
                    $attribute_value = $xml->readString();
                }

                $infoObject[strtolower($nameAttribute)]=$attribute_value;
                

                if ($nameAttribute=="publYear") {
                        $infoObjects[]=$infoObject;
                    }
                                
            }
             
        }

        $xml->close();
0

There are 0 best solutions below