downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

SimpleXMLElement::addAttribute> <Dealing with XML errors
Last updated: Fri, 13 Nov 2009

view this page in

The SimpleXMLElement class

Introduction

Represents an element in XML document.

Class synopsis

SimpleXMLElement
SimpleXMLElement {
/* Methods */
void addAttribute ( string $name , string $value [, string $namespace ] )
SimpleXMLElement addChild ( string $name [, string $value [, string $namespace ]] )
mixed asXML ([ string $filename ] )
SimpleXMLElement attributes ([ string $ns [, bool $is_prefix ]] )
SimpleXMLElement children ([ string $ns [, bool $is_prefix ]] )
array getDocNamespaces ([ bool $recursive ] )
string getName ( void )
array getNamespaces ([ bool $recursive ] )
bool registerXPathNamespace ( string $prefix , string $ns )
array xpath ( string $path )
}

Table of Contents



add a note add a note User Contributed Notes
SimpleXMLElement
brett at brettbrewer dot com
28-Oct-2009 12:53
Figuring out how to access the properties of a SimpleXmlElement object was a little tricky for me. In particular, it took a while to discover that I needed to cast my SimpleXmlElement properties to be of type "string" to print them or do comparisons on them. For instance, assuming you already have a string of xml in $xmlstr...

<?php
$sxml
= new SimpleXmlElement($xmlstr);

if ((string)
$sxml->property== "somevalue") {
    echo (string)
$sxml->property;
}
?>
The properties of a SimpleXmlElement object are objects themselves, so you need to put "(string)" before them, which casts their values to a string instead of an object. I assume if you were doing a numeric comparison you'd want to cast to an (int) or something numeric instead.

SimpleXMLElement::addAttribute> <Dealing with XML errors
Last updated: Fri, 13 Nov 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites