SimpleXMLElement Object
(
[@attributes] => Array
(
[domain] => http://www.eatingwell.com/category/publication/magazine /september/october_2009
)
[0] => September/October 2009
[1] => American
[2] => Easy
[3] => Diabetes appropriate
[4] => Healthy weight
[5] => High calcium
[6] => Low calorie
[7] => Low cholesterol
[8] => Bone Health
[9] => Super Bowl
[10] => Recipes & Menus - Fresh
[11] => Recipes & Menus - Vegetarian
[12] => Cheese
[13] => Dairy
[14] => Greens
[15] => Vegetables
[16] => Wheat
[17] => Whole Grains
[18] => Vegetarian, other
[19] => Appetizers
[20] => Dinner

[21] => Bake
[22] => Fall
[23] => Spring
[24] => Summer
[25] => Winter
[26] => Budget
[27] => Entertaining, casual
[28] => Everyday favorites
[29] => Quick (total 30 min. or less)
[30] => Vegetarian
[31] => Appetizer
[32] => Main dish, vegetarian
[33] => Pizza
)
我希望从rss Feed中检索该类别,但是$bullet =$item->category;pr($bullet);
它会显示上述结果。我只想要数组值[0]到[33]。如何从上面的结果中删除@attributes?
答案 0 :(得分:0)
我认为pr()
是一个仅仅因为某种原因而包裹print_r()
的函数,在这种情况下我会告诉你我用SimpleXML问题告诉每个人:不要' t trust print_r (或var_dump,或任何正常的调试输出函数)。
简单的事实是那些属性不存在。 SimpleXML使用迭代器,魔术方法和其他技巧来提供令人难以置信的方便"尽我所能"接口,但很难在调试输出中总结它。
如果要循环使用相同名称的所有元素,请编写foreach ( $item->category as $bullet )
,并且永远不会发现$bullet
被设置为任何属性。但是,它将被设置为每个节点的对象,因此,如果您正在执行比echo $bullet
更复杂的操作,则可能需要使用(string)$bullet
提取文本内容。
如果你想获得一个属性,你不会通过查看@attributes以任何形式找到它,而是通过使用数组式访问,例如(string)$bullet['domain']
。
将您的代码基于the examples in the manual,而不是调试输出,并且您将获得更远的距离:)
答案 1 :(得分:0)
尝试:
unset(xml->attributes()->domain);
答案 2 :(得分:-1)
根据IMSoP的观察,@属性并不是该对象的真实属性。但是,你可以"作弊"通过执行以下操作,只要您不再需要作为SimpleXMLElement对象与对象进行交互:
<?php
function purgeAttributes( $object ) {
foreach ( $object as $key => $value ) {
if ( gettype( $value ) == 'object' ) {
$object->$key = purgeAttributes( $value );
}
if ( $key == '@attributes' ) {
unset( $object->$key );
}
}
return $object;
}
$x = SimpleXML_load_string('<foo bar="baz" />');
$x = json_decode( json_encode( $x ) );
$x = purgeAttributes( $x );
var_dump( $x );
答案 3 :(得分:-1)
我这样做,似乎有效。编码&gt;解码应足以将SimpleXMLElement对象转换为StdClass对象并按您希望的方式运行:
public function cleanXML($ xmlString) {
foreach (DataGridViewRow sr in dataGridView1.SelectedRows)
{
if(i >= 10)
{
strs.Add(new List<string>(str));
str.Clear();
i = 0;
var a = strs; //Here strs have one str with 0 values
}
str.Add(sr.Cells["MOBILNI"].Value.ToString().Trim());
i++;
}
strs.Add(str);