我有一个XML文档,如下所示:
<Data
xmlns="http://www.domain.com/schema/data"
xmlns:dmd="http://www.domain.com/schema/data-metadata"
>
<Something>...</Something>
</Data>
Run Code Online (Sandbox Code Playgroud)
我正在使用PHP中的SimpleXML解析信息.我正在处理数组,我似乎遇到了命名空间的问题.
我的问题是:如何删除这些名称空间?我从XML文件中读取数据.
谢谢!
我正在阅读的XML看起来像这样:
<show id="8511">
<name>The Big Bang Theory</name>
<link>http://www.tvrage.com/The_Big_Bang_Theory</link>
<started>2007-09-24</started>
<country>USA</country>
<latestepisode>
<number>05x23</number>
<title>The Launch Acceleration</title>
</latestepisode>
</show>
Run Code Online (Sandbox Code Playgroud)
要获得(例如)最新一集的数量,我会这样做:
$ep = $xml->latestepisode[0]->number;
Run Code Online (Sandbox Code Playgroud)
这很好用.但是如何从中获取ID <show id="8511">呢?
我尝试过类似的东西:
$id = $xml->show;
$id = $xml->show[0];
Run Code Online (Sandbox Code Playgroud)
但都没有效果.
更新
我的代码片段:
$url = "http://services.tvrage.com/feeds/episodeinfo.php?show=".$showName;
$result = file_get_contents($url);
$xml = new SimpleXMLElement($result);
//still doesnt work
$id = $xml->show->attributes()->id;
$ep = $xml->latestepisode[0]->number;
echo ($id);
Run Code Online (Sandbox Code Playgroud)
大利.XML:
http://services.tvrage.com/feeds/episodeinfo.php?show=The.Big.Bang.Theory
Run Code Online (Sandbox Code Playgroud) 这个:
$XML = new SimpleXMLElement("<foo />");
echo($XML->asXML());
Run Code Online (Sandbox Code Playgroud)
...输出:
<?xml version="1.0"?>
<foo/>
Run Code Online (Sandbox Code Playgroud)
但我也希望它输出编码:
<?xml version="1.0" encoding="UTF-8"?>
<foo/>
Run Code Online (Sandbox Code Playgroud)
有没有办法告诉SimpleXMLElement包含<?xml?>标签的编码属性?除此之外:
$XML = new SimpleXMLElement("<?xml version='1.0' encoding='utf-8'?><foo />");
echo($XML->asXML());
Run Code Online (Sandbox Code Playgroud)
哪个有效,但是必须手动指定版本和编码很烦人.
假设为了这个问题的目的,我不能使用DOMDocument.
我正在尝试解决如何迭代返回的SimpleXML对象.
我正在使用一个名为Tarzan AWS的工具包,它连接到Amazon Web Services(SimpleDB,S3,EC2等).我特意使用SimpleDB.
我可以将数据放入Amazon SimpleDB服务,我可以将其恢复.我只是不知道如何处理返回的SimpleXML对象.
Tarzan AWS文档说明了这一点:
查看响应以浏览响应的标题和正文.请注意,这是一个对象,而不是一个数组,并且正文是一个SimpleXML对象.
这是返回的SimpleXML对象的示例:
[body] => SimpleXMLElement Object
(
[QueryWithAttributesResult] => SimpleXMLElement Object
(
[Item] => Array
(
[0] => SimpleXMLElement Object
(
[Name] => message12413344443260
[Attribute] => Array
(
[0] => SimpleXMLElement Object
(
[Name] => active
[Value] => 1
)
[1] => SimpleXMLElement Object
(
[Name] => user
[Value] => john
)
[2] => SimpleXMLElement Object
(
[Name] => message
[Value] => This is a message.
)
[3] = … 我正在尝试从Flickr读取RSS提要,但它有一些不能被Simple XML(media:thumbnail,flickr:profile等等)读取的节点.
我怎么绕这个?当我查看DOM的文档时,我的头疼.所以我想避免它,因为我不想学习.
顺便说一句,我正试图获取缩略图.
我正在尝试从此youtube播放列表供稿中获取视频数据,并将有趣的数据添加到数组中并稍后使用,但正如您从Feed中看到的,某些视频链接已"死",这会导致我的代码出现问题.
当我尝试访问$ attrs ['url']时,我得到的错误是"节点不再存在".在我访问它之前,我已经尝试了几个小时找到检查节点是否存在的方法,但我没有运气.
如果有人可以帮助我以相同的结果以其他方式解析feed,或者创建一个if-node-exists检查,我将非常高兴.先感谢您
$url = 'http://gdata.youtube.com/feeds/api/playlists/18A7E36C33EF4B5D?v=2';
$sxml = simplexml_load_file($url);
$i = 0;
$videoobj;
foreach ($sxml->entry as $entry) {
// get nodes in media: namespace for media information
$media = $entry->children('http://search.yahoo.com/mrss/');
// get video player URL
$attrs = $media->group->player->attributes();
$videoobj[$i]['url'] = $attrs['url'];
// get video thumbnail
$attrs = $media->group->thumbnail[0]->attributes();
$videoobj[$i]['thumb'] = $attrs['url'];
$videoobj[$i]['title'] = $media->group->title;
$i++;
}
Run Code Online (Sandbox Code Playgroud) 我想从表中获取数据而不使用正则表达式.我很高兴使用simplexml来解析RSS提要,并想知道它是否可以用来从另一个页面中获取表格.
例如.用curl抓取页面或只是file_get_contents(); 然后使用simplexml来获取内容?
请考虑以下代码:
$string = '<device>
<id>1234</id>
<label>118</label>
<username>root</username>
<password>helloWorld</password>
<hardware>
<memory>4GB RAM</memory>
<storage_drives>
<storage_drive_1>2TB SATA 7,200RPM</storage_drive_1>
<storage_drive_2>1TB SATA 7,200RPM</storage_drive_2>
<storage_drive_3>Not Applicable</storage_drive_3>
<storage_drive_4>Not Applicable</storage_drive_4>
</storage_drives>
</hardware>
</device>';
$xml = new SimpleXMLElement($string);
$deviceDetails = Array();
foreach($xml as $element){
$tag = $element->getName();
$deviceDetails += Array($tag => '$element->$tag)',
);
}
Run Code Online (Sandbox Code Playgroud)
输出$detailsDetails数组如下:
Array
(
[id] => $element->$tag)
[label] => $element->$tag)
[username] => $element->$tag)
[password] => $element->$tag)
[hardware] => $element->$tag)
)
Run Code Online (Sandbox Code Playgroud)
这是错的.
我的问题是,如何$element->$tag开展工作?
我正在尝试使用SimpleXML加载远程URL.
如果我在浏览器中键入以下内容;
http://api.ean.com/ean-services/rs/hotel/v3/list?cid=55505&minorRev=12&apiKey=2hkhej72gxyas3ky6hhjtsga&locale=en_US¤cyCode=USD&customerIpAddress=10.184.2.9&customerUserAgent=Mozilla/5.0+(Windows;+U;+Windows+NT+6.1;+en-US;+rv:1.9.2.25)+Gecko/20111212+Firefox/3.6.25&customerSessionId=&xml=<HotelListRequest><arrivalDate>01/22/2012</arrivalDate><departureDate>01/24/2012</departureDate><RoomGroup><Room><numberOfAdults>1</numberOfAdults><numberOfChildren>1</numberOfChildren><childAges>4</childAges></Room></RoomGroup><city>Amsterdam</city><countryCode>NL</countryCode><supplierCacheTolerance>MED</supplierCacheTolerance></HotelListRequest>
Run Code Online (Sandbox Code Playgroud)
有用!
如果我试试;
$url = 'http://api.ean.com/ean-services/rs/hotel/v3/list?cid=55505&minorRev=12&apiKey=2hkhej72gxyas3ky6hhjtsga&locale=en_US¤cyCode=USD&customerIpAddress=10.184.2.9&customerUserAgent=Mozilla/5.0+(Windows;+U;+Windows+NT+6.1;+en-US;+rv:1.9.2.25)+Gecko/20111212+Firefox/3.6.25&customerSessionId=&xml=<HotelListRequest><arrivalDate>01/22/2012</arrivalDate><departureDate>01/24/2012</departureDate><RoomGroup><Room><numberOfAdults>1</numberOfAdults><numberOfChildren>1</numberOfChildren><childAges>4</childAges></Room></RoomGroup><city>Amsterdam</city><countryCode>NL</countryCode><supplierCacheTolerance>MED</supplierCacheTolerance></HotelListRequest> ';
$xml = simplexml_load_file($url);
Run Code Online (Sandbox Code Playgroud)
我遇到了以下错误;
Warning: simplexml_load_file() [function.simplexml-load-file]: http://api.ean.com/ean-services/rs/hotel/v3/list?cid=55505&minorRev=12&apiKey=2hkhej72gxyas3ky6hhjtsga&locale=en_US¤cyCode=USD&customerIpAddress=10.184.2.9&customerUserAgent=Mozilla/5.0%2B(Windows;%2BU;%2BWindows%2BNT%2B6.1;%2Ben-US;%2Brv:1.9.2.25)%2BGecko/20111212%2BFirefox/3.6.25&customerSessionId=&xml=%3CHotelListRequest%3E%3CarrivalDate%3E01/22/2012%3C/arrivalDate%3E%3CdepartureDate%3E01/24/2012%3C/departureDate%3E%3CRoomGroup%3E%3CRoom%3E%3CnumberOfAdults%3E1%3C/numberOfAdults%3E%3CnumberOfChildren%3E1%3C/numberOfChildren%3E%3CchildAges%3E4%3C/childAges%3E%3C/Room%3E%3C/RoomGroup%3E%3Ccity%3EAmsterdam%3C/city%3E%3CcountryCode%3ENL%3C/countryCode%3E%3CsupplierCacheTolerance%3EMED%3C/supplierCacheTolerance%3E%3C/HotelListRequest%3E%20:1: parser error : Start tag expected, '<' not found in C:\Program Files\XAMPP\xampplite\htdocs\hotel\results.php on line 29
Run Code Online (Sandbox Code Playgroud)
该错误最值得注意的部分是:解析器错误:期望开始标记,找不到"<"
我想访问该文件并使用PHP/SimpleXML格式化结果 - 但到目前为止我被卡住了.
我认为,这个线程HERE其中谈到rawurlencode可能有助于但事与愿违,要么或我失去了一些东西.
我正在亚马逊联盟wordpress页面上工作.为此,我使用aws_signed_request函数来获取亚马逊的价格和链接.
这是返回xml的aws_signed_request函数:
function aws_signed_request($region, $params, $public_key, $private_key, $associate_tag) {
$method = "GET";
$host = "ecs.amazonaws.".$region;
$uri = "/onca/xml";
$params["Service"] = "AWSECommerceService";
$params["AWSAccessKeyId"] = $public_key;
$params["AssociateTag"] = $associate_tag;
$params["Timestamp"] = gmdate("Y-m-d\TH:i:s\Z");
$params["Version"] = "2009-03-31";
ksort($params);
$canonicalized_query = array();
foreach ($params as $param=>$value)
{
$param = str_replace("%7E", "~", rawurlencode($param));
$value = str_replace("%7E", "~", rawurlencode($value));
$canonicalized_query[] = $param."=".$value;
}
$canonicalized_query = implode("&", $canonicalized_query);
$string_to_sign = $method."\n".$host."\n".$uri."\n".
$canonicalized_query;
/* calculate the signature using HMAC, SHA256 and base64-encoding */
$signature = base64_encode(hash_hmac("sha256",
$string_to_sign, $private_key, True)); …Run Code Online (Sandbox Code Playgroud)