Back to index

awl  0.53
Public Member Functions | Protected Attributes
XMLElement Class Reference

List of all members.

Public Member Functions

 __construct ($tagname, $content=false, $attributes=false, $xmlns=null)
 Constructor - nothing fancy as yet.
 CountElements ()
 Count the number of elements.
 SetAttribute ($k, $v)
 Set an element attribute to a value.
 SetContent ($v)
 Set the whole content to a value.
 GetTag ()
 Accessor for the tag name.
 GetNSTag ()
 Accessor for the full-namespaced tag name.
 GetAttribute ($attr)
 Accessor for a single attribute.
 GetAttributes ()
 Accessor for the attributes.
 GetContent ()
 Accessor for the content.
 GetElements ($tag=null, $recursive=false)
 Return an array of elements matching the specified tag, or all elements if no tag is supplied.
 GetPath ($path)
 Return an array of elements matching the specified path.
 AddSubTag (&$v)
 Add a sub-element.
NewElement ($tagname, $content=false, $attributes=false, $xmlns=null)
 Add a new sub-element.
 RenderContent ($indent=0, $nslist=null, $force_xmlns=false)
 Render just the internal content.
 Render ($indent=0, $xmldef="", $nslist=null, $force_xmlns=false)
 Render the document tree into (nicely formatted) XML.
 __tostring ()

Protected Attributes

 $tagname
 $xmlns
 $attributes
 $content
 $_parent

Detailed Description

Definition at line 20 of file XMLElement.php.


Constructor & Destructor Documentation

XMLElement::__construct ( tagname,
content = false,
attributes = false,
xmlns = null 
)

Constructor - nothing fancy as yet.

Parameters:
string$tagnameThe tag name of the new element
mixed$contentEither a string of content, or an array of sub-elements
array$attributesAn array of attribute name/value pairs
string$xmlnsAn XML namespace specifier

Definition at line 35 of file XMLElement.php.

                                                                                   {
    $this->tagname=$tagname;
    if ( gettype($content) == "object" ) {
      // Subtree to be parented here
      $this->content = array(&$content);
    }
    else {
      // Array or text
      $this->content = $content;
    }
    $this->attributes = $attributes;
    if ( isset($xmlns) ) {
      $this->xmlns = $xmlns;
    }
    else {
      if ( preg_match( '{^(.*):([^:]*)$}', $tagname, $matches) ) {
        $prefix = $matches[1];
        $tag = $matches[2];
        if ( isset($this->attributes['xmlns:'.$prefix]) ) {
          $this->xmlns = $this->attributes['xmlns:'.$prefix];
        }
      }
      else if ( isset($this->attributes['xmlns']) ) {
        $this->xmlns = $this->attributes['xmlns'];
      }
    }
  }

Member Function Documentation

Definition at line 324 of file XMLElement.php.

                        {
    return $this->Render();
  }

Here is the call graph for this function:

Add a sub-element.

Parameters:
objectAn XMLElement to be appended to the array of sub-elements

Definition at line 215 of file XMLElement.php.

                          {
    if ( gettype($this->content) != "array" ) $this->content = array();
    $this->content[] =& $v;
    return count($this->content);
  }

Count the number of elements.

Returns:
int The number of elements

Definition at line 68 of file XMLElement.php.

                            {
    if ( $this->content === false ) return 0;
    if ( is_array($this->content) ) return count($this->content);
    if ( $this->content == '' ) return 0;
    return 1;
  }

Accessor for a single attribute.

Parameters:
string$attrThe name of the attribute.
Returns:
string The value of that attribute of the element

Definition at line 121 of file XMLElement.php.

                                 {
    if ( $attr == 'xmlns' ) return $this->xmlns;
    if ( isset($this->attributes[$attr]) ) return $this->attributes[$attr];
    return null;
  }

Accessor for the attributes.

Returns:
array The attributes of this element

Definition at line 132 of file XMLElement.php.

                           {
    return $this->attributes;
  }

Accessor for the content.

Returns:
array The content of this element

