Class TXmlDocument

Description

TXmlDocument class.

TXmlDocument represents a DOM representation of an XML file. Besides all properties and methods inherited from TXmlElement, you can load an XML file or string by loadFromFile or loadFromString. You can also get the version and encoding of the XML document by the Version and Encoding properties.

To construct an XML string, you may do the following:

  1. $doc=new TXmlDocument('1.0','utf-8');
  2. $doc->TagName='Root';
  3.  
  4. $proc=new TXmlElement('Proc');
  5. $proc->setAttribute('Name','xxxx');
  6. $doc->Elements[]=$proc;
  7.  
  8. $query=new TXmlElement('Query');
  9. $query->setAttribute('ID','xxxx');
  10. $proc->Elements[]=$query;
  11.  
  12. $attr=new TXmlElement('Attr');
  13. $attr->setAttribute('Name','aaa');
  14. $attr->Value='1';
  15. $query->Elements[]=$attr;
  16.  
  17. $attr=new TXmlElement('Attr');
  18. $attr->setAttribute('Name','bbb');
  19. $attr->Value='1';
  20. $query->Elements[]=$attr;
The above code represents the following XML string:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Root>
  3. <Proc Name="xxxx">
  4. <Query ID="xxxx">
  5. <Attr Name="aaa">1</Attr>
  6. <Attr Name="bbb">1</Attr>
  7. </Query>
  8. </Proc>
  9. </Root>

  • since: 3.0
  • version: $Id: TXmlDocument.php 2370 2008-01-23 01:49:02Z xue $
  • author: Qiang Xue <qiang.xue@gmail.com>

Located in /Xml/TXmlDocument.php (line 307)

TComponent
   |
   --TXmlElement
      |
      --TXmlDocument
Method Summary
TXmlDocument __construct ([string $version = '1.0'], [string $encoding = ''])
string getEncoding ()
string getVersion ()
boolean loadFromFile (string $file)
boolean loadFromString (string $string)
void saveToFile (string $file)
string saveToString ()
void setEncoding (string $encoding)
void setVersion (string $version)
string __toString ()
Methods
Constructor __construct (line 323)

Constructor.

  • access: public
TXmlDocument __construct ([string $version = '1.0'], [string $encoding = ''])
  • string $version: version of this XML document
  • string $encoding: encoding of this XML document

Redefinition of:
TXmlElement::__construct()
Constructor.
getEncoding (line 349)
  • return: encoding of this XML document
  • access: public
string getEncoding ()
getVersion (line 333)
  • return: version of this XML document
  • access: public
string getVersion ()
loadFromFile (line 368)

Loads and parses an XML document.

  • return: whether the XML file is parsed successfully
  • access: public
  • throws: TIOException if the file fails to be opened.
boolean loadFromFile (string $file)
  • string $file: the XML file path
loadFromString (line 382)

Loads and parses an XML string.

The version and encoding will be determined based on the parsing result.

  • return: whether the XML string is parsed successfully
  • access: public
boolean loadFromString (string $string)
  • string $string: the XML string
saveToFile (line 415)

Saves this XML document as an XML file.

  • access: public
  • throws: TIOException if the file cannot be written
void saveToFile (string $file)
  • string $file: the name of the file to be stored with XML output
saveToString (line 430)

Saves this XML document as an XML string

  • return: the XML string of this XML document
  • access: public
string saveToString ()
setEncoding (line 357)
  • access: public
void setEncoding (string $encoding)
  • string $encoding: encoding of this XML document
setVersion (line 341)
  • access: public
void setVersion (string $version)
  • string $version: version of this XML document
__toString (line 452)

Magic-method override. Called whenever this document is used as a string.

  1. $document = new TXmlDocument();
  2. $document->TagName = 'root';
  3. echo $document;
or
  1. $document = new TXmlDocument();
  2. $document->TagName = 'root';
  3. $xml = (string)$document;

  • return: string representation of this document
  • access: public
string __toString ()

Redefinition of:
TXmlElement::__toString()
Magic-method override. Called whenever this element is used as a string.

Inherited Methods

Inherited From TXmlElement

TXmlElement::__construct()
TXmlElement::getAttribute()
TXmlElement::getAttributes()
TXmlElement::getElementByTagName()
TXmlElement::getElements()
TXmlElement::getElementsByTagName()
TXmlElement::getHasAttribute()
TXmlElement::getHasElement()
TXmlElement::getParent()
TXmlElement::getTagName()
TXmlElement::getValue()
TXmlElement::setAttribute()
TXmlElement::setParent()
TXmlElement::setTagName()
TXmlElement::setValue()
TXmlElement::toString()
TXmlElement::__toString()

Inherited From TComponent

TComponent::addParsedObject()
TComponent::attachEventHandler()
TComponent::canGetProperty()
TComponent::canSetProperty()
TComponent::createdOnTemplate()
TComponent::detachEventHandler()
TComponent::evaluateExpression()
TComponent::evaluateStatements()
TComponent::getEventHandlers()
TComponent::getSubProperty()
TComponent::hasEvent()
TComponent::hasEventHandler()
TComponent::hasProperty()
TComponent::raiseEvent()
TComponent::setSubProperty()
TComponent::__get()
TComponent::__set()

Documentation generated on Mon, 21 Apr 2008 11:36:52 -0400 by phpDocumentor 1.3.0RC4