[fpc-pascal] Modify XML Document

Tony Pelton tpelton at gmail.com
Fri Jun 23 05:31:15 CEST 2006


On 6/22/06, Felipe Monteiro de Carvalho
<felipemonteiro.carvalho at gmail.com> wrote:

-- snip TDOCDocument interface source from FPC 2.0.2 --
TDOMDocument = class(TDOMNode_WithChildren)
  protected
    FDocType: TDOMDocumentType;
    FImplementation: TDOMImplementation;
    function GetDocumentElement: TDOMElement;
  public
    property DocType: TDOMDocumentType read FDocType;
    property Impl: TDOMImplementation read FImplementation;
    property DocumentElement: TDOMElement read GetDocumentElement;

    function CreateElement(const tagName: DOMString): TDOMElement; virtual;
    function CreateDocumentFragment: TDOMDocumentFragment;
    function CreateTextNode(const data: DOMString): TDOMText;
    function CreateComment(const data: DOMString): TDOMComment;
    function CreateCDATASection(const data: DOMString): TDOMCDATASection;
      virtual;
    function CreateProcessingInstruction(const target, data: DOMString):
      TDOMProcessingInstruction; virtual;
    function CreateAttribute(const name: DOMString): TDOMAttr; virtual;
    function CreateEntityReference(const name: DOMString): TDOMEntityReference;
      virtual;
    // Free NodeList with TDOMNodeList.Release!
    function GetElementsByTagName(const tagname: DOMString): TDOMNodeList;

    // Extensions to DOM interface:
    constructor Create;
    function CreateEntity(const data: DOMString): TDOMEntity;
  end;
-- snip --

it's been a bit since i played with the FPC implementation of the XML
stuff, but i've done quite a bit of work with DOM in Java.

i've included the interface from dom.pp in the 2.0.2 source above for reference.

first thing, you need to understand is that TDOMDocument is the
"handle" to the DOM.

so you need to start with an instance of one of these, either by
creating one, or by "loading" an XML document.

from there, you ...

a) create "nodes" by calling methods on the TDOMDocument (the parent
of all nodes).

i included some of the specific and more common methods from
TDOMDocument below, such as the <CreateElement> function, and the
<CreateAttribute> function for instance.

-- snip --
    function CreateElement(const tagName: DOMString): TDOMElement; virtual;
    function CreateTextNode(const data: DOMString): TDOMText;
    function CreateCDATASection(const data: DOMString): TDOMCDATASection;
      virtual;
    function CreateAttribute(const name: DOMString): TDOMAttr; virtual;
-- snip --

again, you call these on a properly created instance of the
TDOMDocument ... because nodes must be "owned" by a document.

b) once you have a node, and you've mutated it (set its value etc ...)
to your liking, you can then parent it into the XMLDocument by walking
the DOM ( getChildren() etc ...) until you get to a node that you want
to attach your newly created node to, as a child or sibling or what
have you.

you create attributes in the same way, by calling methods on the
TDOMDocument, which return instances of the attribute class, and then
they can be attached to a node in the tree as required, becoming an
attribute of that node.

you never instantiate nodes and attributes directly, at least not ime.

> Felipe Monteiro de Carvalho

Tony

-- 
X-SA user ? 0.5.1 is out !
XData 0.1 for X-SA is out !
http://x-plane.dsrts.com



More information about the fpc-pascal mailing list