Definition at line 141 of file XMLElement.php.

                        {
    return $this->content;
  }
XMLElement::GetElements ( tag = null,
recursive = false 
)

Return an array of elements matching the specified tag, or all elements if no tag is supplied.

Unlike GetContent() this will always return an array.

Returns:
array The XMLElements within the tree which match this tag

Definition at line 151 of file XMLElement.php.

                                                      {
    $elements = array();
    if ( gettype($this->content) == "array" ) {
      foreach( $this->content AS $k => $v ) {
        if ( empty($tag) || $v->GetNSTag() == $tag ) {
          $elements[] = $v;
        }
        if ( $recursive ) {
          $elements = $elements + $v->GetElements($tag,true);
        }
      }
    }
    else if ( empty($tag) || (isset($v->content->tagname) && $v->content->GetNSTag() == $tag) ) {
      $elements[] = $this->content;
    }
    return $elements;
  }

Accessor for the full-namespaced tag name.

Returns:
string The tag name of the element, prefixed by the namespace

Definition at line 112 of file XMLElement.php.

                      {
    return (empty($this->xmlns) ? '' : $this->xmlns . ':') . $this->tagname;
  }

Here is the caller graph for this function:

XMLElement::GetPath ( path)

Return an array of elements matching the specified path.

Returns:
array The XMLElements within the tree which match this tag

That is the full path

There is more to the path, so we recurse into that sub-part

If our input $path was not rooted, we recurse further

Definition at line 175 of file XMLElement.php.

                            {
    $elements = array();
    // printf( "Querying within '%s' for path '%s'\n", $this->tagname, $path );
    if ( !preg_match( '#(/)?([^/]+)(/?.*)$#', $path, $matches ) ) return $elements;
    // printf( "Matches: %s -- %s -- %s\n", $matches[1], $matches[2], $matches[3] );
    if ( $matches[2] == '*' || $matches[2] == $this->GetNSTag()) {
      if ( $matches[3] == '' ) {
        $elements[] = $this;
      }
      else if ( gettype($this->content) == "array" ) {
        foreach( $this->content AS $k => $v ) {
          $elements = array_merge( $elements, $v->GetPath($matches[3]) );
        }
      }
    }

    if ( $matches[1] != '/' && gettype($this->content) == "array" ) {
      foreach( $this->content AS $k => $v ) {
        $elements = array_merge( $elements, $v->GetPath($path) );
      }
    }
    // printf( "Found %d within '%s' for path '%s'\n", count($elements), $this->tagname, $path );
    return $elements;
  }

Here is the call graph for this function:

Accessor for the tag name.

Returns:
string The tag name of the element

Definition at line 103 of file XMLElement.php.

                    {
    return $this->tagname;
  }
& XMLElement::NewElement ( tagname,
content = false,
attributes = false,
xmlns = null 
)

Add a new sub-element.

Parameters:
stringThe tag name of the new element
mixedEither a string of content, or an array of sub-elements
arrayAn array of attribute name/value pairs
Returns:
objectref A reference to the new XMLElement

Definition at line 230 of file XMLElement.php.

                                                                                   {
    if ( gettype($this->content) != "array" ) $this->content = array();
    $element = new XMLElement($tagname,$content,$attributes,$xmlns);
    $this->content[] =& $element;
    return $element;
  }
XMLElement::Render ( indent = 0,
xmldef = "",
nslist = null,
force_xmlns = false 
)

Render the document tree into (nicely formatted) XML.

Parameters:
intThe indenting level for the pretty formatting of the element

Render the element attribute values

