如何更改simpleXML元素的属性?

时间:2011-09-08 00:20:58

标签: php xml simplexml

我用SimepleXML解析了一个XML文件:

<element data="abc,def">
 EL
</element>

但是现在我想在“数据”属性中添加一些内容。不是在文件中,而是在我的变量中(在我从simplexml_load_file获得的对象结构中)。

我该怎么做?

4 个答案:

答案 0 :(得分:10)

未经测试,但应该有效:

$element->attributes()->data = ((string) $element->attributes()->data) . ',ghi';

答案 1 :(得分:1)

嗯,可以像$element['data'] .= ',ghi';

那样完成

答案 2 :(得分:1)

使用此更正。...

add_filter( 'woocommerce_format_weight', 'custom_format_weight', 20, 2 );
function custom_format_weight( $weight_string, $weight ){
    // Format decimals from default weight value
    $weight_string  = wc_format_localized_decimal( $weight );
    // Format decimals from custom converted weight value
    $weight_string2 = wc_format_localized_decimal( round($weight * 0.45359237, 2) );

    if ( ! empty( $weight_string ) ) {
        $weight_string2 = ' ( ' . $weight_string2 . ' ' . __( 'kg', 'woocommerce' ) . ' )';
        $weight_string .= ' ' . get_option( 'woocommerce_weight_unit' ) . $weight_string2;
    } else {
        $weight_string = __( 'N/A', 'woocommerce' );
    }

    return $weight_string;
}

add_filter( 'woocommerce_format_dimensions', 'custom_format_dimensions', 10, 2 );
function custom_format_dimensions( $dimension_string, $dimensions ){
    // Initializing variable
    $dimentions2 = array();

    // Loop though dimensions array (and set custom converted formatted decimals values in a new array)
    foreach( $dimensions as $key => $value ){
        if( ! empty($value) && $value != 0 )
            $dimentions2[$key] = wc_format_localized_decimal( round($value * 2.54, 2) );
    }

    // Format default dimentions in a string
    $dimension_string  = implode( ' x ', array_filter( array_map( 'wc_format_localized_decimal', $dimensions ) ) );

    if ( ! empty( $dimension_string ) ) {
        // Format custom converted array in a string and append it to default formatted dimensions string
        $dimension_string2 = ' ( ' . implode( ' x ', $dimentions2 ) . ' ' . __( 'cm', 'woocommerce' ) . ' )';
        $dimension_string .= ' ' . get_option( 'woocommerce_dimension_unit' ) . $dimension_string2;
    } else {
        $dimension_string = __( 'N/A', 'woocommerce' );
    }

    return $dimension_string;
}

示例:

class ExSimpleXMLElement extends SimpleXMLElement
{    
   function setAttribute(ExSimpleXMLElement $node, $attributeName, $attributeValue, $replace=true)
   {
        $attributes = $node->attributes();

        if (isset($attributes[$attributeName])) {
          if(!empty($attributeValue)){
            if($replace){
                $attributes->$attributeName = (string)$attributeValue;
            } else {
                $attributes->$attributeName = (string)$attributes->$attributeName.(string)$attributeValue;
            }
          } else {
            unset($attributes->$attributeName);
          }
        } else {
          $node->addAttribute($attributeName, $attributeValue);
        }
    }
}

结果:

<?php
  $xml_string = <<<XML
    <root>
        <item id="foo"/>
    </root>
  XML;

  $xml1 = simplexml_load_string($xml_string, 'ExSimpleXMLElement');
  $xml1->setAttribute($xml1, 'id', 'bar');
  echo $xml1->asXML();

  $xml2 = simplexml_load_string($xml_string, 'ExSimpleXMLElement');
  $xml2->setAttribute($xml2->item, 'id', 'bar');
  echo $xml2->asXML();

  $xml3 = simplexml_load_string($xml_string, 'ExSimpleXMLElement');
  $xml3->setAttribute($xml3->item, 'id', 'bar', false);
  echo $xml3->asXML();

  $xml4 = simplexml_load_string($xml_string, 'ExSimpleXMLElement');
  $xml4->setAttribute($xml4->item, 'id', NULL);
  echo $xml4->asXML();
?>

答案 3 :(得分:0)

您可以使用此功能(如果没有名称空间):

function setAttribute(SimpleXMLElement $node, $attributeName, $attributeValue)
{
    $attributes = $node->attributes();
    if (isset($attributes->$attributeName)) {
        $attributes->$attributeName = $attributeValue;
    } else {
        $attributes->addAttribute($attributeName, $attributeValue);
    }
}