如何使用命名空间解析

如何使用命名空间解析

本文介绍了如何使用命名空间解析

问题描述

这是我尝试使用 PHP simple</description> 标记并且不返回其余的

<media:hash algo="md5">0AF4AD25977BF50888075D3547F67173</media:hash><pl: added>星期日,2013 年 12 月 22 日 02:41:46 GMT</pl: added><plfile:assetType>mpeg4</plfile:assetType></media:content><media:thumbnail expression="full" fileSize="79478" type="image/jpeg" height="556" isDefault="false" url="http://pmdvod.nationalgeographic.com/NG_Video/997/323/67322_1_1280x720_990x556_177596483623.jpg" width="990"><media:hash algo="md5">60FFCE1C849440050CBCBD259EF2CE65</media:hash><pl: added>星期日,2014 年 3 月 2 日 05:26:41 GMT</pl: added><plfile:assetType>海报</plfile:assetType><plfile:assetType>MED</plfile:assetType></media:thumbnail><media:thumbnail expression="full" fileSize="209762" type="image/jpeg" height="720" isDefault="false" url="http://pmdvod.nationalgeographic.com/NG_Video/997/323/67322_1_1280x720.jpg" width="1280"><media:hash algo="md5">08AC83096DC5F7E48A7CDE4BFA23A27A</media:hash><pl: added>星期日,2014 年 3 月 2 日 05:26:30 GMT</pl: added><plfile:assetType>夹层图像</plfile:assetType></media:thumbnail><media:thumbnail expression="full" fileSize="2962" type="image/jpeg" height="68" isDefault="false" url="http://pmdvod.nationalgeographic.com/NG_Video/997/323/67322_1_1280x720_120x68_177597507564.jpg" width="120"><media:hash algo="md5">605D86F9A2B606D97A653576E6CE99D2</media:hash><pl: added>星期日,2014 年 3 月 2 日 05:26:45 GMT</pl: added><plfile:assetType>缩略图</plfile:assetType></media:thumbnail><media:thumbnail expression="full" fileSize="23707" type="image/jpeg" height="270" isDefault="false" url="http://pmdvod.nationalgeographic.com/NG_Video/997/323/67322_1_1280x720_480x270_177598019578.jpg" width="480"><media:hash algo="md5">73F78D0314E0CC15CDA5C36AAE4560AD</media:hash><pl: added>星期日,2014 年 3 月 2 日 05:26:42 GMT</pl: added><plfile:assetType>海报</plfile:assetType><plfile:assetType>X-SM</plfile:assetType></media:thumbnail><media:thumbnail expression="full" fileSize="83905" type="image/jpeg" height="576" isDefault="false" url="http://pmdvod.nationalgeographic.com/NG_Video/997/323/67322_1_1280x720_1024x576_177598019579.jpg" width="1024"><media:hash algo="md5">5C4B05CE487697A63F4FEC8B51009497</media:hash><pl:已添加>星期日,2014 年 3 月 2 日 05:26:43 GMT</pl:已添加><plfile:assetType>海报</plfile:assetType><plfile:assetType>LG</plfile:assetType></media:thumbnail><media:thumbnail expression="full" fileSize="114846" type="image/jpeg" height="720" isDefault="false" url="http://pmdvod.nationalgeographic.com/NG_Video/997/323/67322_1_1280x720_1280x720_177598019580.jpg" width="1280"><media:hash algo="md5">31BFBD1020B2277B7E6FDD353973F263</media:hash><pl: added>Sun, 02 Mar 2014 05:26:43 GMT</pl: added><plfile:assetType>海报</plfile:assetType><plfile:assetType>X-LG</plfile:assetType></media:thumbnail><media:thumbnail expression="full" fileSize="37132" type="image/jpeg" height="360" isDefault="true" url="http://pmdvod.nationalgeographic.com/NG_Video/997/323/67322_1_1280x720_640x360_177598019582.jpg" width="640"><media:hash algo="md5">2654AFBE193785E29FF0533CF977A473</media:hash><pl: added>星期日,2014 年 3 月 2 日 05:26:44 GMT</pl: added><plfile:assetType>海报</plfile:assetType><plfile:assetType>SM</plfile:assetType></media:thumbnail><pubDate>星期六,2013 年 12 月 21 日 06:56:49 GMT</pubDate><plmedia:defaultThumbnailUrl>http://pmdvod.nationalgeographic.com/NG_Video/997/323/67322_1_1280x720_640x360_177598019582.jpg</plmedia:defaultThumbnailUrl</plmedia:defaultThumbnailUrl<<plmedia:provider/><pl1:youTubeInStreamStandard>true</pl1:youTubeInStreamStandard><pl1:youTubeInStreamTrueView>true</pl1:youTubeInStreamTrueView><pl1:youTubeInVideo>true</pl1:youTubeInVideo><pl1:youTubePublic>false</pl1:youTubePublic><ngs:clipTypeOld>none</ngs:clipTypeOld><ngs:contentCreationDate>2013 年 5 月 13 日星期一 19:03:22 GMT</ngs:contentCreationDate><ngs:network>NGS</ngs:network><ngs:redirectToExternal>从不</ngs:redirectToExternal><ngs:requiresAuth>false</ngs:requiresAuth><ngs:series>{"updated_datetime":null,"creation_datetime":null,"is_active":false,"name":"National Geographic Live","translated_names":[],"synonyms":[],"uuid":null,"notes":null,"objectType":"org.ngs.video.Show","groups":[],"url":null}</ngs:series><ngs:seriesName>国家地理直播</ngs:seriesName><ngsTax:location>{"updated_datetime":"2014-04-03T19:09:42.570Z","is_active":true,"creation_datetime":"2013-11-28T20:27:32.077Z","parent":"http://taxonomy.nationalgeographic.com/api/v1/locations/eea9ad49-32fc-3a7c-bf15-cfeabdaa2fb7","geonames_uri":null,"url":"http://taxonomy.nationalgeographic.com/api/v1/locations/ff4e742f-46e6-3867-be8c-003a7af8f86c","dbpedia_uri":"http://dbpedia.org/page/Arctic","name":"北极","经度":0.0,"层次结构":"地球/北极/","纬度":0.0,"uuid":"ff4e742f-46e6-3867-be8c-003a7af8f86c","notes":"","objectType":"org.ngs.taxonomy.GeoLocation","groups":["Place","re​​gion"],"english_name":null,"parentName":"Earth"}</ngsTax:location><ngsTax:location>{"updated_datetime":"2014-05-28T20:43:43.719Z","is_active":true,"creation_datetime":"2013-11-28T20:01:26.936Z","synonyms":[null,null],"parent":"http://taxonomy.nationalgeographic.com/api/v1/locations/eea9ad49-32fc-3a7c-bf15-cfeabdaa2fb7","geonames_uri":"http://sws.geonames.org/6697173/","url":"http://taxonomy.nationalgeographic.com/api/v1/locations/d2ce52aa-c6ac-3a13-9f8c-59cb13ecb924","dbpedia_uri":null,"name":"Antarctica","translated_names":[null],"longitude":16.40626,"hierarchy":"earth/antarctica/","latitude":-78.15856,"uuid":"d2ce52aa-c6ac-3a13-9f8c-59cb","notes":"","objectType":"org.ngs.taxonomy.GeoLocation","groups":["地方","大陆"],"english_name":null,"parentName":"地球"}</ngsTax:location><ngsTax:organization>{"updated_datetime":"2014-05-02T15:37:25.776Z","creation_datetime":"2013-11-28T19:40:52.694Z","is_active":true,"name":"National Geographic Live","synonyms":[null],"uuid":"bae34d69-0601-342b-abe4-345c4c2486c4","notes":"","objectType":"org.ngs.taxonomy.Organization","english_name":null,"groups":["Organization"],"url":"http://taxonomy.nationalgeographic.com/api/v1/organizations/bae34d69-0601-342b-abe4-345c4c2486c4"}</ngsTax:organization><ngsTax:slug>ousland-sheikh-bonus-nglive</ngsTax:slug><ngsTax:subject>{"updated_datetime":"2014-06-04T15:56:21.047Z","definition":"","is_active":true,"creation_datetime":"2013-11-28T19:59:40.956Z","scope_note":"","synonyms":[null],"parent":"http://taxonomy.nationalgeographic.com/api/v1/subjects/83bf00d5-93bd-3cca-acc3-543652f06d52","url":"http://taxonomy.nationalgeographic.com/api/v1/subjects/58f9272e-f37b-3550-a228-a5ee119553ae","name":"Explorers","hierarchy":"exploration/explorers/","uuid":"58f9272e-f37b-3550-a228-a5ee119553ae","notes":"","objectType":"org.ngs.taxonomy.Subject","groups":["Subject"],"english_name":null,"parentName":"Exploration"}</ngsTax:subject><ngsTax:subject>{"updated_datetime":"2014-01-29T14:56:08.338Z","definition":"","is_active":true,"creation_datetime":"2013-11-28T19:52:45.402Z","scope_note":"","parent":"http://taxonomy.nationalgeographic.com/api/v1/subjects/83bf00d5-93bd-3cca-acc3-543652f06d52","url":"http://taxonomy.nationalgeographic.com/api/v1/subjects/83c5f238-8dbc-373e-9d62-4deb4c59e122","name":"Expeditions","hierarchy":"exploration/expeditions/","uuid":"83c5f238-8dbc-373e-9d62-4deb4c59e122","notes":"","objectType":"org.ngs.taxonomy.Subject","groups":["Subject"],"english_name":null,"parentName":"探索"}</ngsTax:subject><ngsTax:subject>{"updated_datetime":"2014-02-10T19:47:37.384Z","definition":"","is_active":true,"creation_datetime":"2014-01-16T17:48:21.375Z","scope_note":"","synonyms":[null,null,null],"parent":"http://taxonomy.nationalgeographic.com/api/v1/subjects/a817863e-de0f-3ffd-944d-c75ed20308ff","url":"http://taxonomy.nationalgeographic.com/api/v1/subjects/f0c33fd8-5e31-36b2-9229-b71628df3367","name":"Travel","hierarchy":"旅行和冒险/旅行/","uuid":"f0c33fd8-5e31-36b2-9229-b71628df3367","notes":"","objectType":"org.ngs.taxonomy.Subject","groups":["Subject"],"english_name":null,"parentName":"Travel and Adventure"}</ngsTax:subject><ngsTax:title>NG Live!:北极酋长</ngsTax:title><wpf:allTaxonomyId>18160</wpf:allTaxonomyId><wpf:allTaxonomyId>1589</wpf:allTaxonomyId><wpf:allTaxonomyId>858</wpf:allTaxonomyId><wpf:altLinkedLegacyUrl/><wpf:作者/><wpf:firstPublishedTime>2013 年 5 月 13 日星期一 19:11:17 GMT</wpf:firstPublishedTime><wpf:id>13615</wpf:id><wpf:lastPublishedTime>2014 年 2 月 3 日星期一 21:50:20 GMT</wpf:lastPublishedTime><wpf:lineupName>Nat Geo Live 2</wpf:lineupName><wpf:locationMigration>北极</wpf:locationMigration><wpf:locationMigration>南极</wpf:locationMigration><wpf:metaKeyword>Nat Geo Live</wpf:metaKeyword><wpf:metaKeyword>Nat Geo Live!</wpf:metaKeyword><wpf:metaKeyword>讲座</wpf:metaKeyword><wpf:metaKeyword>国家地理直播</wpf:metaKeyword><wpf:metaKeyword>国家地理直播!</wpf:metaKeyword><wpf:metaKeyword>NGLive</wpf:metaKeyword><wpf:metaKeyword>NG直播</wpf:metaKeyword><wpf:metaKeyword>Nat Geo Events</wpf:metaKeyword><wpf:metaKeyword>Børge Ousland</wpf:metaKeyword><wpf:metaKeyword>polar</wpf:metaKeyword><wpf:metaKeyword>冒险</wpf:metaKeyword><wpf:metaKeyword>探索</wpf:metaKeyword><wpf:metaKeyword>探索</wpf:metaKeyword><wpf:metaKeyword>远征</wpf:metaKeyword><wpf:metaKeyword>滑雪</wpf:metaKeyword><wpf:metaKeyword>北极</wpf:metaKeyword><wpf:metaKeyword>孤独</wpf:metaKeyword><wpf:metaKeyword>奇怪的</wpf:metaKeyword><wpf:metaKeyword>访问者</wpf:metaKeyword><wpf:metaKeyword>ice</wpf:metaKeyword><wpf:metaKeyword>照片</wpf:metaKeyword><wpf:metaKeywords>Nat Geo Live,Nat Geo Live!,讲座,国家地理直播,国家地理直播!,NGLive,NG Live,Nat Geo Events,Børge Ousland,极地,冒险,探索,探索,远征,滑雪,北极,孤独,陌生,访客,冰,照片</wpf:metaKeywords><wpf:organizationMigration>国家地理直播</wpf:organizationMigration><wpf:sitePath>Specials Video::Nat Geo Live::Nat Geo Live 2</wpf:sitePath><wpf:siteUrlPath>/video/specials/nat-geo-live-specials/nat-geo-live-2/ousland-sheikh-bonus-nglive/</wpf:siteUrlPath><wpf:subjectMigration>Explorer</wpf:subjectMigration><wpf:subjectMigration>Expeditions</wpf:subjectMigration><wpf:subjectMigration>旅行</wpf:subjectMigration><wpf:taxonomyId>18160</wpf:taxonomyId><ytcp:youTubeAssetType>web</ytcp:youTubeAssetType><ytcp:youTubeSavedMatchRights>在所有国家/地区获利</ytcp:youTubeSavedMatchRights><ytcp:youTubeSavedUsageRights>在所有国家/地区获利</ytcp:youTubeSavedUsageRights></项目></频道></rss>

