xHarbour Reference Documentation > Class Reference (textmode) xHarbour Developers Network  

THtmlNode()

Creates a new THtmlNode object.

Syntax

THtmlNode():new( <cHtmlString> ) --> oTHtmlNode

or

THtmlNode():new( <oTHtmlNode>  , ;
                 <cTagName>    , ;
                {<hAttributes>], ;
                [<cText>]        ) --> oTHtmlNode

Arguments

<cHtmlString>
When a character string is passed to method :new(), the string is parsed into a tree of THtmlNode objects. The returned THtmlNode object represents the root node.
<oTHtmlNode>
When a THtmlNode object is passed as first parameter, it serves as parent node for the new object created.
<cTagName>
This is a character string holding the tag name of the THtmlNode object.
<hAttributes>
Optionally, the attributes of of the HTML node to create can be passed as a Hash. The Hash key is the attribute name and the Hash value is the attribute value. Note that both, Hash values and keys, must be of data type Character.

Alternatively, a HTML compliant character string holding the attributes of the THtmlNode object can be passed (e.g. 'border="0" cellspacing="0" cellpadding="0"' are attributes for a <table> node).

<cText>
This is a character string holding the textual content of the THtmlNode object. If this parameter is used, the THtmlNode object represents a leaf, not a node. <cText> is the text appearing between the opening and closing HTML <cTagName>.

Return

The function returns a THtmlNode object and method :new() initializes the object.

Description

Objects of the THtmlNode class represent a single node, or a leaf, in an HTML document. HTML documents are maintained by the THtmlDocument() class which builds a tree of HTML nodes when reading an HTML document. A THtmlDocument object creates this tree and fills it with THtmlNode objects.

The THtmlNode class is designed for comfortable HTML processing from PRG source code. The creation of HTML documents is supported by operator overloading and built in lookup tables representing all HTML 4.0 tag names and associated attributes. Refer to Overloaded operators below.

Note:  when no THtmlNode object exists anymore, the memory resources for the HTML lookup tables should be released with function THtmlCleanup().

Html Attribute data

:attr
Attributes of an HTML node.

Html Node data

:childNodes
Child nodes of an HTML node.
:document
Document of an HTML node.
:nextNode
Next HTML node.
:parentNode
Parent node
:prevNode
Previous HTML node.
:siblingNodes
Sibling nodes of an HTML node.

Html Tag data

:htmlEndTagName
Character string with closing tag name.
:htmlTagName
Character string with opening tag name.
:htmlTagType
Array specifying the type of an HTML tag.
:isBlock
Checks if an HTML tag designates a block of text.
:isEmpty
Checks if an HTML tag is always empty.
:isInline
Checks if an HTML tag may appear inline.
:isNode
Checks if this is a node.
:isOptional
Checks if an HTML tag has an optional end tag.

Html Text data

:text
Text node.

Html Attribute methods

:attrToString() --> cAttributes
Returns all HTML attributes as a character string.
:delAttribute( <cAttrName> ) --> lSuccess
Deletes an HTML attribute.
:delAttributes() --> lSuccess
Removes all HTML attributes.
:getAttribute( <cAttrName> ) --> cAttrValue|NIL
Returns the value of an HTML attribute.
:getAttributes() --> hHash
Returns all HTML attributes.
:isAttribute( <cAttrName> ) --> lExists
Checks if an attribute is present.
:setAttribute( <cAttrName>, <cAttrValue> ) --> cValue
Sets an attribute and its value.
:setAttributes( <cHtmlAttr> ) --> hHash
Sets multiple attributes and their values.

Html Node methods

:addNode( <oTHtmlNode> ) --> oTHtmlNode
Adds a child node.
:collect() --> aTHtmlNodes
Returns all nodes including this node.
:delete() --> self
Removes an HTML node from the HTML tree.
:firstNode( [<lRoot>] ) --> oTHtmlNode
Returns the first node below this node.
:insertAfter( <oTHtmlNode> ) --> oTHtmlNode
Inserts an HTML node after the current node.
:insertBefore( <oTHtmlNode> ) --> oTHtmlNode
Inserts an HTML node before the current node.
:insertBelow( <oTHtmlNode> ) --> oTHtmlNode
Inserts a new HTML node below the current HTML node.
:lastNode() --> oTHtmlNode
Returns the last node below this node.
:unlink() --> self
Synonym for :delete()