Definition at line 273 of file XMLElement.php.

                                                                           {
    $r = ( $xmldef == "" ? "" : $xmldef."\n");

    $attr = "";
    $tagname = $this->tagname;
    $xmlns_done = false;
    if ( gettype($this->attributes) == "array" ) {
      foreach( $this->attributes AS $k => $v ) {
        if ( preg_match('#^xmlns(:?(.+))?$#', $k, $matches ) ) {
//          if ( $force_xmlns ) printf( "1: %s: %s\n", $this->tagname, $this->xmlns );
          if ( !isset($nslist) ) $nslist = array();
          $prefix = (isset($matches[2]) ? $matches[2] : '');
          if ( isset($nslist[$v]) && $nslist[$v] == $prefix ) continue; // No need to include in list as it's in a wrapping element
          $nslist[$v] = $prefix;
          if ( !isset($this->xmlns) ) $this->xmlns = $v;
          $xmlns_done = true;
        }
        $attr .= sprintf( ' %s="%s"', $k, htmlspecialchars($v) );
      }
    }
    if ( isset($this->xmlns) && isset($nslist[$this->xmlns]) && $nslist[$this->xmlns] != '' ) {
//      if ( $force_xmlns ) printf( "2: %s: %s\n", $this->tagname, $this->xmlns );
      $tagname = $nslist[$this->xmlns] . ':' . $tagname;
      if ( $force_xmlns ) $attr .= sprintf( ' xmlns="%s"', $this->xmlns);
    }
    else if ( isset($this->xmlns) && !isset($nslist[$this->xmlns]) && gettype($this->attributes) == 'array' && !isset($this->attributes[$this->xmlns]) ) {
//      if ( $force_xmlns ) printf( "3: %s: %s\n", $this->tagname, $this->xmlns );
      $attr .= sprintf( ' xmlns="%s"', $this->xmlns);
    }
    else if ( $force_xmlns && isset($this->xmlns) && ! $xmlns_done ) {
//      printf( "4: %s: %s\n", $this->tagname, $this->xmlns );
      $attr .= sprintf( ' xmlns="%s"', $this->xmlns);
    }
    
    $r .= substr("                        ",0,$indent) . '<' . $tagname . $attr;

    if ( (is_array($this->content) && count($this->content) > 0) || (!is_array($this->content) && strlen($this->content) > 0) ) {
      $r .= ">";
      $r .= $this->RenderContent($indent,$nslist,$force_xmlns);
      $r .= '</' . $tagname.">\n";
    }
    else {
      $r .= "/>\n";
    }
    return $r;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

XMLElement::RenderContent ( indent = 0,
nslist = null,
force_xmlns = false 
)

Render just the internal content.

Returns:
string The content of this element, as a string without this element wrapping it.

Render the sub-elements with a deeper indent level

Render the content, with special characters escaped

  

Definition at line 243 of file XMLElement.php.

                                                                       {
    $r = "";
    if ( is_array($this->content) ) {
      $r .= "\n";
      foreach( $this->content AS $k => $v ) {
        if ( is_object($v) ) {
          $r .= $v->Render($indent+1, "", $nslist, $force_xmlns);
        }
      }
      $r .= substr("                        ",0,$indent);
    }
    else {
      $r .= htmlspecialchars($this->content, ENT_NOQUOTES );
    }
    return $r;
  }

Here is the caller graph for this function:

XMLElement::SetAttribute ( k,
v 
)

Set an element attribute to a value.

Parameters:
stringThe attribute name
stringThe attribute value

Definition at line 81 of file XMLElement.php.

                               {
    if ( gettype($this->attributes) != "array" ) $this->attributes = array();
    $this->attributes[$k] = $v;
    if ( strtolower($k) == 'xmlns' ) {
      $this->xmlns = $v;
    }
  }

Set the whole content to a value.

Parameters:
mixedThe element content, which may be text, or an array of sub-elements

Definition at line 94 of file XMLElement.php.

                          {
    $this->content = $v;
  }

Member Data Documentation

XMLElement::$_parent [protected]

Definition at line 25 of file XMLElement.php.

Definition at line 23 of file XMLElement.php.

XMLElement::$content [protected]

Definition at line 24 of file XMLElement.php.

XMLElement::$tagname [protected]

Definition at line 21 of file XMLElement.php.

XMLElement::$xmlns [protected]

Definition at line 22 of file XMLElement.php.


The documentation for this class was generated from the following file: