douyin8813 2015-11-19 10:18
浏览 26
已采纳

PHP SimpleXML用于复杂的XML来获取数据[关闭]

I try to get some data from some XML, which are very complex for me, so i ask you to help.

The XML is

<?xml version="1.0" encoding="UTF-8"?> <OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">    <responseDate>2015-11-19T09:15:23Z</responseDate>    <request verb="GetRecord" identifier="oai:nava.hu:Document-2359008" metadataPrefix="navadoc">http://10.10.1.6/dev/navadoc/index.php</request> <GetRecord>
      <record>
         <header>
            <identifier>oai:nava.hu:Document-2359008</identifier>
            <datestamp>2015-11-19T08:51:52Z</datestamp>
         </header>
         <metadata>
            <navadoc:document xmlns:navadoc="http://nava.hu/oai-pmh/navadoc/xsd/" xmlns:document="http://nava.hu/oai-pmh/navadoc/xsd/document/" xsi:schemaLocation="http://nava.hu/oai-pmh/navadoc/xsd/ http://nava.hu/oai-pmh/navadoc/xsd/navadoc.xsd">
               <document:main>
                  <id>2359008</id>
                  <title>Summa</title>
                  <provider id="nava">NAVA</provider>
                  <collection id="101">Magyar Televízió 1</collection>
                  <type>document</type>
               </document:main>
               <document:meta>
                  <broadcast>
                     <date type="day">2015-11-06</date>
                     <date type="start_time">2015-11-06 14:17:19</date>
                     <date type="end_time">2015-11-06 14:39:07</date>
                     <date type="scheduled">2015-11-06 14:15:00</date>
                  </broadcast>
                  <duration>00:21:48</duration>
                  <genre type="NAVA">háttér- és közéleti műsor</genre>
                  <genre type="EBU">3.1.3 TÁRSADALMI/POLITIKAI MŰSOROK</genre>
                  <date type="production_year">2015</date>
                  <description type="guide">(nincs kh.) Közben: Tőzsdenyitás, Egy Perc Híradó Feliratozva a Teletext 111. oldalán. (26 perc) Summa, a Magyar Televízió új háttérműsora. Miért kellett magyar tulajdonba venni az étkezési jegy üzletet? Miért kell visszaszerezni a külföldi cégektől a közüzemi vállalatokat,és miért kell nekünk magyar járműgyártás? Miért kerültek egyáltalán korábban külföldi cégek kezébe ezek a komoly nyereséget termelő vállalatok? Mitől jobb gazda a magyar állam, mint a külföldi befektető? Kérdések, amelyekre a legilletékesebbek válaszolnak a Summában, szerdán, az Este című műsor után. Hírek és ami mögöttük van.</description>
                  <description type="provider" />
                  <description type="technote" />
                  <description type="subtitle" />
                  <description type="main" />
                  <description type="private">- projekt: full FELDOLGOZÁS VÁLTOZÁSA 2015-08-18 ADÁSNAPTÓL! 1-es projektfeladatban lesznek részletesebb adatokkal, daraboláskor csak sablon+MSZI ha van Törzsidő: M1, péntek, 14:15 feldolgozás:kulcsképek, tárgyszavak; (új stáblista kissé nehezen olvasható...) Spotokkal megszakítva, de egyelőre egybevágjuk</description>
                  <attributes>
                     <color>Színes</color>
                     <sound>Hangos</sound>
                     <screenformat>N/A</screenformat>
                  </attributes>
                  <agelimit>korhatár nélkül</agelimit>
                  <nava>
                     <document_level>1</document_level>
                     <processing_level>3</processing_level>
                     <processing_state>2</processing_state>
                     <access since="2015-11-09">FREE</access>
                     <validated>false</validated>
                     <collectable>true</collectable>
                     <hasvideo>true</hasvideo>
                     <hassubtitle>true</hassubtitle>
                     <preview>00:00:00</preview>
                  </nava>
                  <language iso="hun">magyar</language>
                  <title type="Főcím" id="751954">Summa</title>
                  <image image="allframe/2015/11/06/m1-87212/m1-87212-08221900.jpg" time="00:00:00" />
                  <image image="allframe/2015/11/06/m1-87212/m1-87212-08230000.jpg" time="00:00:41" />
                  <image image="allframe/2015/11/06/m1-87212/m1-87212-08240000.jpg" time="00:01:41" />
                  <image image="allframe/2015/11/06/m1-87212/m1-87212-08250000.jpg" time="00:02:41" />
                  <image image="allframe/2015/11/06/m1-87212/m1-87212-08260000.jpg" time="00:03:41" />
                  <image image="allframe/2015/11/06/m1-87212/m1-87212-08270000.jpg" time="00:04:41" />
                  <image image="allframe/2015/11/06/m1-87212/m1-87212-08280000.jpg" time="00:05:41" />
                  <image image="allframe/2015/11/06/m1-87212/m1-87212-08290000.jpg" time="00:06:41" />
                  <image image="allframe/2015/11/06/m1-87212/m1-87212-08300000.jpg" time="00:07:41" />
                  <image image="allframe/2015/11/06/m1-87212/m1-87212-08310000.jpg" time="00:08:41" />
                  <image image="allframe/2015/11/06/m1-87212/m1-87212-08320000.jpg" time="00:09:41" />
                  <image image="allframe/2015/11/06/m1-87212/m1-87212-08330000.jpg" time="00:10:41" />
                  <image image="allframe/2015/11/06/m1-87212/m1-87212-08340000.jpg" time="00:11:41" />
                  <image image="allframe/2015/11/06/m1-87212/m1-87212-08350000.jpg" time="00:12:41" />
                  <image image="allframe/2015/11/06/m1-87212/m1-87212-08360000.jpg" time="00:13:41" />
                  <image image="allframe/2015/11/06/m1-87212/m1-87212-08370000.jpg" time="00:14:41" />
                  <image image="allframe/2015/11/06/m1-87212/m1-87212-08380000.jpg" time="00:15:41" />
                  <image image="allframe/2015/11/06/m1-87212/m1-87212-08390000.jpg" time="00:16:41" />
                  <image image="allframe/2015/11/06/m1-87212/m1-87212-08400000.jpg" time="00:17:41" />
                  <image image="allframe/2015/11/06/m1-87212/m1-87212-08410000.jpg" time="00:18:41" />
                  <image image="allframe/2015/11/06/m1-87212/m1-87212-08420000.jpg" time="00:19:41" />
                  <image image="allframe/2015/11/06/m1-87212/m1-87212-08430000.jpg" time="00:20:41" />
                  <image image="allframe/2015/11/06/m1-87212/m1-87212-08440000.jpg" time="00:21:41" />
                  <image image="allframe/2015/11/06/m1-87212/m1-87212-08440700.jpg" time="00:21:48" />
                  <image type="LOGÓ" subtype="Műsorszámlogó" id="2964186" image="gallery/keyframe/2015/03/17/m1-76264/m1-76264-03302700.jpg">Summa</image>
                  <image type="SZEMÉLY / CSOPORT" subtype="Személy / Személyek" time="00:01:28" id="3180774" image="gallery/keyframe/2015/11/06/m1-87212/m1-87212-08234700.jpg">Rogán Antal, miniszter, Miniszterelnöki Kabinet Iroda</image>
                  <image type="SZEMÉLY / CSOPORT" subtype="Személy / Személyek" time="00:02:33" id="3180778" image="gallery/keyframe/2015/11/06/m1-87212/m1-87212-08245200.jpg">György László phd, közgazdász</image>
                  <image type="SZEMÉLY / CSOPORT" subtype="Személy / Személyek" time="00:03:03" id="3180782" image="gallery/keyframe/2015/11/06/m1-87212/m1-87212-08252200.jpg">Horváth Dániel, vezető kutató, Migrációkutató Intézet</image>
                  <image type="SZEMÉLY / CSOPORT" subtype="Személy / Személyek" time="00:03:45" id="3180788" image="gallery/keyframe/2015/11/06/m1-87212/m1-87212-08260400.jpg">Lattmann Tamás, nemzetközi jogász</image>
                  <image type="INFORMÁCIÓS TARTALOM" subtype="Téma / Felirat / Inzert" time="00:07:15" id="3180790" image="gallery/keyframe/2015/11/06/m1-87212/m1-87212-08293400.jpg">Amerikai-magyar kereskedelmi kapcsolatok, 1989 óta</image>
                  <image type="SZEMÉLY / CSOPORT" subtype="Személy / Személyek" time="00:07:28" id="3180796" image="gallery/keyframe/2015/11/06/m1-87212/m1-87212-08294700.jpg">Kerekes György, igazgató, Nemzetközi tanácsadás, Századvég Zrt.</image>
                  <image type="SZEMÉLY / CSOPORT" subtype="Személy / Személyek" time="00:08:53" id="3180848" image="gallery/keyframe/2015/11/06/m1-87212/m1-87212-08311200.jpg">Colleen Bell, az USA magyarországi nagykövete</image>
                  <image type="SZEMÉLY / CSOPORT" subtype="Személy / Személyek" time="00:12:13" id="3180856" image="gallery/keyframe/2015/11/06/m1-87212/m1-87212-08343200.jpg">Mészáros László, elnök, Országos Magyar Méhészeti Egyesület</image>
                  <image type="SZEMÉLY / CSOPORT" subtype="Személy / Személyek" time="00:12:59" id="3180858" image="gallery/keyframe/2015/11/06/m1-87212/m1-87212-08351800.jpg">Kutasi Tamás, méhészmester</image>
                  <image type="SZEMÉLY / CSOPORT" subtype="Személy / Személyek" time="00:14:05" id="3180862" image="gallery/keyframe/2015/11/06/m1-87212/m1-87212-08362400.jpg">Petneházy Antal, méhész</image>
                  <image type="SZEMÉLY / CSOPORT" subtype="Személy / Személyek" time="00:14:50" id="3180864" image="gallery/keyframe/2015/11/06/m1-87212/m1-87212-08370900.jpg">Jandácsik Attila, Pest Megyei Méhészeti Szaktanácsadó</image>
                  <relation type="tartalmazza">2358946</relation>
                  <media format="REAL" bitrate="128" resolution="192x144" clipBegin="08:22:19" clipEnd="08:44:07" media="2015/11/06/m1-20151106-055500.rm" />
                  <media format="REAL" bitrate="512" resolution="384x288" clipBegin="01:16:06" clipEnd="01:37:54" media="2015/11/06/m1-20151106-055500-07061300-12035300.rm" />
                  <media format="MPG" bitrate="8000" resolution="704x576" clipBegin="01:16:06" clipEnd="01:37:54" media="2015/11/06/m1-87212-07061300-12035300.mpg" />
                  <media format="MP4" bitrate="768" resolution="352x288" clipBegin="01:16:06" clipEnd="01:37:54" media="Broadcast/2015/11/06/m1-20151106-055500-07061300-12035300.mp4" />
                  <track type="subtitle" format="srt" language="hun" file="2015/11/06/m1-20151106-055500-07061300-12035300.srt" />
                  <person type="contributor" id="103356" role="hírigazgató" sex="Férfi" comment="szerkesztőségvezető, MTV">Németh Zsolt</person>
                  <person type="contributor" id="133458" role="produkciós vezető" sex="Férfi" comment="produkciós vezető">Tóth István</person>
                  <person type="contributor" id="148662" role="kreatív producer" sex="Férfi" comment="kreatív producer">Pető Zoltán</person>
                  <subject source="Földrajzi név" id="30">Európai Unió</subject>
                  <subject source="Földrajzi név" id="1403">Egyesült Államok</subject>
                  <subject source="Tezaurusz" id="4905">méhészet</subject>
                  <subject source="Tezaurusz" id="4906">méz</subject>
                  <subject source="Tezaurusz" id="4942">bevándorlás</subject>
                  <subject source="Tezaurusz" id="8945">migráció</subject>
                  <subject source="Tezaurusz" id="11161">menekült</subject>
                  <workflow date="2015-11-09" time="07:48:03" user="265" name="mate">Létrehozás</workflow>
                  <workflow date="2015-11-16" time="10:17:52" user="488" name="vera">Feladat kiosztása</workflow>
                  <workflow date="2015-11-19" time="08:51:52" user="488" name="vera">Feldolgozás vége</workflow>
               </document:meta>
            </navadoc:document>
         </metadata>
      </record>    </GetRecord> </OAI-PMH>

I want to get all record from where type="SZEMÉLY / CSOPORT"

I dont know if the php simplexml is enougth for this, or i have to use some other object.

  • 写回答

1条回答 默认 最新

  • doulang5323 2015-11-19 18:29
    关注

    I think that you can use simplexml_load_file and then use xpath. For example, lets assume your xml is in 'temp.xml'.

    $xml = simplexml_load_file("temp.xml");
    $xml->registerXPathNamespace('e', 'http://www.openarchives.org/OAI/2.0/');
    

    This will find your 10 images from the xml:

    $images = $xml->xpath('//e:image[@type="SZEMÉLY / CSOPORT"]');
    

    Now you can loop the $images, and each $image is of type SimpleXMLElement. Then you can get the value and the values of the properties for example:

    foreach ($images as $image) {
        $content =  $image->__toString();
        $type =  $image->attributes()->type;
        $subtype =  $image->attributes()->subtype;
        $time = $image->attributes()->time;
        $attributes = $image->attributes()->id;
        $image =  $image->attributes()->image;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?