duanjiu1003 2011-09-28 08:31
浏览 29
已采纳

在PHP5中解析复杂的XML

Hello this is my first post. i'm trying to parse an xml using php, but i'm stuck with it

example of my xml :

<xml xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
     xmlns:rs='urn:schemas-microsoft-com:rowset'
     xmlns:z='#RowsetSchema'>
<s:Schema id='RowsetSchema'>
   <s:ElementType name='row' content='eltOnly' rs:CommandTimeout='30'>
      <s:AttributeType name='ows_LinkTitle' rs:name='Phone Number*' rs:number='1'>
         <s:datatype dt:type='string' dt:maxLength='512' />
      </s:AttributeType>
      <s:AttributeType name='ows_GSM_x0020_PUK' rs:name='GSM PUK' rs:number='2'>
         <s:datatype dt:type='string' dt:maxLength='512' />
      </s:AttributeType>
   </s:ElementType>
</s:Schema>
<rs:data>
   <z:row ows_LinkTitle='628558621183' ows_GSM_x0020_PUK='41016732'/>
   <z:row ows_LinkTitle='628558621181' ows_GSM_x0020_PUK='21783670'/>
</rs:data>
</xml>

i'm trying to get the value from each of 'z:row' Any help is very appreciated. Thanks

i'm trying this code and it doesnt work

$xml_all = simplexml_load_string($xmlnya);

echo '<ol>';
foreach ($xml_all->xml->rs->z as $zrow)
{
 echo '<li> ows_LinkTitle: '.$zrow['ows_LinkTitle'];
 echo '</li>';
}
echo '</ol>';
  • 写回答

2条回答 默认 最新

  • dongshan4549 2011-09-28 08:57
    关注

    The snippets below assume the (fixed) XML document is a string in $xml.

    SimpleXML

    $xml  = simplexml_load_string($xml);
    $rows = $xml->children('rs', TRUE)->data->children('z', TRUE)->row;
    foreach ($rows as $row) {
        $attributes = $row->attributes();
        $link_title = (string) $attributes->ows_LinkTitle;
        $gsm_puk    = (string) $attributes->ows_GSM_x0020_PUK;
        var_dump($link_title, $gsm_puk);
    }
    

    To read: SimpleXML usage, SimpleXMLElement::children(), SimpleXMLElement::attributes().

    DOM

    $doc = new DOMDocument;
    $doc->loadXML($xml);
    $rows = $doc->getElementsByTagNameNS('#RowsetSchema', 'row');
    foreach ($rows as $row) {
        $link_title = $row->getAttribute('ows_LinkTitle');
        $gsm_puk    = $row->getAttribute('ows_GSM_x0020_PUK');
        var_dump($link_title, $gsm_puk);
    }
    

    To read: DOMDocument::getElementsByTagNameNS(), DOMElement::getAttribute().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!