我有以下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>
如何在php中获取以下元素及其子元素,以获取名称=“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>
我需要抓住上面提到的这一部分并将其转换为字符串,以便我可以将它传递给PHP应用程序使用的c#dll。
答案 0 :(得分:1)
在输出之前,当然,您需要首先搜索以下值。您可以使用->attributes()
方法。最后,只需使用asXML
方法。像这样:
$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();