通过PHP访问xml文档的每个子级

时间:2018-09-27 08:58:32

标签: php xml dom simplexml

我试图手动将xml文件转换为json文件,因此不使用json_encode / json_decode,也不要将XML保存到文件系统中,并使用readFromFile类型技术。因此,我想使用本机PHP XML解析模块,循环和数组/集合解析来实现此目的。我设法将xml转换为字符串,但是如何访问xml文件中的每个子项?

$string = <<<XML
<?xml version='1.0'?> 
<priip>
  <data>
    <product>
      <priipCloudProductTemplate>otc</priipCloudProductTemplate>
      <priipCloudProductType>fxSwap</priipCloudProductType>
      <productIdentifier>RBI_fxSwap_EURUSD_long_1Y2D_EUR</productIdentifier>
    </product>
    <manufacturer>
      <id>rbi</id>
      <nameLong>Raiffeisen Bank International AG</nameLong>
      <nameShort>RBI</nameShort>
      <address>Am Stadtpark 9, 1030 Wien, Austria</address>
      <telephoneNumber>+43 1 71707 0</telephoneNumber>
      <website>http://www.rbinternational.com</website>
      <email>complaints@rbinternational.com</email>
    </manufacturer>
    <document>
      <type>final</type>
    </document>
    <properties>
      <includeEarlyRedemptionInExtraordinaryEventsAlert>true</includeEarlyRedemptionInExtraordinaryEventsAlert>
    </properties>
    <tradeDate>2018-01-18</tradeDate>
    <effectiveDate>2018-01-20</effectiveDate>
    <fxSwap>
      <holder>client</holder>
      <currencyPair>EURUSD</currencyPair>
      <notionalAmount>1000000</notionalAmount>
      <notionalAmountCurrency>EUR</notionalAmountCurrency>
      <terminationDate>2019-01-20</terminationDate>
      <forwardRate>
        <value>1.25620</value>
      </forwardRate>
      <spotRate>
        <value>1.2207</value>
      </spotRate>
    </fxSwap>
    <costs>
      <entryCosts>0.0025</entryCosts>
    </costs>
    <riskMeasures version="v1.0">
      <sriRelatedValues>
        <valueAtRisk>0</valueAtRisk>
        <valueAtRiskEquivalentVolatility>0</valueAtRiskEquivalentVolatility>
        <mrm>7</mrm>
        <crm>2</crm>
        <sri>7</sri>
      </sriRelatedValues>
      <performanceScenariosRelatedValues>
        <positiveScenarioPayoutRHP>11139.633068665</positiveScenarioPayoutRHP>
        <positiveScenarioActualReturnRHP>0.1139633069</positiveScenarioActualReturnRHP>
        <positiveScenarioAverageReturnPerYearRHP>0.114276</positiveScenarioAverageReturnPerYearRHP>
        <positiveScenarioPayoutIHP1>null</positiveScenarioPayoutIHP1>            
        <stressScenarioPayoutRHP>6841.9699464563</stressScenarioPayoutRHP>
        <stressScenarioActualReturnRHP>-0.3158030054</stressScenarioActualReturnRHP>
        <stressScenarioAverageReturnPerYearRHP>-0.316671</stressScenarioAverageReturnPerYearRHP>
        <stressScenarioPayoutIHP1>null</stressScenarioPayoutIHP1>
        <stressScenarioActualReturnIHP1>null</stressScenarioActualReturnIHP1>
        <stressScenarioAverageReturnPerYearIHP1>null</stressScenarioAverageReturnPerYearIHP1>
        <stressScenarioPayoutIHP2>null</stressScenarioPayoutIHP2>
        <stressScenarioActualReturnIHP2>null</stressScenarioActualReturnIHP2>
        <stressScenarioAverageReturnPerYearIHP2>null</stressScenarioAverageReturnPerYearIHP2>
      </performanceScenariosRelatedValues>
    </riskMeasures>
    <costOutputs>
      <costsOverTime>
        <totalCostsRHP>
          <value>24.4219183409</value>
        </totalCostsRHP>
        <totalCostsIHP1>
          <value>null</value>
        </totalCostsIHP1>
        <totalCostsIHP2>
          <value>null</value>
        </totalCostsIHP2>

        <reductionInActualYieldRHP>
          <value>0.0024421918</value>
        </reductionInActualYieldRHP>
        <reductionInActualYieldIHP1>
          <value>null</value>
        </reductionInActualYieldIHP1>
        <reductionInActualYieldIHP2>
          <value>null</value>
        </reductionInActualYieldIHP2>
        <reductionInYieldRHP>
          <value>0.0024489008</value>
        </reductionInYieldRHP>
        <reductionInYieldIHP1>
          <value>null</value>
        </reductionInYieldIHP1>
        <reductionInYieldIHP2>
          <value>null</value>
        </reductionInYieldIHP2>
      </costsOverTime>
      <compositionOfCosts>
        <actualEntryCosts>
          <value>0.0024421918</value>
        </actualEntryCosts>
        <actualOtherRecurringCostsPA>
          <value>null</value>
        </actualOtherRecurringCostsPA>
        <actualExitCosts>
          <value>0</value>
        </actualExitCosts>
        <entryCosts>
          <value>0.0024489008</value>
        </entryCosts>
        <otherRecurringCostsPA>
          <value>null</value>
        </otherRecurringCostsPA>
        <exitCosts>
          <value>0</value>
        </exitCosts>
      </compositionOfCosts>
    </costOutputs>
  </data>
</priip>    
XML;

$xml = simplexml_load_string($string);
print_r($xml);

我尝试使用此功能

$xml=simplexml_load_string($string);
foreach ($xml->children() as $child){
  echo "Child node: " . $child . "<br>";
}

但这不起作用

@穆罕默德 @rokoko 我什至不知道你是否是同一个人。 Almost the same problem.

1 个答案:

答案 0 :(得分:1)

使用xpath()通过XPath选择元素。 *选择文档中的所有标签。

$xml = simplexml_load_string($string);
foreach ($xml->xpath('//*') as $node){    
    // $node is element
}

demo中查看结果