[PHP] xml转数组

这篇博客介绍了在PHP中如何将XML转换为数组的方法,主要聚焦于使用SimpleXMLElement类和simplexml_load_file()函数。内容包括SimpleXMLElement的官方文档简介和一个包含命名空间的XML文档的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

These functions/constants are available as of PHP 5.1.0, and the following core extensions rely on this libxml extension:
DOM,
libxml,
SimpleXML,
SOAP,
WDDX,
XSL,
XML,
XMLReader,
XMLRPCXMLWriter.

转换方法

php文档- xml操作

  • SimpleXMLElement 对象
  • simplexml_load_file() 方法

SimpleXMLElement 介绍

官方文档

代码示例

以下是带有命名空间的xml文档

$xml = <<<XML
<ns:getJournalsResponse xmlns:ns="http://jane.erasmusmc.org" xmlns:ax21="http://dataClasses.jane.erasmusmc.org/xsd">
    <ns:return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax21:Journal">
        <ax21:score>0.09468732609088781</ax21:score>
        <ax21:ai>10.7</ax21:ai>
        <ax21:airank>100</ax21:airank>
        <ax21:issn>0149-2195</ax21:issn>
        <ax21:journalAbbr>MMWR-MORBID MORTAL W</ax21:journalAbbr>
        <ax21:medlineIndexed>true</ax21:medlineIndexed>
        <ax21:name>MMWR. Morbidity and mortality weekly report</ax21:name>
        <ax21:openAccess>false</ax21:openAccess>
        <ax21:papers xsi:type="ax21:Paper">
            <ax21:authors>Nkengasong J</ax21:authors>
            <ax21:journal>MMWR. Morbidity and mortality weekly report</ax21:journal>
            <ax21:pmid>27880749</ax21:pmid>
            <ax21:title>Early Diagnosis of HIV Infection in Infants - One Caribbean and Six Sub-Saharan African Countries, 2011-2015.</ax21:title>
            <ax21:year>2016</ax21:year>
        </ax21:papers>
        <ax21:papers xsi:type="ax21:Paper">
            <ax21:score>0.6406980752944946</ax21:score>
            <ax21:authors>Dokubo EK</ax21:authors>
            <ax21:authors>Baddeley A</ax21:authors>
            <ax21:authors>Pathmanathan I</ax21:authors>
            <ax21:authors>Coggin W</ax21:authors>
            <ax21:authors>Firth J</ax21:authors>
            <ax21:authors>Getahun H</ax21:authors>
            <ax21:authors>Kaplan J</ax21:authors>
            <ax21:authors>Date A</ax21:authors>
            <ax21:journal>MMWR. Morbidity and mortality weekly report</ax21:journal>
            <ax21:pmid>25426652</ax21:pmid>
            <ax21:title>Provision of antiretroviral therapy for HIV-positive TB patients--19 countries, sub-Saharan Africa, 2009-2013.</ax21:title>
            <ax21:year>2014</ax21:year>
        </ax21:papers>
        <ax21:papers xsi:type="ax21:Paper">
            <ax21:score>0.6056187152862549</ax21:score>
            <ax21:authors>Kothegal N</ax21:authors>
            <ax21:authors>Wang A</ax21:authors>
            <ax21:authors>Jonnalagadda S</ax21:authors>
            <ax21:authors>MacNeil A</ax21:authors>
            <ax21:authors>Hassani AS</ax21:authors>
            <ax21:authors>Nkumbula T</ax21:authors>
            <ax21:authors>Patel H</ax21:authors>
            <ax21:authors>Parekh B</ax21:authors>
            <ax21:authors>Voetsch AC</ax21:authors>
            <ax21:journal>MMWR. Morbidity and mortality weekly report</ax21:journal>
            <ax21:pmid>33705366</ax21:pmid>
            <ax21:title>Screening for HIV Among Patients at Tuberculosis Clinics - Results from Population-Based HIV Impact Assessment Surveys, Malawi, Zambia, and Zimbabwe, 2015-2016.</ax21:title>
            <ax21:year>2021</ax21:year>
        </ax21:papers>
        <ax21:papers xsi:type="ax21:Paper">
            <ax21:score>0.596133828163147</ax21:score>
            <ax21:authors>Melgar M</ax21:authors>
            <ax21:authors>Nichols C</ax21:authors>
            <ax21:authors>Cavanaugh JS</ax21:authors>
            <ax21:authors>Kirking HL</ax21:authors>
            <ax21:authors>Surie D</ax21:authors>
            <ax21:authors>Date A</ax21:authors>
            <ax21:authors>Ahmedov S</ax21:authors>
            <ax21:authors>Maloney S</ax21:authors>
            <ax21:authors>Fukunaga R</ax21:authors>
            <ax21:authors>CDC Country Offices’ Tuberculosis/HIV Advisors</ax21:authors>
            <ax21:authors>National Ministries and Departments of Health Tuberculosis Program Managers</ax21:authors>
            <ax21:journal>MMWR. Morbidity and mortality weekly report</ax21:journal>
            <ax21:pmid>32214084</ax21:pmid>
            <ax21:title>Tuberculosis Preventive Treatment Scale-Up Among Antiretroviral Therapy Patients - 16 Countries Supported by the U.S. President's Emergency Plan for AIDS Relief, 2017-2019.</ax21:title>
            <ax21:year>2020</ax21:year>
        </ax21:papers>
        <ax21:pmcMonths>0</ax21:pmcMonths>
    </ns:return>
    <ns:return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax21:Journal">
        <ax21:score>0.07672679254569237</ax21:score>
        <ax21:ai>1.1</ax21:ai>
        <ax21:airank>81</ax21:airank>
        <ax21:issn>1932-6203</ax21:issn>
        <ax21:journalAbbr>PLOS ONE</ax21:journalAbbr>
        <ax21:medlineIndexed>true</ax21:medlineIndexed>
        <ax21:name>PloS one</ax21:name>
        <ax21:openAccess>true</ax21:openAccess>
        <ax21:papers xsi:type="ax21:Paper">
            <ax21:score>0.6838493347167969</ax21:score>
            <ax21:authors>Girault P</ax21:authors>
            <ax21:authors>Misa Wong C</ax21:authors>
            <ax21:authors>Jittjang S</ax21:authors>
            <ax21:authors>Fongkaew K</ax21:authors>
            <ax21:authors>Phanuphak P</ax21:authors>
            <ax21:journal>PloS one</ax21:journal>
            <ax21:pmid>34398926</ax21:pmid>
            <ax21:title>Uptake of oral fluid-based HIV self-testing among men who have sex with men and transgender women in Thailand.</ax21:title>
            <ax21:year>2021</ax21:year>
        </ax21:papers>
        <ax21:papers xsi:type="ax21:Paper">
            <ax21:score>0.645950436592102</ax21:score>
            <ax21:authors>Kripke K</ax21:authors>
            <ax21:authors>Reed J</ax21:authors>
            <ax21:journal>PloS one</ax21:journal>
            <ax21:pmid>27783681</ax21:pmid>
            <ax21:title>Impact and Cost of Scaling Up Voluntary Medical Male Circumcision for HIV Prevention in the Context of the New 90-90-90 HIV Treatment Targets.</ax21:title>
            <ax21:year>2016</ax21:year>
        </ax21:papers>
        <ax21:pmcMonths>0</ax21:pmcMonths>
    </ns:return>
</ns:getJournalsResponse>
XML;

try {
    //需要处理命名空间前缀
    // $sx = new SimpleXMLElement($xml, 0, false, 'ns', true);
    $sx = new SimpleXMLElement($xml, 0, false);
    $ns = $sx->getNamespaces(true);
    $child = $sx->children($ns['ns']);
    foreach ($child->return as $out_ns) {
        // var_dump($out_ns);
        $as = $out_ns->getNamespaces(true);
        $s = $out_ns->children($as['ax21']);
        // var_dump($s);
    }
} catch (Exception $e) {

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值