我有以下 XML,我需要从中解析值。
<ads>
<ad id="987654321">
<price>
<currency-iso-code>
<value localized-label="£">GBP</value>
</currency-iso-code>
<amount>345</amount>
</price>
<price_frequency>
<value>WEEKLY</value>
</price_frequency>
<title>This is the Title</title>
<description>
Description
</description>
<ad_status>
<value>ACTIVE</value>
</ad_status>
<email>EXIST</email>
<user-id>123456</user-id>
<phone>123456</phone>
<modification-date-time>2013-09-02T11:40:41.000+01:00</modification-date-time>
<start-date-time>2013-09-02T11:40:39.000+01:00</start-date-time>
<features_active>
<category id="3">
<id-name>category-name</id-name>
<localized-name>Category name</localized-name>
</category>
<locations>
<location id="10000392">
<id-name>uk</id-name>
<localized-name>United Kingdom</localized-name>
</location>
</locations>
<neighborhood>Neighbourhood Name</neighborhood>
<attributes>
<attribute localized-label="Seller type" type="ENUM" name="seller_type">
<value localized-label="Agency">trade</value>
</attribute>
<attribute localized-label="Property type" type="ENUM" name="property_type">
<value localized-label="Flat">flat</value>
</attribute>
<attribute localized-label="Number of beds" type="LONG" name="property_number_beds">
<value>1</value>
</attribute>
<attribute localized-label="Date available" type="DATETIME" name="available_date">
<value localized-label="15/05/2013">2013-05-15T00:00:00.000+01:00</value>
</attribute>
</attributes>
<link rel="self" href="https://api2.domain.com/api/ads/987654321">
<link rel="self-user" href="https://api2.domain.com/api/users/123456/ads/987654321">
<public_link href="http://domain.com/public_facing_link">
<pictures>
<picture>
<link rel="extrabig" href="http://domain.com/images/80.JPG">
<link rel="preview" href="http://domain.com/images/81.JPG">
<link rel="big" href="http://domain.com/images/79.JPG">
<link rel="thumb" href="http://domain.com/images/78.JPG">
<link rel="moreadsthumb" href="http://domain.com/images/77.JPG">
</picture>
<picture>
<link rel="extrabig" href="http://domain.com/images/80.JPG">
<link rel="preview" href="http://domain.com/images/81.JPG">
<link rel="big" href="http://domain.com/images/79.JPG">
<link rel="thumb" href="http://domain.com/images/78.JPG">
<link rel="moreadsthumb" href="http://domain.com/images/77.JPG">
</picture>
</pictures>
</public_link>
</features_active>
</ad>
</ads>
我需要从这个 xml 中提取以下数据
标题、描述 - 很容易拉。完成它们
我还需要 public_link href 和带有 rel="thumb"的图片 href 链接
通过查看此处的大量帖子和 php 文档,我得出了类似的结论。
$ads = simplexml_load_string($xml);
foreach ($ads as $ad) {
$item['price'] = $ad->price->amount;
$item['title'] = $ad->title;
$link = simplexml_load_string($ad->features_active->public_link);
foreach($link->attributes() as $attr => $value) {
if($attr == 'href'){
$item['link'] = $value;
}
}
$pics = simplexml_load_string($ad->features_active->public_link->pictures);
foreach($pics->picture[0]->attributes() as $attr => $value) {
if($attr == 'thumb'){
$item['picture'] = $value;
}
}
echo "<br><br>" . $item['price'];
echo "<br>" . $item['title'];
echo "<br>" . $item['link'];
echo "<br>" . $item['picture'];
}
出于某种原因,它不想提取属性。谁能为此指出正确的方向?
最佳答案
为 XML 解析器尝试 PHP DOMDocument。请引用以下 URL 获取标签和属性值。
http://www.php.net/manual/en/domdocument.getelementsbytagname.php
$objDOM = new DOMDocument('1.0');
$objDOM->preserveWhiteSpace = false;
$objDOM->loadXML($xml);
关于php - XMLA解析获取多个属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18571609/
我有一个字符串input="maybe(thisis|thatwas)some((nice|ugly)(day|night)|(strange(weather|time)))"Ruby中解析该字符串的最佳方法是什么?我的意思是脚本应该能够像这样构建句子:maybethisissomeuglynightmaybethatwassomenicenightmaybethiswassomestrangetime等等,你明白了......我应该一个字符一个字符地读取字符串并构建一个带有堆栈的状态机来存储括号值以供以后计算,还是有更好的方法?也许为此目的准备了一个开箱即用的库?
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我正在使用ruby1.9解析以下带有MacRoman字符的csv文件#encoding:ISO-8859-1#csv_parse.csvName,main-dialogue"Marceu","Giveittohimóhe,hiswife."我做了以下解析。require'csv'input_string=File.read("../csv_parse.rb").force_encoding("ISO-8859-1").encode("UTF-8")#=>"Name,main-dialogue\r\n\"Marceu\",\"Giveittohim\x97he,hiswife.\"\
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2
我正在尝试修改当前依赖于定义为activeresource的gem:s.add_dependency"activeresource","~>3.0"为了让gem与Rails4一起工作,我需要扩展依赖关系以与activeresource的版本3或4一起工作。我不想简单地添加以下内容,因为它可能会在以后引起问题:s.add_dependency"activeresource",">=3.0"有没有办法指定可接受版本的列表?~>3.0还是~>4.0? 最佳答案 根据thedocumentation,如果你想要3到4之间的所有版本,你可以这