Php simplexml change attribute value

Use this corrected....

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];
        }
    }
}

Exemple :


result:



    




     




      




      

[PHP 5, PHP 7, PHP 8]

SimpleXMLElement::attributesIdentifies an element's attributes

Description

public SimpleXMLElement::attributes[?string $namespaceOrPrefix = null, bool $isPrefix = false]: ?SimpleXMLElement

Note: SimpleXML has made a rule of adding iterative properties to most methods. They cannot be viewed using var_dump[] or anything else which can examine objects.

Parameters

namespaceOrPrefix

An optional namespace for the retrieved attributes

isPrefix

Default to false

Return Values

Returns a SimpleXMLElement object that can be iterated over to loop through the attributes on the tag.

Returns null if called on a SimpleXMLElement object that already represents an attribute and not a tag.

Examples

Example #1 Interpret an XML string

The above example will output:

Xeoncross

12 years ago

It is really simple to access attributes using array form. However, you must convert them to strings or ints if you plan on passing the values to functions.



Then using a function



I can get the "id" like this

chris at chlab dot ch

10 years ago

Note that you must provide the namespace if you want to access an attribute of a non-default namespace:

Consider the following example:






XML;$sxml = new SimpleXMLElement[$xml];/**
* Access attribute of default namespace
*/
var_dump[[string] $sxml->Table[0]['Foo']];
// outputs: 'Bar'

/**
* Access attribute of non-default namespace
*/

var_dump[[int] $sxml->Table[0]['ExpandedColumnCount']];
// outputs: 0var_dump[[int] $sxml->Table[0]->attributes['ss', TRUE]->ExpandedColumnCount];
// outputs: '7'
?>

sarlak

11 years ago


   
        PHP
        www.php.net
   
   
        Java
        www.oracle.com/br/technologies/java/‎
   

Checks if the attribute value equals "Language", if equal prints everything that is related to "Language".

Verifica se o valor do atributo é igual a "Language", se for, imprime tudo o que for relativo ao mesmo.



output:
saída:

[name] PHP
[link] www.php.net

leap

11 years ago

If you want to save the value of an attribute into an array, typecast it to a string.

alan at performantsystems dot com

12 years ago

So lets say you have database type data in an XML string called $xmlstring with the key or item ID as an XML Attribute and all content data as regular XML Elements, as above. SimpleXML processes the Attributes as an array, so we can play along and push the Attributes into an array. Then we can get the value of any specific Attribute we want by addressing it by name, such as "ID".

Considering this data:



  
      Navarro Corp.
  
  
      Performant Systems
  
  
      Digital Showcase
  

Example of listing both the ID Attribute and Company Element values:

skerr at mojavi dot org

17 years ago

You can also access the node as an array to get attributes:

jurchiks101 at gmail dot com

4 years ago

An undocumented way - and, IMO, a hack - of getting an element's value OR attributes
[but not both] without type-casting is to use the function `current[]`:

Chủ Đề