duanpo1498 2016-01-27 11:21
浏览 99
已采纳

使用SimpleXMLElement读取特定的xml数据

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.

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

<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条回答 默认 最新

  • dongzong8110 2016-01-27 12:00
    关注

    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 :

    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;
    

    <kbd>eval.in demo</kbd>

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图