这是我的代码

 函数 parse
解决方案

您不会得到所有节点,因为它们使用命名空间,例如:pl:id.要解析这些,您可以使用 Xpath.第一个示例获取所有 pl: added 第二个获取 media:hash.

如果您知道文件将始终使用相同的前缀,则只能使用以下内容.如果没有,您必须为您使用的每个 simple

xpath('//pl: added');}//输出$已添加echo "

";var_dump($add);//获取 media:hashforeach($xpath('//media:thumbnail') as $element) {$hash = $element->xpath('//media:hash');}//输出$hashecho "

";var_dump($hash);?>

当您需要注册命名空间时,您可以执行以下操作.首先,您可以对命名空间进行 var dump,以便您知道需要使用哪个 url.

["os"]=>string(36) "http://a9.com/-/spec/opensearch/1.1/" 所以对于 os,你需要那个 url.>

";//var_dump($namespaces);$registerXPathNamespace('media', 'http://search.yahoo.com/mrss/');foreach($xpath('//media:content') as $element) {$element->registerXPathNamespace('pl', 'http://xpath('//pl: added');}echo "

";var_dump($add);$registerXPathNamespace('media', 'http://search.yahoo.com/mrss/');foreach($xpath('//media:thumbnail') as $element) {$element->registerXPathNamespace('media', 'http://search.yahoo.com/mrss/');$hash = $element->xpath('//media:hash');}echo "

";var_dump($hash);?>

This is my </description> tag and rest of the

<?

here is my code

   function parse
解决方案

You won't get all nodes because they use namespaces, for exmple: pl:id. To parse these you could use Xpath. first example grabs all pl:added second one gets media:hash.

You can only use the following if you know the file will always use the same prefix. If not, you have to register the namespace for each simple

<?
$str = '*your string here*';
$

When you need to register namespaces you can do the following. First you can var dump the namespaces so you know which url you need to use.

["os"]=>string(36) "http://a9.com/-/spec/opensearch/1.1/" So for os you need that url.

<?
$str = '*your string here*';
$

这篇关于如何使用命名空间解析

08-13 06:58