【问题标题】:PHP Fetch all Attributes from xmlPHP从xml中获取所有属性
【发布时间】:2017-01-31 17:31:36
【问题描述】:

我使用 simplexml_load_string 解析了以下 xml:

<ItemAttributes>
                <Binding>Misc.</Binding>
                <Brand>Gotcha</Brand>
                <Feature>schöne Kontraststickerei</Feature>
                <Feature>langer und taillierter Schnitt</Feature>
                <Feature>kleine Kängurutasche</Feature>
                <Feature>Druckknopfleiste mit großen Metallknöpfen</Feature>
                <Feature>Rückseite figurbetonend gerafft</Feature>
                <ProductGroup>Sports</ProductGroup>
                <ProductTypeName>SPORTING_GOODS</ProductTypeName>
                <Title>Gotcha Damen Strickjacke</Title>
            </ItemAttributes>

并且想要获取数组键中的所有“功能”。其实我是这样做的:

$response = array(
            "details" => htmlentities((string) $item->ItemAttributes->Feature)
);  

这样我只获取第一个属性。 “schöne Kontraststickerei”。但我想在 $response['details'] 中包含所有属性。最好用逗号分隔。

【问题讨论】:

  • 显示如何加载 xml 内容
  • $response = array(); foreach ($item-&gt;children() as $node) { $response[] = $node-&gt;Feature; } print_r($response);

标签: php xml


【解决方案1】:

您可以将每个简单的 XML 对象转换为一个数组:

$response = array(
    "details" => htmlentities((string) implode(",",(array) $item->ItemAttributes->Feature))
); 

【讨论】:

  • 就是这样!非常感谢!
猜你喜欢
  • 1970-01-01
  • 2014-05-30
  • 2022-01-13
  • 1970-01-01
  • 2021-08-26
  • 2012-10-22
  • 1970-01-01
  • 1970-01-01
  • 2012-02-28
相关资源
最近更新 更多