Html Tag methods

:isType( <nCM_TYPE> ) --> lIsType
Checks the content model type of an HTML tag.

Html Text methods

:getText( [<cCRLF>] ) --> cHtmlText
Returns the textual content of an HTML node.
:toString() --> cHtmlString
Returns a HTML formatted character string.

Overloaded operators

+ <cTagName>
Opens a new HTML node
- <cTagName>
Closes a new HTML node
:<tagName> --> oTHtmlNode
Returns the first HTML node with the specified tag name.
:<tagName>S[(<nOrdinal>)] --> aTHtmlNodes
Returns all HTML nodes with the specified tag name.

Info

See also:AnsiToHtml(), THtmlCleanup(), THtmlInit(), THtmlDocument(), TIpClientHttp()
Category: HTML functions , Object functions , xHarbour extensions
Header:thtml.ch
Source:tip\thtml.prg
LIB:xhb.lib
DLL:xhbdll.dll

Example

// This example demonstrates operator overloading for
// creating an HTML document from a database.

   PROCEDURE Main
      LOCAL oDoc, oNode, oTable, oRow, oCell
      LOCAL nDll, pApi
      CLS

      TRY
         USE Test.dbf
      CATCH
         ? "Error: Database not found TEST.DBF"
         QUIT
      END

      oDoc          := THtmlDocument():new()

      // Operator "+" creates a new node
      oNode         := oDoc:head + "meta"
      oNode:name    := "Generator"
      oNode:content := "THtmlDocument"

      // Operator ":" returns first "h1" from body (creates if not existent)
      oNode         := oDoc:body:h1
      oNode:text    := "My address book"

      // Operator "+" creates a new <p> node
      oNode         := oDoc:body + "p"

      // Operator "+" creates a new <font> node with attribute
      oNode         := oNode   + 'font size="5"'
      oNode:text    := "This is a "

      // Operator "+" creates a new <b> node
      oNode         := oNode   + "b"

      // Operator "+" creates a new <font> node with attribute
      oNode         := oNode   + "font color='blue'"
      oNode:text    := "sample "

      // Operator "-" closes 2nd <font>, result is <b> node
      oNode         := oNode   - "font"

      // Operator "-" closes <b> node, result is 1st <font> node
      oNode         := oNode   - "b"

      oNode:text    := "database!"

      // Operator "-" closes 1st <font> node, result is <p> node
      oNode         := oNode   - "font"

      oNode         := oNode   + "hr"

      // Operator ":" returns first "table" from body (creates if not existent)
      oTable        := oDoc:body:table
      oTable:attr   := 'border="0" width="100%" cellspacing="0" cellpadding="0"'

      oRow          := oTable  + 'tr bgcolor="lightcyan"'
      FOR i:=1 TO FCount()
          oCell     := oRow + "th"
          oCell:text:= FieldName(i)
      NEXT

      FOR i:=1 TO 10
         oRow         := oTable + "tr"
         oRow:bgColor := IIf( Recno() % 2 == 0, "lightgrey", "white" )

         FOR j:=1 TO FCount()
            oCell      := oRow + "td"
            oCell:text := FieldGet(j)
         NEXT

         SKIP
      NEXT

      oNode := oDoc:body  + "hr"
      oNode := oDoc:body  + "p"

      oNode:text := "10 records from database " + Alias() + ".dbf"

      DbCloseArea()

      IF oDoc:writeFile( "Address.html" )
         ? "File created: Address.html"
      ELSE
         ? "Error: ", FError()
      ENDIF

      WAIT
      ? HtmlToOem( oDoc:body:getText() )

      nDll := DllLoad( "Shell32.dll" )
      pApi := GetProcAddress( nDll, "ShellExecute" )

      CallDll( pApi, 0, "open", "Address.html", NIL, "", 1 )

      DllUnload( nDll )
   RETURN

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe