0

I have the following xml doc

   <assumption_list>
<assumption name="Sample1567" id="9" description="reinvestment description" is_shared="no">
<reinvestment>
<reinvestmentType>Global REINV</reinvestmentType>
<autoReinv autoReinvEnd="6 mo before Final Maturity"/>
<atRP currentPrinCash="NO" recoveries="NO" scheduled="NO" prepayment="NO">0</atRP>
</reinvestment>
</assumption>
<assumption name="Sample" id="8" description="reinvestment description" is_shared="no">
<reinvestment>
<reinvestmentType>Global REINV1</reinvestmentType>
<autoReinv autoReinvEnd="6 mo1 before Final Maturity"/>
<atRP currentPrinCash="7778" recoveries="NO" scheduled="NO" prepayment="NO">0</atRP>
</reinvestment>
</assumption>
</assumption_list>

How can i grab the following element and its child element in php for assumption tag with name="Sample1567"

<reinvestment>
<reinvestmentType>Global REINV</reinvestmentType>
<autoReinv autoReinvEnd="6 mo before Final Maturity"/>
<atRP currentPrinCash="NO" recoveries="NO" scheduled="NO" prepayment="NO">0</atRP>
</reinvestment>

I need to grab this section mentioned above and convert it to a string so that i can pass it c# dll used by PHP Application.

user3290807
  • 381
  • 1
  • 5
  • 23

1 Answers1

1

Before outputing it, of course, you need to search first that following value. You can use ->attributes() method for that. Then in the end, just use asXML method. Like this:

$xml_string = '<assumption_list><assumption name="Sample1567" id="9" description="reinvestment description" is_shared="no"><reinvestment><reinvestmentType>Global REINV</reinvestmentType><autoReinv autoReinvEnd="6 mo before Final Maturity"/><atRP currentPrinCash="NO" recoveries="NO" scheduled="NO" prepayment="NO">0</atRP></reinvestment></assumption><assumption name="Sample" id="8" description="reinvestment description" is_shared="no"><reinvestment><reinvestmentType>Global REINV1</reinvestmentType><autoReinv autoReinvEnd="6 mo1 before Final Maturity"/><atRP currentPrinCash="7778" recoveries="NO" scheduled="NO" prepayment="NO">0</atRP></reinvestment></assumption></assumption_list>';
$xml = simplexml_load_string($xml_string);
$return = new SimpleXMLElement('<reinvestment/>');
$reinvestment = null;
$needle = 'Sample1567';
foreach($xml->assumption as $values) {
    if($values->attributes()['name'] == $needle) {
        $reinvestment = $values->reinvestment;
    }

}

echo $reinvestment->asXML();
user1978142
  • 7,946
  • 3
  • 17
  • 20