使用SimpleXMLElement读取特定的xml数据

时间:2022-11-26 01:35:23

I have challenges reading the value of "CMBDESTINOSV" and each of "chk_xxxxx" in the "coberData" using SimpleXMLElement in the below code snippet. The code snippet is from an xml response.

我在下面的代码片段中使用SimpleXMLElement在“coberData”中读取“CMBDESTINOSV”和“chk_xxxxx”的值时遇到了挑战。代码段来自xml响应。

I have tried $xml->coberData->chk_C0000; $xml->riskData->CMBDESTINOSV->option->cd[1]; but nothing was returned. Please help.

我试过$ xml-> coberData-> chk_C0000; $ XML-> riskData-> CMBDESTINOSV->选项 - > CD [1];但没有任何回报。请帮忙。

<root> 
  <riskData label="Risk details"> 
    <CMBDESTINOSV afectaAlPrecio="true" dataName="CMBDESTINOSV" id="CMBDESTINOSV" name="CMBDESTINOSV" type="5" style="width:120" label="Destination" visible="true" maxlength="2" readOnly="false" obligatorio="true" size="3" newLine="false" etiquetaEncima="false" value="-1">-1
      <option>
        <cd>-1</cd>
        <ds/>
      </option>  
      <option>
        <cd>7</cd>
        <ds>05 - Wordlwide Excluding USA &amp; Canada - Wordlwide Excluding USA &amp; Canada</ds>
      </option>  
      <option>
        <cd>10</cd>
        <ds>08 - WORLDWIDE - ALL COUNTRIES</ds>
      </option> 
    </CMBDESTINOSV>
  </riskData>  
  <coberData label="Coverage details"> 
    <chk_C0000 afectaAlPrecio="true" dataName="chk_C0000" id="chk_C0000" name="chk_C0000" type="6" label="BAGAGES" visible="true" onClick="" maxlength="180" readOnly="false" obligatorio="false" size="180" newLine="false" etiquetaEncima="false" value="0"/>  
    <chk_I0000 afectaAlPrecio="true" dataName="chk_I0000" id="chk_I0000" name="chk_I0000" type="6" label="MEDICAL COMPLEMENTARY SERVICES" visible="true" onClick="" maxlength="180" readOnly="true" obligatorio="true" size="180" newLine="false" etiquetaEncima="false" value="1"/>  
    <chk_F0000 afectaAlPrecio="true" dataName="chk_F0000" id="chk_F0000" name="chk_F0000" type="6" label="PERSONAL ACCIDENTS" visible="true" onClick="" maxlength="180" readOnly="true" obligatorio="true" size="180" newLine="false" etiquetaEncima="false" value="1"/>  
    <chk_D0002 afectaAlPrecio="true" dataName="chk_D0002" id="chk_D0002" name="chk_D0002" type="6" label="TRAVEL DELAY" visible="true" onClick="" maxlength="180" readOnly="true" obligatorio="true" size="180" newLine="false" etiquetaEncima="false" value="1"/>  
    <chk_G0000 afectaAlPrecio="true" dataName="chk_G0000" id="chk_G0000" name="chk_G0000" type="6" label="PERSONAL LIABILITY" visible="true" onClick="" maxlength="180" readOnly="true" obligatorio="true" size="180" newLine="false" etiquetaEncima="false" value="1"/>  
    <chk_B0003 afectaAlPrecio="true" dataName="chk_B0003" id="chk_B0003" name="chk_B0003" type="6" label="PERSONAL ASSISTANCE" visible="true" onClick="" maxlength="180" readOnly="true" obligatorio="true" size="180" newLine="false" etiquetaEncima="false" value="1"/>  
    <chk_CAN afectaAlPrecio="true" dataName="chk_CAN" id="chk_CAN" name="chk_CAN" type="6" label="CANCELLATION" visible="true" onClick="" maxlength="180" readOnly="false" obligatorio="false" size="180" newLine="false" etiquetaEncima="false" value="0"/>  
    <chk_U0030 afectaAlPrecio="true" dataName="chk_U0030" id="chk_U0030" name="chk_U0030" type="6" label="ACCIDENTAL DAMAGE" visible="true" onClick="" maxlength="180" readOnly="true" obligatorio="true" size="180" newLine="false" etiquetaEncima="false" value="1"/> 
  </coberData> 
</root>

1 个解决方案

#1


0  

Your code worked fine to retrieve the target element, only it prints inner text of the element, which is empty. If you call asXML() instead, it will print the correct element :

您的代码可以很好地检索目标元素,只打印元素的内部文本,这是空的。如果你改为调用asXML(),它将打印正确的元素:

echo $xml->coberData->chk_C0000->asXML();

If you meant to access one of the attributes instead, try this way :

如果您打算访问其中一个属性,请尝试以下方式:

echo $xml->coberData->chk_C0000->attributes()->afectaAlPrecio;

eval.in demo

output :

<chk_C0000 afectaAlPrecio="true" dataName="chk_C0000" id="chk_C0000" name="chk_C0000" type="6" label="BAGAGES" visible="true" onClick="" maxlength="180" readOnly="false" obligatorio="false" size="180" newLine="false" etiquetaEncima="false" value="0"/>

true

#1


0  

Your code worked fine to retrieve the target element, only it prints inner text of the element, which is empty. If you call asXML() instead, it will print the correct element :

您的代码可以很好地检索目标元素,只打印元素的内部文本,这是空的。如果你改为调用asXML(),它将打印正确的元素:

echo $xml->coberData->chk_C0000->asXML();

If you meant to access one of the attributes instead, try this way :

如果您打算访问其中一个属性,请尝试以下方式:

echo $xml->coberData->chk_C0000->attributes()->afectaAlPrecio;

eval.in demo

output :

<chk_C0000 afectaAlPrecio="true" dataName="chk_C0000" id="chk_C0000" name="chk_C0000" type="6" label="BAGAGES" visible="true" onClick="" maxlength="180" readOnly="false" obligatorio="false" size="180" newLine="false" etiquetaEncima="false" value="0"/>

true